您的位置:首页 > 其它

核心动画

2015-09-09 13:08 281 查看

//1 . CABasicAnimation
#pragma mark - 旋转动画
- (void)rotate {

// 1. 创建动画对象
CABasicAnimation *anim = [CABasicAnimation animation];

// 2. 设置动画属性
// keyPath 决定了执行怎么样的动画,调整那个属性来执行动画
// 让图层动起来的本质是改动画的属性
anim.keyPath = @"transform";
// 目标点
anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 1, 1, 0)];
anim.duration = 2.0f;

// 核心动画的缺点,反弹  两个属性必须连用,让图层保持动画执行完毕后的状态
//   1>.动画执行完毕后不要删除动画
anim.removedOnCompletion = NO;
//   2>. 保持最新的状态
anim.fillMode = kCAFillModeForwards;

// 3. 添加动画到图层
[self.layer addAnimation:anim forKey:nil];

}

#pragma mark - 平移动画
- (void)translate {

// 1. 创建动画对象
CABasicAnimation *anim = [CABasicAnimation animation];

// 2. 设置动画属性
// keyPath 决定了执行怎么样的动画,调整那个属性来执行动画
// 让图层动起来的本质是改动画的属性
anim.keyPath = @"position";
//    @property(strong) id fromValue, toValue, byValue;
// 起始
anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
// 目标点 - 达到什么点  byValue 增加多少
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)];
anim.duration = 2.0f;

// 核心动画的缺点,反弹  两个属性必须连用,让图层保持动画执行完毕后的状态
//   1>.动画执行完毕后不要删除动画
anim.removedOnCompletion = NO;
//   2>. 保持最新的状态
anim.fillMode = kCAFillModeForwards;

// 3. 添加动画到图层
[self.layer addAnimation:anim forKey:nil];
}

#pragma mark - 缩放动画
- (void)scale {

// 1. 创建动画对象
CABasicAnimation *anim = [CABasicAnimation animation];

// 2. 设置动画属性
// keyPath 决定了执行怎么样的动画,调整那个属性来执行动画
// 让图层动起来的本质是改动画的属性
anim.keyPath = @"bounds";
//    @property(strong) id fromValue, toValue, byValue;

// 目标点
anim.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
anim.duration = 2.0f;

// 核心动画的缺点,反弹  两个属性必须连用,让图层保持动画执行完毕后的状态
//   1>.动画执行完毕后不要删除动画
anim.removedOnCompletion = NO;
//   2>. 保持最新的状态
anim.fillMode = kCAFillModeForwards;

// 3. 添加动画到图层
[self.layer addAnimation:anim forKey:nil];
}

CAKeyFrameAnimation
//@interface CAKeyframeAnimation : CAPropertyAnimation

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

// 获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();

CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];

anim.keyPath = @"position";
// 禁止反弹
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;

// create开头的需要释放
CGMutablePathRef path = CGPathCreateMutable();
// 将路径添加到上下文
CGContextAddPath(ctx, path);

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

// 路径
anim.path = path;
// 节奏
anim.timingFunction = [CAMediaTimingFunction functionWithName:@"linear"];
CGPathRelease(path);

CGContextSetLineWidth(ctx, 2);
// 渲染‘’‘’‘’‘’‘’‘
[[UIColor blueColor] setStroke];
CGContextStrokePath(ctx);

anim.delegate = self;
[self.RedView.layer addAnimation:anim forKey:nil];
}

#pragma mark - animation  代理方法
#pragma mark 动画开始的时候调用
- (void)animationDidStart:(CAAnimation *)anim {

NSLog(@"%s", __FUNCTION__);
}

#pragma mark 动画开始的时候调用
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {

NSLog(@"%s", __FUNCTION__);
}

- (void)touchMove {

// basic fromValue - toValue  两个值之间切换

// key frame 多个值之间切换

/**
*
1.CAKeyframeAnimation
2.CABasicAnimation

继承自 - > CAPropertyAnimation
*/

CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];

anim.keyPath = @"position";

NSValue *value1 = [NSValue valueWithCGPoint:CGPointZero];
NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(100, 0)];
NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(200, 100)];
NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];

anim.values = @[value1,value2,value3,value4];
anim.keyTimes = @[@(0.2),@(0.2),@(0.3),@(0.3)];
anim.duration = 2.0f;

// 禁止反弹
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;

// 添加动画到图层
[self.RedView.layer addAnimation:anim forKey:nil];
}

图标抖动 简单实现
- (IBAction)startAction {
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"transform.rotation";

// 抖动角度
anim.values = @[@(Angle2Radian(-5)),@(Angle2Radian(5)),@(Angle2Radian(-5))];

anim.duration = 2.0f;
anim.repeatCount = 10;
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;

[self.imageView.layer addAnimation:anim forKey:@"shake"]; // 动画的标记
}

