首页 > 代码库 > 视图自定义旋转动画 类似百度音乐
视图自定义旋转动画 类似百度音乐
@interface FirstViewController ()
@property (nonatomic,assign)BOOL isplay;
@end
@implementation FirstViewController
@synthesize isplay;
- (void)viewDidLoad
{
[superviewDidLoad];
isplay = NO;
//圆角
self.imgview.layer.cornerRadius =150.0 / 2.0;
self.imgview.layer.masksToBounds =YES;
//添加动画
CABasicAnimation *monkeyAnimation = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.z"];
monkeyAnimation.toValue = [NSNumbernumberWithFloat:2.0 *M_PI];
monkeyAnimation.duration = 1.5f;
monkeyAnimation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionLinear];
monkeyAnimation.cumulative = NO;
monkeyAnimation.removedOnCompletion = NO; //No Remove
monkeyAnimation.repeatCount = FLT_MAX;
[self.imgview.layeraddAnimation:monkeyAnimation forKey:@"AnimatedKey"];
[self.imgviewstopAnimating];
// 加载动画但不播放动画
self.imgview.layer.speed =0.2;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//开始动画
- (IBAction)startAnimate:(id)sender {
if (!isplay) {
isplay = YES;
self.imgview.layer.speed =1.0;
self.imgview.layer.beginTime =0.0;
CFTimeInterval pausedTime = [self.imgview.layertimeOffset];
CFTimeInterval timeSincePause = [self.imgview.layerconvertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
self.imgview.layer.beginTime = timeSincePause;
}
}
//停止动画并保存当前的角度
- (IBAction)stioAnimate:(id)sender {
if (isplay) {
isplay = NO;
CFTimeInterval pausedTime = [self.imgview.layerconvertTime:CACurrentMediaTime() fromLayer:nil];
self.imgview.layer.speed =0.0;
self.imgview.layer.timeOffset = pausedTime;
}
}
@end