用基础动画实现iOS控件循环旋转

- (void)viewDidLoad {
    [super viewDidLoad];    
    UIButton* ag=[[UIButton alloc]initWithFrame:CGRectMake(screenWidth/2-75, 174, 150, 150)];
    UIImage* img=[UIImage imageNamed:@"m_01"];
    [ag setImage:img forState:UIControlStateNormal];
    ag.backgroundColor=[UIColor redColor];
    [self rotate360DegreeWithButton:ag];
    [self.view addSubview:ag];
}

- (UIButton*)rotate360DegreeWithButton:(UIButton*)button
{
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.toValue = [NSNumber numberWithFloat:M_PI];
    animation.duration = 10;
    //旋转效果累计 先转180度 接着再旋转180度 从而实现360旋转
    animation.cumulative = YES;
    animation.repeatCount = 1000;
    [button.layer addAnimation:animation forKey:@"transform.rotation.z"];
    return button;
}

 

posted @ 2015-12-09 13:48  death3721  阅读(545)  评论(0编辑  收藏  举报