定义属性
@property(nonatomic,retain)UIView *myView;
创建
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myView=[[UIView alloc] initWithFrame:CGRectMake(100, 100, 150, 0)];
self.myView.backgroundColor=[UIColor yellowColor];
[self.view addSubview:self.myView];
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(100, 300, 150, 50);
[button setTitle:@"点击" forState: UIControlStateNormal];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside ];
[self.view addSubview:button];
}
点击方法
-(void)click:(UIButton *)button{
//1.动画(动画从起始位置移动到指定位置)
//参数1:动画的播放时长,单位秒
//参数2:把要实现的动画效果,放到block去完成
// [UIView animateWithDuration:2 animations:^{
// self.myView.frame=CGRectMake(0, 0, 50, 50);
// self.myView.backgroundColor=[UIColor cyanColor];
//设置透明度,如果透明度是1,动画在移动过程中颜色也会渐变
// self.myView.alpha=0;
// }];
//UIView的第二种动画效果(放大缩小,可以恢复原状)(动画下拉并回收)
// [UIView animateWithDuration:2 animations:^{
// self.myView.frame=CGRectMake(100, 100, 150, 100);
// } completion:^(BOOL finished) {
// self.myView.backgroundColor=[UIColor cyanColor];
// [UIView animateWithDuration:2 animations:^{
// self.myView.frame=CGRectMake(100, 100, 150, 0);
// }];
// }];
//
//UIView的第三种动画(动画下拉并回收,可以设置延时)
//参数2:延时多久执行
// [UIView animateWithDuration:2 delay:0.1 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{
// self.myView.frame=CGRectMake(100, 100, 150, 200);
// } completion:^(BOOL finished) {
// [UIView animateWithDuration:2 animations:^{
// self.myView.frame=CGRectMake(100, 100, 150, 0);
// }];
// }];
//UIView动画的第四种(动画下拉,并震动)
//参数3:震动的幅度,越小越大
//参数4:初始速度,越大越快
[UIView animateWithDuration:2 delay:0.1 usingSpringWithDamping:0.1 initialSpringVelocity:100 options: UIViewAnimationOptionRepeat animations:^{
self.myView.frame=CGRectMake(100, 100, 150, 200);
} completion:^(BOOL finished) {
}];
}