CABasicAnimation的区别是:CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CAKeyframeAnimation会使用一个NSArray保存这些数值
属性解析:
values:就是上述的NSArray对象。里面的元素称为”关键帧”(keyframe)。动画对象会在指定的时间(duration)内,依次显示values数组中的每一个关键帧
path:可以设置一个CGPathRef\CGMutablePathRef,让层跟着路径移动。path只对CALayer的anchorPoint和position起作用。如果你设置了path,那么values将被忽略
keyTimes:可以为对应的关键帧指定对应的时间点,其取值范围为0到1.0,keyTimes中的每一个时间值都对应values中的每一帧.当keyTimes没有设置的时候,各个关键帧的时间是平分的
说明:CABasicAnimation可看做是最多只有2个关键帧的CAKeyframeAnimation
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UIButton *btn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.btn=[UIButton buttonWithType:UIButtonTypeSystem];
self.btn.frame=CGRectMake(100, 100, 50, 40);
[self.btn setTitle:@"按钮" forState:UIControlStateNormal];
[self.btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.btn];
}
-(void)btnClick:(id)sender
{
//关键帧动画有两种实现方式 一种是通过设置values的值来改变 另一种是通过设置path属性值来改变
//values属性
CAKeyframeAnimation *keyframeAnimation=[CAKeyframeAnimation animation];
keyframeAnimation.keyPath=@"position";
NSValue *value1=[NSValue valueWithCGPoint:CGPointMake(100, 100)];
NSValue *value2=[NSValue valueWithCGPoint:CGPointMake(200, 100)];
NSValue *value3=[NSValue valueWithCGPoint:CGPointMake(200, 200)];
NSValue *value4=[NSValue valueWithCGPoint:CGPointMake(100, 200)];
NSValue *value5=[NSValue valueWithCGPoint:CGPointMake(100, 100)];
keyframeAnimation.values=@[value1,value2,value3,value4,value5];
//values和keyTimes的个数是一样的 值在0-1之间 逐渐增加直到为1
keyframeAnimation.keyTimes=@[[NSNumber numberWithFloat:0.1],[NSNumber numberWithFloat:0.3],[NSNumber numberWithFloat:0.5],[NSNumber numberWithFloat:0.75],[NSNumber numberWithFloat:1]];
keyframeAnimation.removedOnCompletion=NO;
keyframeAnimation.fillMode=kCAFillModeForwards;
keyframeAnimation.duration=4.0;
keyframeAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
keyframeAnimation.delegate=self;
//设置动画的key 这样可以移除动画 [self.btn.layer removeAnimationForKey:<#(NSString *)#>]
[self.btn.layer addAnimation:keyframeAnimation forKey:@"cuiyw"];
//path属性
// CAKeyframeAnimation *keyframeAnimation=[CAKeyframeAnimation animation];
// keyframeAnimation.keyPath=@"position";
// CGMutablePathRef path=CGPathCreateMutable();
// CGPathAddEllipseInRect(path, NULL, CGRectMake(100, 100, 200, 200));
// keyframeAnimation.path=path;
// keyframeAnimation.duration=5.0;
// //ease 缓解
//// kCAMediaTimingFunctionLinear 线性(匀速)|
//// kCAMediaTimingFunctionEaseIn 先慢|
//// kCAMediaTimingFunctionEaseOut 后慢|
//// kCAMediaTimingFunctionEaseInEaseOut 先慢 后慢 中间快|
//// kCAMediaTimingFunctionDefault 默认
// keyframeAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
// keyframeAnimation.removedOnCompletion=NO;
// keyframeAnimation.fillMode=kCAFillModeForwards;
//
// [self.btn.layer addAnimation:keyframeAnimation forKey:nil];
//
}
- (void)animationDidStart:(CAAnimation *)anim
{
NSLog(@"开始动画");
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
NSLog(@"结束动画");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
时间: 2024-10-28 09:57:18