- (IBAction)stopAction {

// 通过key  移除指定的动画
[self.imageView.layer removeAnimationForKey:@"shake"];
}

CATransition转场动画 (过渡动画)
- (IBAction)formPic:(id)sender {

self.currentIndex--;

if (self.currentIndex == -1) {
self.currentIndex = 8;
}
NSString *fileName = [NSString stringWithFormat:@"%d.jpg",self.currentIndex+1];
self.iconImageView.image = [UIImage imageNamed:fileName];

// 转场动画
CATransition *anim = [CATransition animation];

// 属性
anim.type = @"cube";
anim.subtype = kCATransitionFromLeft;
anim.duration = 0.5f;

// 执行
[self.iconImageView.layer addAnimation:anim forKey:nil];
}

- (IBAction)nextPic:(id)sender {

self.currentIndex++;

if (self.currentIndex == 9) {
self.currentIndex = 0;
}
NSString *fileName = [NSString stringWithFormat:@"%d.jpg",self.currentIndex+1];
self.iconImageView.image = [UIImage imageNamed:fileName];

// 转场动画
CATransition *anim = [CATransition animation];

// 属性
anim.type = @"rippleEffect";
anim.subtype = kCATransitionFromRight;
anim.duration = 0.5f;

// 执行
[self.iconImageView.layer addAnimation:anim forKey:nil];

/*
 过渡效果 fade     //交叉淡化过渡(不支持过渡方向) kCATransitionFade push     //新视图把旧视图推出去  kCATransitionPush
 moveIn   //新视图移到旧视图上面   kCATransitionMoveIn reveal   //将旧视图移开,显示下面的新视图  kCATransitionReveal cube     //立方体翻滚效果 oglFlip  //上下左右翻转效果 suckEffect   //收缩效果,如一块布被抽走(不支持过渡方向) rippleEffect //滴水效果(不支持过渡方向) pageCurl     //向上翻页效果 pageUnCurl   //向下翻页效果 cameraIrisHollowOpen  //相机镜头打开效果(不支持过渡方向) cameraIrisHollowClose //相机镜头关上效果(不支持过渡方向)*//* 过渡方向 kCATransitionFromRight kCATransitionFromLeft kCATransitionFromBottom
 kCATransitionFromTop

CATransition的使用
CATransition *anim = [CATransition animation];
anim.type = @“cube”; // 动画过渡类型
anim.subtype = kCATransitionFromTop; // 动画过渡方向
anim.duration = 1; // 动画持续1s
// 代理,动画执行完毕后会调用delegate的animationDidStop:finished:
anim.delegate = self;*/

/*******中间穿插改变layer属性的代码**********/

}

CAAnimationGroup#pragma mark -  ' 动画组的简单使用 '
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

CAAnimationGroup *animGroup = [CAAnimationGroup animation];

// 1. 创建旋转动画对象
CABasicAnimation *rotate = [CABasicAnimation animation];

rotate.keyPath = @"transform.rotation";
rotate.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 1, 1, 0)];

// 2. 创建缩放动画对象
CABasicAnimation *scale = [CABasicAnimation animation];

scale.keyPath = @"transform.scale";
scale.toValue = @(0.0);

// 3. 创建平移动画对象
CABasicAnimation *translate = [CABasicAnimation animation];

translate.keyPath = @"transform.translation";
translate.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];

// 控制权交给组
animGroup.duration = 2.0f;
animGroup.removedOnCompletion = NO;
animGroup.fillMode = kCAFillModeForwards;
// 一起执行
animGroup.animations = @[scale,rotate];

[self.animView.layer addAnimation:animGroup forKey:nil];

}

UIView的封装动画
#pragma mark -  ' 动画组的简单使用 '
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

CAAnimationGroup *animGroup = [CAAnimationGroup animation];

// 1. 创建旋转动画对象
CABasicAnimation *rotate = [CABasicAnimation animation];

rotate.keyPath = @"transform.rotation";
rotate.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 1, 1, 0)];

// 2. 创建缩放动画对象
CABasicAnimation *scale = [CABasicAnimation animation];

scale.keyPath = @"transform.scale";
scale.toValue = @(0.0);

// 3. 创建平移动画对象
CABasicAnimation *translate = [CABasicAnimation animation];

translate.keyPath = @"transform.translation";
translate.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];

// 控制权交给组
animGroup.duration = 2.0f;
animGroup.removedOnCompletion = NO;
animGroup.fillMode = kCAFillModeForwards;
// 一起执行
animGroup.animations = @[scale,rotate];

ad39

[self.animView.layer addAnimation:animGroup forKey:nil];

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