iOS巅峰之UI视图动画详解

UIView动画
- (void)addSubViews
定义一个imageView和button
// 保存一下中心点
_center  = self.imageView.center;
// 保存一下frame
_frame = self.imageView.frame;

- (void)button
{
// UIView动画  特点:全是类方法调用, 开始与结束之间的部分是动画改变的部分
// 动画大小, 位置, 颜色, 透明度等
// 动画开始
// 参数1:标示符(名字)   参数2:携带的参数
[UIView beginAnimations:@“UIView” context:nil];
// 动画提交
// 设置时间, 在多少秒钟完结动画
[UIView setAnimationDuration:1];
// 设置动画延迟多少秒开始
[UIView setAnimationDelay:2];
// 动画设置反转
[UIView setAnimationRepeatAutoreverses:YES];
// 设置代理
[UIView setAnimationDelegate:self];
// 设置代理方法
[UIView setAnimationWillStartSelector:@selector(WillStart)];
[UIView setAnimationDidStopSelector:@selector(DidStop)];
// 设置循环几次
[UIView setAnimationRepeatCount:1];
// 设置速度曲线
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
// UIView改变位置
self.imageView.center = CGPointMake(200, 500);
// 设置持续执行动画
[UIView setAnimationBeginsFromCurrentState:YES];
// 改变颜色
self.imageView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha : 1];
// 改变透明度
self.imageView.alpha = arc4random() % 256 / 255.0;
// 改变大小
CGRect frame = self.imageView.frame;
frame.size = CGSizeMake(200, 200);
self.imageView.frame = frame;
// 动画提交
[UIImage commitAnimations];
}
#pragma mark ——自己设置的代理方法
- (void)WillStart
{
NSLog(@“动画开始”);
}
- (void)DidStop
{
NSLog(@“动画结束”);
// 复原位置
self.imageView.center = self.center;
self.imageView.frame = self.frame;
// 记录一下形变属性
_transform = self.imageView.transform;
}

- (void)addSubButton
定义一个button

// UIView 动画的block方法
- (void)actionBlockButton
{
// UIView block方法
[UIView animateWithDuration:1 animations:^{
// 执行的动画方法
self.imageView.center = CGPointMake(400, 400);
}];

// block方法2
//---------------------------------------
// 参数3:该block写动画结束后要干的事
[UIView animateWithDuration:1 animations:^{
// 执行动画
// 2D仿射变换, transform形变属性
// 平移
// 参数一:填要形变的View
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 100 , 0);
} completion:^(BOOL finished) {
// 上面动画结束后, 触发(相当于代理方法的完成动画方法)
// 复原位置
// 如果设置了, 反转属性, 那么在动画结束后, 不用另添加动画效果
[UIView animateWithDuration:1 animations:^{
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, -100, 0);
}];

// 用反转不用block实现复原位置
[UIView setAnimationRepeatAutoreverses:YES];
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, -100, 0);

#pragma mark————缩放
[UIView animateWithDuration:1 animations:^{
// 设置反转
[UIView setAnimationRepeatAutoreverses:YES];
// 缩放
// 参数2, 3:缩放的比例
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, 2, 2);
} completion:^(BOOL finished) {
// 复原
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, 0.5, 0.5);
self.imageView.transform = _transform;
}];

#pragma mark — 旋转
// 需求:
// 点击按钮:开始一直转,再点停了
//参数2:旋转的角度
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI);
}completion:^(BOOL finished) {
// 结束时调用循环转
[self rotateAnimation];
}];
// 更改转动状态
_isRunning = !_isRunning;
}

// 循环转
- (void)rotateAnimation
{
if (_isRunning == YES) {
[UIView animateWithDuration:1 animations:^{
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI);
} completion:^(BOOL finished) {
[self rotateAnimation];
}];
}
}


// UIView 切换的动画
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions) options completion:(void(^)(BOOL finished)) completion

