一.CAAnimation
1.继承关系
CAAnimation继承关系
- CABasicAnimation基础动画 fromValue toValue byValue
- CAKeyframeAnimation关键帧动画 可定制化高
- CAAnimationGroup组动画
初始化
/* Creates a new animation object with its `keyPath' property set to
* 'path'. */
+ (instancetype)animationWithKeyPath:(nullableNSString*)path;
2.CALayer
@interfaceUIView:UIResponder<NSCoding,UIAppearance,UIAppearanceContainer,UIDynamicItem,UITraitEnvironment,UICoordinateSpace,UIFocusItem,CALayerDelegate>
@interfaceCALayer :NSObject<NSCoding,CAMediaTiming>
@protocol CAMediaTiming
duration动画时长
repeatCount重复的次数
repeatDuration动画时间
beginTime动画开始时间
timeingFunction速度变化
autoreverses 是否执行逆动画
/** Timing function names. **/
CA_EXTERNNSString*const kCAMediaTimingFunctionLinear
CA_EXTERNNSString*const kCAMediaTimingFunctionEaseIn
CA_EXTERNNSString*const kCAMediaTimingFunctionEaseOut
CA_EXTERNNSString*const kCAMediaTimingFunctionEaseInEaseOut
CA_EXTERNNSString*const kCAMediaTimingFunctionDefault
防止动画结束后回到初始状态
transformAnima.removedOnCompletion = NO;
transformAnima.fillMode = kCAFillModeForwards;
CALayer动画其实移动的是CALayer的一个副本,所以移动的时候操作是不行的
fillMode属性的理解该属性定义了你的动画在开始和结束时的动作。默认值是 kCAFillModeRemoved。
添加动画的视图是局部变量时,可使用该方法添加动画给动画设置key-value对
[positionAnimasetValue:@"PositionAnima"forKey:@"AnimationKey"];
[_myView.layeraddAnimation:groupforKey:@"group"];//path是为了在代理中区分不同动画
动画效果
3.demo例子
//围绕y轴旋转
CABasicAnimation*transformAnima = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.y"];
transformAnima.fromValue=@(M_PI_2);
transformAnima.toValue=@(M_PI);
transformAnima.duration=5.0;
transformAnima.timingFunction= [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transformAnima.autoreverses=YES;
transformAnima.repeatCount=1;
transformAnima.beginTime=CACurrentMediaTime();
CAAnimationGroup*group = [CAAnimationGroupanimation];
group.animations=@[transformAnima,transformAnima1];
group.duration=5;
[transformAnima1setValue:@"transform.rotation.y"forKey:@"AnimationKey"];
[_myView.layeraddAnimation:groupforKey:@"group"];//path是为了在代理中区分不同动画
二.UIBezierPath
很简单
demo画三角形
- (void)drawRect:(CGRect)rect {
// Drawing code
//贝塞尔曲线
UIBezierPath*path = [UIBezierPathbezierPath];
[pathmoveToPoint:CGPointMake(100,100)];
[pathaddLineToPoint:CGPointMake(200,150)];
[pathaddLineToPoint:CGPointMake(100,200)];
//[path addLineToPoint:CGPointMake(100, 100)];
[pathclosePath];
path.lineWidth=2;
// 设置填充颜色
UIColor*fillColor = [UIColorgreenColor];
[fillColorset]; [pathfill];
// 设置画笔颜色
UIColor*strokeColor = [UIColorblueColor];
[strokeColorset];
// 根据我们设置的各个点连线
[pathstroke];
UIColor*color = [UIColorcolorWithRed:0green:0blue:0.7alpha:1];
[colorset];//设置线条颜色
}
或者与CAShapeLayer一起用
- (void)drawBezier:(UIBezierPath*)path
{
CAShapeLayer*shapeLayer = [CAShapeLayerlayer];
shapeLayer.fillColor= [UIColorblackColor].CGColor;
shapeLayer.strokeColor= [UIColorgreenColor].CGColor;
shapeLayer.lineWidth=3;
shapeLayer.path= path.CGPath;
[_myView.layeraddSublayer:shapeLayer];
}
http://www.jianshu.com/p/79a0b3c7086c
https://github.com/iosyaowei/exampleForAnimation
网友评论