CABasicAnimation:基本动画,是CAPropertyAnimation的子类,可以创建对象实例化
@interface CABasicAnimation : CAPropertyAnimation
@end
通过+ (instancetype)animationWithKeyPath:(nullable NSString *)path;方法进行实例化
+ (instancetype)animationWithKeyPath:(nullable NSString *)path;
CABasicAnimation 属性
/**
如果设置动画时间为4s,speed设置为2,则动画只需2s即可执行完。
如果同时设置了动画的speed和layer 的speed,则实际的speed为两者相乘。
将speed 属性赋值为0,表示暂停动画
*/
@property float speed;
/**
动画执行开始到结束的时间间隔
*/
@property CFTimeInterval duration;
/**
动画开始时间 执行延迟操作的话 basicAnimation.beginTime = CACurrentMediaTime()+秒数;
*/
@property CFTimeInterval beginTime;
/**
时间偏移 提前/推迟执行动画
*/
@property CFTimeInterval timeOffset;
/**
设置动画的次数。执行有限次数后停止执行。
*/
@property float repeatCount;
/**
设置动画的时间。在该时间内动画一直执行,不计次数。
*/
@property CFTimeInterval repeatDuration;
/**
动画结束时是否执行逆动画
*/
@property BOOL autoreverses;
/**
动画在开始和结束时的动作 默认值是 kCAFillModeRemoved
这个属性一般情况下使用默认的就行
*/
@property(copy) NSString *fillMode;
fillModel有下面几个结果
// 设置为该值,layer的状态将保持在动画的最后一帧,要想保持layer原状,应将removedOnCompletion设置为NO
CA_EXTERN NSString * const kCAFillModeForwards
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
//立即执行动画的第一帧
CA_EXTERN NSString * const kCAFillModeBackwards
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
// kCAFillModeForwards/kCAFillModeBackwards组合CA_EXTERN NSString * const kCAFillModeBoth
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
//设置为该值,动画将在设置的 beginTime 开始执行(如没有设置beginTime属性,则动画立即执行),动画执行完成后将会layer的改变恢复原状
CA_EXTERN NSString * const kCAFillModeRemoved
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
/**
动画结束之后移除
*/
@property(getter=isRemovedOnCompletion) BOOL removedOnCompletion;
CABasicAnimation 实例
下面开看几个基础动画的实例
------位移
-(void)test1{
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, SCREEN_HEIGHT/2)];
basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH - 50, SCREEN_HEIGHT/2)];
basicAnimation.duration = 1;
[self.testView.layer addAnimation:basicAnimation forKey:@"positionAnimation"];
}
------透明度变化
-(void)test2{
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
basicAnimation.fromValue = [NSNumber numberWithFloat:1.0f];
basicAnimation.toValue = [NSNumber numberWithFloat:.5f];
basicAnimation.duration = 1.f;
[self.testView.layer addAnimation:basicAnimation forKey:@"opacity"];
}
------缩放
-(void)test3{
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
basicAnimation.toValue = [NSNumber numberWithFloat:2.0f];
basicAnimation.duration = 1.0f;
[self.testView.layer addAnimation:basicAnimation forKey:@"scaleAnimation"];
}
------旋转
-(void)test4{
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
basicAnimation.toValue = [NSNumber numberWithFloat:M_PI];
basicAnimation.duration = 1.0f;
[self.testView.layer addAnimation:basicAnimation forKey:@"rotationAnimation"];
}
------颜色改变
-(void)test5{
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
basicAnimation.toValue = (id)[UIColor grayColor].CGColor;
basicAnimation.duration = 1.0f;
basicAnimation.beginTime = CACurrentMediaTime()+3;
[self.testView.layer addAnimation:basicAnimation forKey:@"bacogroundAnimation"];
}
网友评论