iOS知识点总结(6)- 动画

CABasicAnimation——基本动画

简单的动画效果:

CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];

moveAnimation.duration = 0.8;//动画时间

//动画起始值和终止值的设置

moveAnimation.fromValue = @(self.imageView.center.x);

moveAnimation.toValue = @(self.imageView.center.x-30);

//一个时间函数,表示它以怎么样的时间运行

moveAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

moveAnimation.repeatCount = HUGE_VALF;

moveAnimation.repeatDuration = 2;

moveAnimation.removedOnCompletion = NO;

moveAnimation.fillMode = kCAFillModeForwards;

//添加动画,后面有可以拿到这个动画的标识

[self.imageView.layer addAnimation:moveAnimationforKey:@"可以拿到这个动画的Key值"];

keyPath :要改变的属性名称(传字符串)

fromValue:keyPath相应属性的初始值

toValue:keyPath相应属性的结束值

CAKeyframeAnimation——关键帧动画

CABasicAnimation:只能从一个数值(fromValue)变到另一个数值(toValue)

CAKeyframeAnimation:会使用一个NSArray保存这些数值

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

NSArray *rotationVelues = @[@(M_PI_4), @(-M_PI_4), @(M_PI_4)]; 

animaiton.values = rotationVelues; 

animation.rotationMode = kCAAnimationRotateAuto;  //方向

animaiton.duration = 3.0f; 

animation.keyTimes = @[@0.2 ,@0.8 ,@1];

animation.path = bezierPath.CGPath;

animaiton.repeatCount = HUGE_VALF;    //  #define    HUGE_VALF    1e50f 

[self.imageView.layer addAnimation:animaiton forKey:nil]; 

UIBezierPath - 贝赛尔曲线

//创建路径

UIBezierPath *bezierPath = [[UIBezierPath alloc] init];

[bezierPath moveToPoint:CGPointMake(0, 450)];

[bezierPath addCurveToPoint:CGPointMake(370, 500) controlPoint1:CGPointMake(350, 200) controlPoint2:CGPointMake(300, 600)]; //一个曲线   

//路径样式

CAShapeLayer *shapeLayer = [CAShapeLayer layer];

shapeLayer.path = bezierPath.CGPath;

shapeLayer.fillColor = [UIColor clearColor].CGColor; //填充色<默认黑色>

shapeLayer.strokeColor = [UIColor blueColor].CGColor; //线色

shapeLayer.lineWidth = 2;

[self.view.layer addSublayer:shapeLayer];

转场动画——CATransition

CATransition *caTransition = [CATransition animation];

caTransition.duration = 0.5;

caTransition.delegate = self;

caTransition.timingFunction = [CAMediaTimingFunction functionWithName:@"easeInEaseOut"];//切换时间函数

caTransition.type = kCATransitionReveal;//动画切换风格

caTransition.subtype = kCATransitionFromLeft;//动画切换方向

//    子视图交换位置

//[self.parentView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];

//动画在父视图

[self.parentView.layer addAnimation:caTransition forKey:@"Key"];

UIView中目前最常用的动画方法应该就是这个方法了

UIView中目前最常用的动画方法应该就是这个方法了

+(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ nullable)(BOOL finished))completion ;

你可能感兴趣的:(iOS知识点总结(6)- 动画)