动画

动画

动画: 即在应用界面上展示的各种过度效果.

  • 一个产品在运行的时候,动画并不会影响产品的功能.
  • 传达状态
  • 提高用户对直接操作的感知- 帮助用户可视化操作的结果

动画大纲

2016-04-20 下午8.52.01.png
@interface RootViewController ()
@property (nonatomic, strong) UIView *redView;
@property (nonatomic, strong) UIView *greenView;
@end
#pragma mark------redView懒加载
- (UIView *)greenView{ 
if (!_greenView) { 
_greenView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
 _greenView.backgroundColor = [UIColor greenColor];  
} 
return _greenView;
}
- (UIView *)redView { if (!_redView) { _redView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)]; _redView.backgroundColor = [UIColor redColor];
// [self.view addSubview:_redView]; 
} 
return _redView; 
}
#先创建一个button的初始化方法
- (void)buttonWithTitle:(NSString*)title action:(SEL)action frame:(CGRect)frame{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setFrame:frame];
[button setTitle:title forState:UIControlStateNormal];
[button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];return button;}
//添加一个 button 按钮
- (void)viewDidLoad{
UIButton *button_1 = [self buttonWithTitle:@"动画块" action:@selector(animation) frame:CGRectMake(50, 400, 60, 50)];
[self.view addSubview:button_1];
}

#pragma mark-----button 的点击方法
- (void)animation {
//第一个参数:动画的标记
//第二个参数:上下文,可以为动画的代理方法传值
[UIView beginAnimations:@"第一个动画" context:@"11"];
//动画时长
[UIView setAnimationDuration:2];
//重复次数 
[UIView setAnimationRepeatCount:5];
//设置翻转,让动画原速返回,只有设置了重复次数,此属性才能生效 [UIView setAnimationRepeatAutoreverses:YES];
//代理方法 
[UIView setAnimationDelegate:self];
//动画开始时的代理方法 
[UIView setAnimationWillStartSelector:@selector(animationWillStart: context:)];
//动画停止时的代理方法 [UIView setAnimationDidStopSelector:@selector(animationStop)];

//改变redView的frame 
//OC提下的点和C语言的点,意义不一样,不能直接复制
CGRect rect = self.redView.frame; 
rect.origin = CGPointMake(200, 300); 
self.redView.frame = rect;
//提交动画
 [UIView commitAnimations];
}
#pragma mark-----动画开始时的调用的方法
- (void)animationWillStart:(NSString *)animationTag context:(NSString *)context{
 NSLog(@"%@动画开始-------context------%@",animationTag,context);
}
#pragma mark----动画结束时调用的方法
- (void)animationStop{
 NSLog(@"动画结束了");
}

block 动画的常见三种形式;

- (void)viewDidLoad{
UIButton *button_2 = [self buttonWithTitle:@"block 动画" action:@selector(blockAnimation) frame:CGRectMake(160, 400, 100, 50)];
[self.view addSubview:button_2];}
#pragma mark----block动画1
[UIView animateWithDuration:3.0 animations:^{ 
//需要改变的属性 
self.redView.backgroundColor = [UIColor cyanColor];
 }];
#pragma mark----block动画2 
//多了动画结束的 block参数
 [UIView animateWithDuration:3.0 animations:^{ self.redView.backgroundColor = [UIColor cyanColor]; 
} completion:^(BOOL finished) { 
if(finished){
//说明动画结束 self.redView.backgroundColor = [UIColor redColor]; 
} 
}];
#pragma mark----block动画3 
//delay:延时
 //options:动画速率或者是动画效果的一个配置 
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ [UIView setAnimationRepeatCount:5]; 
[UIView setAnimationRepeatAutoreverses:YES]; 
//所有的动画配置,必须写在属性改变的上面 self.redView.frame = CGRectMake(300, 400, 50, 50); 
} completion:^(BOOL finished) { self.redView.backgroundColor = [UIColor cyanColor]; 
}];
}

转场动画

#pragma mark-----转场动画的调用方法
- (void)transitionAnimation{ 
//本质原理:
 [self.redView.superView addSubView:self.greenView]; 
//[self.redView removeFromSuperView]; 
[UIView transitionFromView:self.redView toView:self.greenView duration:1.5 options:UIViewAnimationOptionTransitionFlipFromRight completion:nil];
}

高级动画,关键帧

- (void)keyFrameAnimation{
//要使一个 view 走一个矩形
void (^blockFrame)(void) = ^(){
for(int i = 0; i < 4;i++){
[UIView addKeyframeWithRealativeStartTime:i/4.0 relativeDuration;1/4.0 animations:^{
CGRect redFrame = self.redView.frame;
switch(i){
case0;
 redFrame.origin.x += 200; 
self.redView.frame = redFrame;
 break;
case 1: 
redFrame.origin.y += 200; 
self.redView.frame = redFrame; 
break;
case 2: 
redFrame.origin.x -= 200;
 self.redView.frame = redFrame;
 break;
case 3: 
redFrame.origin.y -= 200; 
self.redView.frame = redFrame; 
break;
default: 
break;
}
}];
}
}
[UIView animateKeyframesWithDuration:4.0 delay:0.0 options:UIViewKeyframeAnimationOptionBeginFromCurrentState animations:blockFrame completion:nil];
// [UIView animateKeyframesWithDuration:4.0 delay:0.0 options:UIViewKeyframeAnimationOptionBeginFromCurrentState animations:^{
// //关键帧
// //startTime:当前帧的开始时间. startTime*总时长
// //一帧的时长: relativeDuration.relativeDuration*总时长. 范围0-1
// [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.2 animations:^{// self.redView.frame = CGRectMake(200,80, 100, 100);// }];
//
 } completion:^(BOOL finished) {

}];
}

spring 动画,果冻效果,弹簧效果

#pragma mark------spring 动画,果冻效果.弹簧效果
- (void)springAnimation{ 
//damping:阻尼 
//velocity:初始速率

[UIView animateWithDuration:2.0 delay:0.0 usingSpringWithDamping:0.1 initialSpringVelocity:50 options:UIViewAnimationOptionRepeat animations:^{
self.redView.frame = CGRectMake(100, 200, 100, 100); self.redView.backgroundColor = [UIColor blackColor];  
} completion:nil]; 
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在iOS实际开发中常用的动画无非是以下四种:UIView动画,核心动画,帧动画,自定义转场动画。 1.UIView...
    请叫我周小帅阅读 8,419评论 1 23
  • 概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你...
    Yiart阅读 9,297评论 3 34
  • 目录 ** UIView 动画 ** ** Core Animation ** ** FaceBook POP动画...
    方向_4d0d阅读 5,674评论 0 3
  • 先看看CAAnimation动画的继承结构 CAAnimation{ CAPropertyAnimation { ...
    时间不会倒着走阅读 5,658评论 0 1
  • boss说:“混迹职场,也要有策略,不能盲目迎合老板的喜好。有人说,要让自己不断有利用价值才能生存下去。但是这样一...
    灵猫喵呜阅读 2,600评论 0 0

友情链接更多精彩内容