How to rotate image 360 degrees endlessly using iP

2019-06-09 18:38发布

I want to rotate image 360 degrees endlessly using iPhone SDK?

How can this be done?

Thanks!

2条回答
Lonely孤独者°
2楼-- · 2019-06-09 19:24
-(void)startAnimationWithRevolutions:(float)revPerSecond forTime:(float)time
{
    spinWheel.userInteractionEnabled = FALSE;
    float totalRevolutions = revPerSecond * time;
    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:time] forKey:kCATransactionAnimationDuration];

    CABasicAnimation* spinAnimation = [CABasicAnimation
                                       animationWithKeyPath:@"transform.rotation"];
    CGAffineTransform transform = spinWheel.transform;
    float fromAngle = atan2(transform.b, transform.a);
    float toAngle = fromAngle + (totalRevolutions*4*M_PI);
    spinAnimation.fromValue = [NSNumber numberWithFloat:fromAngle];
    spinAnimation.toValue = [NSNumber numberWithFloat:toAngle];
    spinAnimation.repeatCount = 0;
    spinAnimation.removedOnCompletion = NO;
    spinAnimation.delegate = self;
    spinAnimation.timingFunction = [CAMediaTimingFunction functionWithName:
                                    kCAMediaTimingFunctionEaseOut];
    [spinWheel.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
    [CATransaction commit];
}

Try using this, it works.

查看更多
劳资没心,怎么记你
3楼-- · 2019-06-09 19:30

Assuming you are displaying the image in a UIImageView, you can rotate the view's local coordinate system (see here) and use NSTimer to increase the rotation periodically (see here).

(I would use OpenGL for this, but that's just a personal preference and is probably overkill for your situation.)

查看更多
登录 后发表回答