// 创建视图
- (void)addSubViews
{
定义一个UIView的对象myView(属性)
// layer是负责显示图层的
//要更改咱们看到的图形形状, 需要更改layer
// 设置圆角
// 先决条件变圆必须是长宽相同
self.myView.layer.cornerRadius = self.myView.frame.size.width / 2;
// 设置阴影的颜色
//CGColorRef 涂层绘制的颜色
self.myView.layer.shadowColor = [UIColor greenColor].CGColor;
// 阴影的显示范围
self.myView.layer.shadowOffset = CGSizeMake(10, 10);
// 阴影的透明度
self.myView.layer.shadowOpacity = 1;
// 阴影的模糊程度
self.myView.layer.shadowRadius = 50;
// 设置边框宽度
self.myView.layer.borderWith = 10;
// 设置边框的颜色
self.myView.layer.borderColor = [UIColor purpleColor].CGColor;
//layer层动画
// CAPropertyAnimation 抽象类
// CABasicAnimation 基础动画, 更改大小, 旋转等
// CAKeyframeAnimation 主要有按轨迹移动, 位置, 比如执行一组动画时使用背景颜色
}
// 实现点击方法
- (void)actionButton:(UIButton *)button
{
[self xyAnimation];
[self sizeAnimation];
[self colorAnimation];
[self positionPoint];
[self douDongImage];
[self transform3D];
[self groupAnimation];
}
// 旋转
- (void)xyAnimation
{
// 新建一个基础动画
// 咱们要更改的是transform.rotation.x 形变属性下弧度的点x轴
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@“transform.rotation.x”];
// 设置属性变化到什么值
// toValue 需要一个对象类型即NSNumber 或NSValue
animation.toValue = [NSNumber numberWithFloat:M_PI * 2];
// 设置动画时间
animation.duration = 1;
// 设置动画重复
animation.repeatCount = 1;
// 把设置添加到layer上
//参数2:添加的动画的标识
[self.myView.layer addAnimation:animation forKey:@“transform.rotation.x”];
}

// 改变size的
- (void)sizeAnimation
{
// 更改大小的话, 需要更改bounds.size
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@“bounds.size”];
// 设置更改的值
// fromValue 初始值
animation.fromValue = [NSValue valueWithCGSize:CGSizeMake(10, 10)];
// 结束值
animation.toValue = [NSValue valueWithCGSize:CGSizeMake(100, 100)];
// 动画时间
animation.duration = 1;
// 添加到layer上
[self.myView.layer addAnimation:animation forKey:@“bounds.size”];
}

// 改变颜色
- (void)colorAnimation
{
// 执行一组动画
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@“backgroundColor”];
// 创建绘制颜色
CGColorRef greed = [UIColor greenColor].CGColor;
CGColorRef red = [UIColor redColor].CGColor;
CGColorRef purp = [UIColor purpleColor].CGColor;
CGColorRef yell = [UIColor yellowColor].CGColor;
// 改变一组颜色
animation.values = @[(id)greed, (id)red, (id)purp, (id)yell];
// 设置一个时间
animation.duration = 5;
// 添加到layer上
[self.myView.layer addAnimation:animation forKey:@“backgroundColor”];
}

// 轨迹移动
- (void)positionPoint
{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@“position”];
// 构建一堆点
NSValue *point1 = [NSValue valueWithCGPoint:CGPointMake(200, 400)];
NSValue *point2 = [NSValue valueWithCGPoint:CGPointMake(20, 80)];
// 添加点进数组
animation.values = @[point1, point2];
animation.duration = 2;
[self.myView.layer addAnimation:animation forKey:@“position”];
}

// 抖动
- (void)douDongImage
{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@“position.x”];
CGFloat center = self.myView.layer.position.x;
CGFloat left = center - 10;
CGFloat right = center + 10;
NSNumber *l = [NSNumber numberWithFloat:left];
NSNumber *c = [NSNumber numberWithFloat:center];
NSNumber *r = [NSNumber numberWithFloat:right];
animation.values = @[l, c, r, l, c, r];
animation.duration = 1;
[self.myView.layer addAnimation:animation forKey:@“position.x”];
}

// 3D旋转

- (void)transform3D
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@“transform”];
// 结束值
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.myView.layer.transform, M_PI, 100, 0, 0)];
animation.duration = 1;
[self.myView.layer addAnimation:animation forKey:@“transform”];

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值