概述
iOS中动画的实现方式
简单的展示平移、缩放、透明度动画
示例
给一张图片添加缩放、平移、透明度动画。很简单看注释
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//加载一张图片,展示到屏幕中
UIImage* image = [UIImage imageNamed:@"timg_1.jpg"];
_imgView = [UIImageView new];
_imgView.frame = CGRectMake(50, 50, 200, 200);
_imgView.image = image;
[self.view addSubview:_imgView];
//点击开始动画按钮
UIButton* btnMove = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnMove setTitle:@"begin animation" forState:UIControlStateNormal];
btnMove.frame = CGRectMake(40, 700, 150, 40);
[btnMove addTarget:self action:@selector(pressStart:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnMove];
}
-(void)pressStart:(UIButton*) btn{
NSLog(@"pressMove==");
//UIView的动画
//p1 动画执行时间(单位:秒)
//p2 动画延时执行时间(单位:秒)
//p3 动画配置参数(例如起始状态、动画变化曲线等等)
//p4 动画执行代码块
//p5 完成动画回调
[UIView animateWithDuration:3 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
//位移和缩放
//_imgView.frame = CGRectMake(200, 200, 100, 100);
//透明度
_imgView.alpha = 0.3;
} completion:^(BOOL finished) {
NSLog(@"动画完成了。。");
}];
}
@end