视图自己定义旋转动画 相似百度音乐

@interface FirstViewController ()

@property (nonatomic,assign)BOOL isplay;

@end




@implementation FirstViewController

@synthesize isplay;


- (void)viewDidLoad

{

    [super viewDidLoad];

    isplay = NO;

    //圆角

    self.imgview.layer.cornerRadius = 150.0 / 2.0;

    self.imgview.layer.masksToBounds = YES;

    

    //加入动画

    CABasicAnimation *monkeyAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

    monkeyAnimation.toValue = [NSNumber numberWithFloat:2.0 *M_PI];

monkeyAnimation.duration = 1.5f;

monkeyAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    monkeyAnimation.cumulative = NO;

    monkeyAnimation.removedOnCompletion = NO; //No Remove

    monkeyAnimation.repeatCount = FLT_MAX;

[self.imgview.layer addAnimation:monkeyAnimation forKey:@"AnimatedKey"];

    [self.imgview stopAnimating];

    

    // 载入动画 但不播放动画

    self.imgview.layer.speed = 0.2;

    

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // 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.layer timeOffset];

        CFTimeInterval timeSincePause = [self.imgview.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;

        self.imgview.layer.beginTime = timeSincePause;

    }

    

    

}

//停止动画并保存当前的角度

- (IBAction)stioAnimate:(id)sender {

    if (isplay) {

        isplay = NO;

        CFTimeInterval pausedTime = [self.imgview.layer convertTime:CACurrentMediaTime() fromLayer:nil];

        self.imgview.layer.speed = 0.0;

        self.imgview.layer.timeOffset = pausedTime;

    }


}

@end

posted @ 2016-03-24 21:48  yxwkaifa  阅读(217)  评论(0编辑  收藏  举报