实现动画,两部分可以实现:UIView 和Layer层
知识点1 :UIView 相关动画
用法1:
[UIView animateWithDuration:1 animations:^{ //这里的 “ 1” 是表示执行时间为1秒
//动画对象的一些属性发生变化
//这里写对象变化的属性
例如:
变化位置 :self.myView.frame = CGRectMake(0,200, 375,200);
变化颜色 :self.myView.backgroundColor = [UIColor greenColor];
}];
用法2:
[UIView animateWithDuration:2 animations:^{ //这里的 “ 2” 是表示执行时间为1秒
//先执行这里面的语句
self.myView.frame = CGRectMake(0,200, 375,200);
self.myView.backgroundColor = [UIColor greenColor];
} completion:^(BOOL finished) {
//等上面执行完动画后,再执行这里的语句
self.myView.frame = CGRectMake(0,50, 375,200);
self.myView.backgroundColor = [UIColor orangeColor];
}];
用法3:
//optios: 枚举,设置动画效果,多个值之间用 | 隔开
[UIViewanimateWithDuration:8delay:3 options:UIViewAnimationOptionAutoreverse |UIViewAnimationOptionRepeat
animations:^{ // 这里的参数“8”表示执行一次动画需要多少时间 后面那个参数“3”表示3秒后执行动画
self.myView.frame =CGRectMake(137,100, 100,100);
} completion:^(BOOL finished) {
}];
用法4:
//翻转效果
[UIView transitionWithView:self.myView duration:2 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{
self.myView.frame = CGRectMake(0, 100, 135, 80);
} completion:^(BOOL finished) {
self.myView.backgroundColor = [UIColor purpleColor];
}];
用法5:
//创建一个新View
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
//从一个View过度到另一个View
[UIView transitionFromView:self.myView toView:view1 duration:2 options:UIViewAnimationOptionTransitionFlipFromTop completion:^(BOOL finished) {
view1.backgroundColor = [UIColor blackColor];
}];
用法6:
[UIView beginAnimations:@”ani” context:nil];
//动画效果的相关设置
[UIView setAnimationDuration:3]; //动画一次的时间
[UIView setAnimationRepeatCount:5]; //动画执行次数
[UIView setAnimationRepeatAutoreverses:YES];
//指定View的相关属性
self.myView.frame = CGRectMake(0, 100, 100, 100);
//提交动画效果
[UIView commitAnimations];
知识点2 : layer层动画实现
用法1:
//将CAAnimation多个对象放入一个数组中,layer执行数组中的动画
//创建CAAnimation对象
CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
animation1.fromValue = [NSNumber numberWithInt:1];
animation1.toValue = [NSNumber numberWithFloat:1.5];
//旋转
CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; //旋转
animation2.fromValue = [NSNumber numberWithInt:0];
animation2.toValue = [NSNumber numberWithInt:5];
//移动
CABasicAnimation *animation3 = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; //移动
animation3.fromValue = [NSNumber numberWithInt:0];
animation3.toValue = [NSNumber numberWithInt:100];
CAAnimationGroup *group = [CAAnimationGroup animation];
//数组中添加CAAnimation对象
group.animations = @[animation3,animation1,animation2];
//动画的设置以group的设置为准
group.duration =3;
[self.myView.layer addAnimation:group forKey:@"cc"];
用法2:
//过度效果
CATransition *transtion = [CATransition animation];
//设置过度类型
transtion.type = @"cube";
transtion.duration = 2;
transtion.autoreverses =YES;
transtion.repeatCount =5;
//layer层添加动画
[self.myView.layer addAnimation:transtion forKey:@"ww"];
用法3:
CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@”position”]; //位置属性
//创建一个CGPath 结构体
CGMutablePathRef path = CGPathCreateMutable();
//设置初始位置
CGPathMoveToPoint(path, NULL, self.redView.center.x, self.redView.center.y);
//path添加经过的点坐标
CGPathAddLineToPoint(path, NULL, 100, 350);
CGPathAddLineToPoint(path, NULL, 300, 350);
CGPathAddLineToPoint(path, NULL, 75, 225);
//贝塞尔曲线
CGPathAddCurveToPoint(path, NULL,self.redView.center.x, self.redView.center.y, 100, 350, 300, 350);
//[NSThread sleepForTimeInterval:3];
self.view.backgroundColor = [UIColor orangeColor];
keyFrame.path = path;
keyFrame.duration = 3;
//keyFrame.repeatCount = NSIntegerMax;
keyFrame.repeatCount = 1;
[self.redView.layer addAnimation:keyFrame forKey:@"rrr"];