ios UIButton一直旋轉動畫

weixin_34007291發表於2017-12-25

-(void)viewDidAppear:(BOOL)animated{
​ UIButton * buyBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 0, 50, 50)];
[buyBtn setImage:[UIImage imageNamed:@"AppIcon40x40"] forState:UIControlStateNormal];
[segmentView2 addSubview:buyBtn];
buyBtn=[self rotate360DegreeWithImageView:buyBtn];
}

  • (UIButton *)rotate360DegreeWithImageView:(UIButton *)imageView{

    CABasicAnimation *animation = [ CABasicAnimation animationWithKeyPath: @"transform" ];

    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];

    //圍繞Z軸旋轉,垂直與螢幕

    animation.toValue = [ NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI/2.0, 0.0, 0.0, 1.0) ];

    animation.duration = 3;

    //旋轉效果累計,先轉180度,接著再旋轉180度,從而實現360旋轉

    animation.cumulative = YES;

    animation.repeatCount = MAXFLOAT;

    /*下面可以不需要
    CGRect imageRrect = CGRectMake(0, 0,imageView.imageView.frame.size.width, imageView.imageView.frame.size.height);
    UIGraphicsBeginImageContext(imageRrect.size);
    //在圖片邊緣新增一個畫素的透明區域,去圖片鋸齒
    [imageView.currentImage drawInRect:CGRectMake(1,1,imageView.imageView.frame.size.width-2,imageView.imageView.frame.size.height-2)];
    [imageView setImage: UIGraphicsGetImageFromCurrentImageContext() forState:UIControlStateNormal];
    UIGraphicsEndImageContext();
    */
    [imageView.layer addAnimation:animation forKey:nil];

    return imageView;
    }

相關文章