iOS 动画效果实现随记


/// 开始旋转动画,效果:Y轴360度旋转后停留2秒再继续旋转

- (void) startRotationAnimation {

    // 添加旋转动画

    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];

    rotationAnimation.fromValue= [NSNumber numberWithFloat:0];

    rotationAnimation.toValue= [NSNumber numberWithFloat:M_PI*2];

    rotationAnimation.duration=1.5;// 旋转动画1.5秒完成

    rotationAnimation.repeatCount=1;


    // 创建动画组

    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];

    animationGroup.animations=@[rotationAnimation];

    animationGroup.duration = 3.5; //间隔2秒后再次开始旋转动画

    animationGroup.repeatCount=HUGE_VALF;

    [self.layer removeAnimationForKey:@"RotationAnimation"];

    [self.layer addAnimation:animationGroup forKey:@"RotationAnimation"];

}


/// 缩放动画,效果:1-1.2倍循环缩放

- (void) startScaleAnimation {

    // 缩放动画

    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

    scaleAnimation.fromValue= [NSNumber numberWithFloat:1.0];

    scaleAnimation.toValue= [NSNumber numberWithFloat:1.2];

    scaleAnimation.duration=0.4;

    scaleAnimation.autoreverses=YES;

    scaleAnimation.repeatCount=HUGE_VALF;


    // 创建动画组

    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];

    animationGroup.animations=@[scaleAnimation];

    animationGroup.duration=0.8;

    animationGroup.repeatCount=HUGE_VALF;


    [self.layer removeAnimationForKey:@"ScaleAnimation"];

    [self.layer addAnimation:animationGroup forKey:@"ScaleAnimation"];

}


/// 晃动效果动画

- (void) startShakeAnimation{

    // 晃动动画

    CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];

    shakeAnimation.values=@[@(-0.3),@(0.3),@(-0.3)];// 晃动角度

    shakeAnimation.keyTimes=@[@(0),@(0.5),@(1)];// 晃动时机

    shakeAnimation.autoreverses=YES;

    shakeAnimation.duration=0.5;


    // 创建动画组

    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];

    animationGroup.animations=@[shakeAnimation];

    animationGroup.duration=2.0;

    animationGroup.repeatCount=HUGE_VALF;


    [self.layer removeAnimationForKey:@"ScaleAnimation"];

    [self.layer addAnimation:animationGroup forKey:@"ScaleAnimation"];

}


/// 添加白光闪过动画

- (void) startWhiteLightAnimation{


    for(CALayer*layer in self.layer.sublayers){

        if([layer.name isEqualToString:@"whiteLightLayer"]){

            [layer removeFromSuperlayer];

            break;

        }

    }


    CALayer*whiteLightLayer = [CALayer layer];

    whiteLightLayer.backgroundColor= [[UIColor whiteColor] colorWithAlphaComponent:0.7].CGColor;

    whiteLightLayer.frame=CGRectMake(5,0,self.frame.size.width-10,3);

    whiteLightLayer.opacity=0.0;

    whiteLightLayer.name=@"whiteLightLayer";

    [self.layer addSublayer:whiteLightLayer];


    // 创建一个 CATransform3D 对象来设置倾斜的角度

    CATransform3D transform = CATransform3DIdentity;

    transform =CATransform3DRotate(transform, -M_PI_4,0.0,0.0,1.0);

    whiteLightLayer.transform= transform;


    // 改变位置动画

    CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];

    positionAnimation.fromValue= [NSValue valueWithCGPoint:CGPointMake(0,0)];

    positionAnimation.toValue= [NSValue valueWithCGPoint:CGPointMake(self.frame.size.width,self.frame.size.height)];

    positionAnimation.duration=0.5;

    positionAnimation.autoreverses=YES;


    // 改变透明度动画

    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];

    opacityAnimation.fromValue=@(1.0);

    opacityAnimation.toValue=@(0.0);

    opacityAnimation.duration=0.5;


    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];

    animationGroup.animations=@[opacityAnimation, positionAnimation];

    animationGroup.duration=1.5;// 持续时间

    animationGroup.repeatCount=HUGE_VALF;


    [whiteLightLayer addAnimation:animationGroup forKey:@"WhiteLightAnimation"];

}

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

推荐阅读更多精彩内容

  • iOS动画篇之CoreAnimation动画 9月 22, 2016发布在Objective-C App如果想被大...
    白水灬煮一切阅读 2,077评论 0 0
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,141评论 5 13
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,551评论 6 30
  • 转自:http://blog.csdn.net/zhibudefeng/article/details/86915...
    咖啡绿茶1991阅读 656评论 0 2
  • 1、Core Animation Core Animation,即为核心动画,它是一组非常强大的动画处理API,使...
    Geniusn阅读 487评论 0 1