How to rotate image 360 degrees endlessly using iPhone SDK?

最后都变了- 提交于 2019-12-23 05:12:34

问题


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

How can this be done?

Thanks!


回答1:


-(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.




回答2:


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.)



来源:https://stackoverflow.com/questions/6080043/how-to-rotate-image-360-degrees-endlessly-using-iphone-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!