UIView属性动画 可动画的属性:frame center bounds alpha backgroundColor tranform
//
修改属性做动画
,
动画结束后属性修改的结果是真实地作用到视图上
,
不能恢复到以前的样子
ios4.0之后使用block块的形式
1.第一种形式
__block typeof(self) welkSelf = self;
[UIView animateWithDuration:3 animations:^{
//1.修改中心点
CGPoint center = welkSelf.aninationView.center;
center.y += 50;
welkSelf.aninationView.center = center;
welkSelf.aninationView.alpha = 0.0;//透明度
welkSelf.aninationView.transform = CGAffineTransformRotate(welkSelf.aninationView.transform, M_PI_4);//变形
}];
[UIView animateWithDuration:3 animations:^{
//1.修改中心点
CGPoint center = welkSelf.aninationView.center;
center.y += 50;
welkSelf.aninationView.center = center;
welkSelf.aninationView.alpha = 0.0;//透明度
welkSelf.aninationView.transform = CGAffineTransformRotate(welkSelf.aninationView.transform, M_PI_4);//变形
}];
2.第二种形式
_block typeof(self) welkSelf = self;
[UIView animateWithDuration:2 animations:^{
CGPoint center = welkSelf.aninationView.center;
center.y += 50;
welkSelf.aninationView.center = center;
welkSelf.aninationView.transform = CGAffineTransformScale(welkSelf.aninationView.transform, 0.5, 0.5);
} completion:^(BOOL finished) {
[welkSelf makeAnimationBack];
}];
[UIView animateWithDuration:2 animations:^{
CGPoint center = welkSelf.aninationView.center;
center.y += 50;
welkSelf.aninationView.center = center;
welkSelf.aninationView.transform = CGAffineTransformScale(welkSelf.aninationView.transform, 0.5, 0.5);
} completion:^(BOOL finished) {
[welkSelf makeAnimationBack];
}];
3.第三种形式
//第三种形式 01持续时间 02动画执行的延迟时间 03设置动画的特效 04修改的动画属性 05动画执行结束的block
__block typeof(self) welkSelf = self;
[UIView animateWithDuration:2 delay:2 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
CGPoint center = welkSelf.aninationView.center;
center.y += 50;
welkSelf.aninationView.center = center;
welkSelf.aninationView.transform = CGAffineTransformScale(welkSelf.aninationView.transform, 0.5, 0.5);
} completion:^(BOOL finished) {
[welkSelf makeAnimationBack];
__block typeof(self) welkSelf = self;
[UIView animateWithDuration:2 delay:2 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
CGPoint center = welkSelf.aninationView.center;
center.y += 50;
welkSelf.aninationView.center = center;
welkSelf.aninationView.transform = CGAffineTransformScale(welkSelf.aninationView.transform, 0.5, 0.5);
} completion:^(BOOL finished) {
[welkSelf makeAnimationBack];
}];
4.第四种形式
第四种形式 01动画的持续的时间 02延迟的时间 03设置弹簧的强度(0.0 ~1.0) 04设置弹簧的速度 05 动画效果 06 改变动画属性写在这里 07 结束动画的时候调用
__block typeof(self) welkSelf = self;
[UIView animateWithDuration:2 delay:1 usingSpringWithDamping:0.5 initialSpringVelocity:1000 options:UIViewAnimationOptionCurveEaseIn animations:^{
CGPoint center = welkSelf.bounces.center;
center.y += 10;
welkSelf.bounces.center = center;
welkSelf.bounces.transform = CGAffineTransformScale(welkSelf.bounces.transform, 1.5, 1.5);
} completion:^(BOOL finished) {
CGPoint center = welkSelf.bounces.center;
center.y-=10;
welkSelf.bounces.center = center;
welkSelf.bounces.transform = CGAffineTransformIdentity;
}];
__block typeof(self) welkSelf = self;
[UIView animateWithDuration:2 delay:1 usingSpringWithDamping:0.5 initialSpringVelocity:1000 options:UIViewAnimationOptionCurveEaseIn animations:^{
CGPoint center = welkSelf.bounces.center;
center.y += 10;
welkSelf.bounces.center = center;
welkSelf.bounces.transform = CGAffineTransformScale(welkSelf.bounces.transform, 1.5, 1.5);
} completion:^(BOOL finished) {
CGPoint center = welkSelf.bounces.center;
center.y-=10;
welkSelf.bounces.center = center;
welkSelf.bounces.transform = CGAffineTransformIdentity;
}];
CAlayer 动画
//UIView的过渡动画
- (void)handleTrasitionAnimation{
__block typeof(self) weakSelf = self;
//01对那个视图添加过渡动画 02动画时长
[UIView transitionWithView:self.aninationView duration:1 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
weakSelf.aninationView.transform = CGAffineTransformRotate(self.aninationView.transform, M_PI);
} completion:nil];
}
- (void)handleTrasitionAnimation{
__block typeof(self) weakSelf = self;
//01对那个视图添加过渡动画 02动画时长
[UIView transitionWithView:self.aninationView duration:1 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
weakSelf.aninationView.transform = CGAffineTransformRotate(self.aninationView.transform, M_PI);
} completion:nil];
}
//CAlayer动画 修改layer层的属性,并没有真实做用到这个视图上 动画只是一中假象
- (void)handleLayer{
//Calayer动画就是对layer层做动画
self.aninationView.layer.borderWidth = 10.0;
self.aninationView.layer.borderColor = [UIColor greenColor].CGColor;
self.aninationView.layer.cornerRadius = 100;
self.aninationView.clipsToBounds = YES;//这种也可以切除Layer多余的部分
// self.aninationView.layer.masksToBounds = YES;//这种也可以切除Layer多余的
self.aninationView.layer.contents = (id)[UIImage imageNamed:@"6.jpg"].CGImage;
//锚点anchorPoint 决定layer层上的那个点是position点 锚点默认是:(0.5,0.5)根视图的中心点重合
//视图以创建出来的时候锚点基本点中心点是重合的
self.aninationView.layer.anchorPoint = CGPointMake(0.0, 1.0);
// frame.origin.x = position.x - anchorPoint.x * bounds.size.width;
// frame.origin.y = position.y - anchorPoint.y * bounds.size.height;
//基准点position决定当前视图的layer在父视图上的位置,它以父视图的坐标系为准
self.aninationView.transform= CGAffineTransformRotate(self.aninationView.transform, M_PI_4);
self.aninationView.layer.position = CGPointMake(60, 384);
}
- (void)handleLayer{
//Calayer动画就是对layer层做动画
self.aninationView.layer.borderWidth = 10.0;
self.aninationView.layer.borderColor = [UIColor greenColor].CGColor;
self.aninationView.layer.cornerRadius = 100;
self.aninationView.clipsToBounds = YES;//这种也可以切除Layer多余的部分
// self.aninationView.layer.masksToBounds = YES;//这种也可以切除Layer多余的
self.aninationView.layer.contents = (id)[UIImage imageNamed:@"6.jpg"].CGImage;
//锚点anchorPoint 决定layer层上的那个点是position点 锚点默认是:(0.5,0.5)根视图的中心点重合
//视图以创建出来的时候锚点基本点中心点是重合的
self.aninationView.layer.anchorPoint = CGPointMake(0.0, 1.0);
// frame.origin.x = position.x - anchorPoint.x * bounds.size.width;
// frame.origin.y = position.y - anchorPoint.y * bounds.size.height;
//基准点position决定当前视图的layer在父视图上的位置,它以父视图的坐标系为准
self.aninationView.transform= CGAffineTransformRotate(self.aninationView.transform, M_PI_4);
self.aninationView.layer.position = CGPointMake(60, 384);
}
//CAlayer的动画基类CAAanimation
//CABasicAnimation
-(void)handleBasecAnimation
{
//CA动画根据KVC的原理,就修改layer的属性,已达到做动画的效果
CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"position.x"];
basic.fromValue = @(-80);
basic.toValue = @400;
//设置动画持续的时间
basic.duration = 3.0;
//设置动画重复的此时
basic.repeatCount = 1000;
[self.cloud.layer addAnimation:basic forKey:nil];
}
//CABasicAnimation
-(void)handleBasecAnimation
{
//CA动画根据KVC的原理,就修改layer的属性,已达到做动画的效果
CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"position.x"];
basic.fromValue = @(-80);
basic.toValue = @400;
//设置动画持续的时间
basic.duration = 3.0;
//设置动画重复的此时
basic.repeatCount = 1000;
[self.cloud.layer addAnimation:basic forKey:nil];
}
//关键贞动画 CAKeyframeAnimation
- (void)handleKeyFrameAnimation{
CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGPoint point1 = self.cloud.center;
CGPoint point2 = CGPointMake(160, 100);
CGPoint point3 = CGPointMake(255, self.cloud.center.y);
NSValue *value1 = [NSValue valueWithCGPoint:point1];
NSValue *value2 = [NSValue valueWithCGPoint:point2];
NSValue *value3 = [NSValue valueWithCGPoint:point3];
//把一组要播放的动画放到数组中,按顺序放到数组中,此时动画执行的效果,就会按数组中数据的顺序发生变换
keyFrame.values = @[value1,value2,value3,value1];
keyFrame.repeatCount = 1000;
keyFrame.duration = 3;
[self.cloud.layer addAnimation:keyFrame forKey:nil];
}
- (void)handleKeyFrameAnimation{
CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGPoint point1 = self.cloud.center;
CGPoint point2 = CGPointMake(160, 100);
CGPoint point3 = CGPointMake(255, self.cloud.center.y);
NSValue *value1 = [NSValue valueWithCGPoint:point1];
NSValue *value2 = [NSValue valueWithCGPoint:point2];
NSValue *value3 = [NSValue valueWithCGPoint:point3];
//把一组要播放的动画放到数组中,按顺序放到数组中,此时动画执行的效果,就会按数组中数据的顺序发生变换
keyFrame.values = @[value1,value2,value3,value1];
keyFrame.repeatCount = 1000;
keyFrame.duration = 3;
[self.cloud.layer addAnimation:keyFrame forKey:nil];
}
//过度动画 CATransaction
- (void)handleLayerTransaction{
//创建过度动画对象
CATransition *transiction = [CATransition animation];
//配置过度动画的样式
transiction.type = @"rippleEffect";
[self.view.layer addAnimation:transiction forKey:nil];//将过度动画添加到layer上
}
- (void)handleLayerTransaction{
//创建过度动画对象
CATransition *transiction = [CATransition animation];
//配置过度动画的样式
transiction.type = @"rippleEffect";
[self.view.layer addAnimation:transiction forKey:nil];//将过度动画添加到layer上
}
//CAAnimationGroup 分组动画
- (void)handleAnimationGroup{
//1.创建第一个关键帧动画,给气球一个运动轨迹
CAKeyframeAnimation *keyFramePath = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//贝塞尔曲线
//1.制定贝塞尔曲线的半径
CGFloat radius = [UIScreen mainScreen].bounds.size.height/2;
//01 圆心 02 半径 03 开始的角度 04 结束的角度 05 旋转方向 YES表示顺时针 NO表示逆时针
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, radius) radius:radius startAngle:-M_PI_2 endAngle:M_PI_2 clockwise:YES];
keyFramePath.path = path.CGPath;//将贝塞尔曲线作为运动轨迹
//2.创建关键帧动画,让气球在运动的时候从小到大在到小的过程
CAKeyframeAnimation *keyFrameScale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
//通过一组数据修改气球的大小
keyFrameScale.values = @[@1.0,@1.2,@1.4,@1.6,@1.8,@1.6,@1.4,@1.2,@1.0];
//创建动画分组对象
CAAnimationGroup *group = [CAAnimationGroup animation];
//将两组动画效果添加到分组动画中
group.duration = 8;
group.repeatCount = 1000;
group.animations = @[keyFramePath,keyFrameScale];
[self.balloon.layer addAnimation:group forKey:nil];
}
- (void)handleAnimationGroup{
//1.创建第一个关键帧动画,给气球一个运动轨迹
CAKeyframeAnimation *keyFramePath = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//贝塞尔曲线
//1.制定贝塞尔曲线的半径
CGFloat radius = [UIScreen mainScreen].bounds.size.height/2;
//01 圆心 02 半径 03 开始的角度 04 结束的角度 05 旋转方向 YES表示顺时针 NO表示逆时针
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, radius) radius:radius startAngle:-M_PI_2 endAngle:M_PI_2 clockwise:YES];
keyFramePath.path = path.CGPath;//将贝塞尔曲线作为运动轨迹
//2.创建关键帧动画,让气球在运动的时候从小到大在到小的过程
CAKeyframeAnimation *keyFrameScale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
//通过一组数据修改气球的大小
keyFrameScale.values = @[@1.0,@1.2,@1.4,@1.6,@1.8,@1.6,@1.4,@1.2,@1.0];
//创建动画分组对象
CAAnimationGroup *group = [CAAnimationGroup animation];
//将两组动画效果添加到分组动画中
group.duration = 8;
group.repeatCount = 1000;
group.animations = @[keyFramePath,keyFrameScale];
[self.balloon.layer addAnimation:group forKey:nil];
}