您的位置:首页 > 移动开发 > IOS开发

iOS抽奖转盘动画之CAKeyFrame关键帧动画

2015-10-10 18:04 363 查看
上面的代码保持不变,依然是:

- (void)viewDidLoad {

[super
viewDidLoad];

//创建一个图层

CALayer *layer = [CALayer
layer];

//设置layer的bounds
以自己为中心的坐标x,y都是00

layer.bounds =
CGRectMake(0,
0, 100, 100);

//相对superlayer的位置

layer.position =
CGPointMake(100,
100);

// layer.anchorPoint = CGPointMake(0, 0);
锚点和position是不同坐标系下的同一个位置

layer.backgroundColor = [UIColor
yellowColor].CGColor;

//添加到视图的图层上

[self.view.layer
addSublayer:layer];

//赋值给自己的layer属性

self.layer = layer;

}

//通过触摸的方式演示动画

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

// [self animationScale];
基础动画

[self
keyAnimation];//关键帧动画

}

下面是实现的关键帧动画函数

//关键帧动画

- (void)keyAnimation{

//1.创建动画对象并设置
让图层沿着圆形运动

CAKeyframeAnimation *anim = [CAKeyframeAnimation
animationWithKeyPath:@"position"];

anim.removedOnCompletion =
NO;//动画结束后不移除

anim.fillMode =
kCAFillModeForwards;//动画状态保持为最后的状态
也就是说状态保持不变

anim.duration =
3.0f;//动画时间3秒

// anim.repeatCount = 2;//动画重复次数2次

// anim.repeatDuration = 2.0f;//重复时间间隔2秒

//创建一个可变路径

CGMutablePathRef path =
CGPathCreateMutable();

//第二个参数为NULL
不进行变换

CGPathAddEllipseInRect(path,
NULL,
CGRectMake(100,
100,
200, 200));

anim.path = path;

//设置动画执行的节奏

anim.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

[self.layer
addAnimation:anim forKey:nil];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: