iOS几种简单动画的实现

文章目录


CABasicAnimation 实现的几种常用的简单动画

不断旋转

-(void)setImageRote:(UIImageView *)imageView andAngel:(float)angel andTime:(float)time
{
    
    NSLog(@"开始旋转");
    
    CABasicAnimation* rotationAnimation;
    
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    rotationAnimation.toValue = [NSNumber numberWithFloat: angel];
    
    rotationAnimation.duration =time;
    
    rotationAnimation.cumulative =YES;
    
    rotationAnimation.repeatCount =MAXFLOAT;
    
    [imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

不断放大缩小

-(void)setImage:(UIImageView *)imageView scale:(CGFloat) scale andDuration:(CGFloat)time
{
    
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.autoreverses = YES;
    scaleAnimation.fromValue = [NSNumber numberWithDouble:0.9]; //一开始时是0.9的大小
    scaleAnimation.toValue = [NSNumber numberWithDouble:scale];  //结束时是scale的大小
    scaleAnimation.duration = time; //设置时间
    scaleAnimation.repeatCount = MAXFLOAT; //重复次数
    [imageView.layer addAnimation:scaleAnimation forKey:@"CQScale"]; //添加动画
  
    
}

随机方向来回平移一段距离

-(void)setAnimationMove:(UIImageView *)imageV changdu:(CGFloat)changdu andDuration:(CGFloat)time
{
 
    int aaa = arc4random() % 2;
    if (aaa == 1) {
        changdu = changdu * 1.0;
    }else{
        changdu = -1.0 * changdu;
    }
    
    CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@[@"transform.translation.x",@"transform.translation.y"][arc4random()%2]];
    positionAnimation.autoreverses = YES;
    positionAnimation.fromValue = [NSNumber numberWithDouble:0.f];
    positionAnimation.toValue = [NSNumber numberWithDouble:changdu];
    positionAnimation.duration = 2;
    positionAnimation.repeatCount = MAXFLOAT;
    [imageV.layer addAnimation:positionAnimation forKey:@"CQPosition"];

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容