UIView简单动画

UIView动态实现的效果有以下几种:

     1.动态改变frame

     2.动态改变color

     3.动态改变alpha

     4.动态改变bounds

首先,我们先看几种BasicView动画

 1 #pragma mark - 改变frame
 2 - (IBAction)changeFrame:(UIButton *)sender {
 3     
 4     // UIView动画有开始,有结束,以beginAnimation开始,以commitAnimation结束
 5     // 第一步:开始UIView动画
 6     [UIView beginAnimations:@"move" context:nil];
 7     
 8     // 第二步:设置动画时长
 9     [UIView setAnimationDuration:2];
10     
11     // 第三步:设置UIView动画的回调代理
12     [UIView setAnimationDelegate:self];
13     
14     // 第四步:设置相关的对象的frame
15     _textView.frame = CGRectMake(100, 100, 200, 100);
16     
17     // 第五步:结束动画
18     [UIView commitAnimations];
19 }
1 #pragma mark - 改变color
2 - (IBAction)changeColor:(UIButton *)sender {
3     
4     [UIView beginAnimations:@"color" context:nil];
5     [UIView setAnimationDuration:2];
6     [UIView setAnimationDelegate:self];
7     _textView.backgroundColor = [UIColor blueColor];
8     [UIView commitAnimations];
9 }
1 #pragma mark - 改变alpha
2 - (IBAction)changeAlpha:(UIButton *)sender {
3     
4     [UIView beginAnimations:@"alpha" context:nil];
5     [UIView setAnimationDuration:2];
6     [UIView setAnimationDelegate:self];
7     _textView.alpha = 0.3;
8     [UIView commitAnimations];
9 }
 1 #pragma mark - 翻转
 2 - (IBAction)rotationAction:(UIButton *)sender {
 3     
 4     // 第一步:开始动画
 5     [UIView beginAnimations:@"rotation" context:nil];
 6     
 7     // 第二步:设置时长
 8     [UIView setAnimationDuration:2];
 9     
10     // 第三步:设置淡入效果
11     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
12     
13     // 第四步:设置代理
14     [UIView setAnimationDelegate:self];
15     
16     // 第五步:设置翻转方向
17     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_textView cache:YES];
18     
19     // 第六步:结束动画
20     [UIView commitAnimations];
21 }
 1 #pragma mark - 旋转
 2 - (IBAction)transtromAction:(UIButton *)sender {
 3     
 4     // 第一步:开始动画
 5     [UIView beginAnimations:@"transform" context:nil];
 6     
 7     // 第二步:设置时长
 8     [UIView setAnimationDuration:2];
 9     
10     // 第三步:要进行旋转,所以要设置角度
11     CGAffineTransform transform = CGAffineTransformMakeRotation(3 * M_PI);
12     
13     // 第四步:设置旋转角度的对象
14     [_textView setTransform:transform];
15     
16     // 第五步:设置代理
17     [UIView setAnimationDelegate:self];
18     
19     // 第六步:结束动画
20     [UIView commitAnimations];
21 }

然后我们来说几种Block动画:

1.简单动画

 1 #pragma mark - 简单动画
 2 - (IBAction)easyAnimation:(UIButton *)sender {
 3     
 4     // 参数1:设置动画时长
 5     // 参数2:动画要显示的效果
 6     // 参数3:动画完成时进行的事情
 7     __weak typeof(self)weakSelf = self;
 8     [UIView animateWithDuration:2.0f animations:^{
 9         weakSelf.testImgView.center = self.view.center;
10     } completion:^(BOOL finished) {
11         NSLog(@"动画完成");
12     }];
13 }

2.复杂动画

 1 #pragma mark - 复杂动画
 2 - (IBAction)complexBlockAnimation:(UIButton *)sender {
 3     
 4     // 参数1:时长
 5     // 参数2:动画的延迟时间
 6     // 参数3:动画的枚举值
 7     // 参数4:要实现的动画效果
 8     // 参数5:动画完成的时候要干的事情
 9     __weak typeof(self)weakSelf = self;
10     [UIView animateWithDuration:3.0f delay:1.0f options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
11         weakSelf.testImgView.frame = CGRectMake(100, 200, 200, 100);
12     } completion:^(BOOL finished) {
13         NSLog(@"动画完成");
14     }];
15 }

3.关键帧动画

 1 #pragma mark - 关键帧动画
 2 - (IBAction)keyFrameAnimation:(UIButton *)sender {
 3     
 4     // 参数1:时长
 5     // 参数2:动画的延迟时间
 6     // 参数3:枚举值
 7     // 参数4:开始动画
 8     __weak typeof(self)weakSelf = self;
 9     [UIView animateKeyframesWithDuration:3.0f delay:1.0f options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
10         
11         // 在这个里面需要加一个方法,即创建block的关键帧
12         // 参数1:帧动画的开始时间
13         // 参数2:帧动画的持续时间
14         // 参数3:实现的效果
15         [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.5 animations:^{
16             // 实现的效果
17             weakSelf.testImgView.frame = CGRectMake(100, 200, 200, 100);
18         }];
19     } completion:^(BOOL finished) {
20         NSLog(@"哈哈哈");
21     }];
22 }

4.Spring动画

 1 // 参数1:动画时长
 2 // 参数2:延迟
 3 // 参数3:类似弹簧的效果值,(0-1)
 4 // 参数4:初始化spring的一个速度
 5 // 参数5:spring动画的枚举值
 6 // 参数6:开始动画
 7 // 参数7:动画完成
 8 __weak typeof(self)weakSelf = self;
 9 [UIView animateWithDuration:3.0f delay:0.5f usingSpringWithDamping:1.0 initialSpringVelocity:10 options:UIViewAnimationOptionOverrideInheritedDuration animations:^{
10     
11     weakSelf.imgView.frame = CGRectMake(100, 100, 200, 100);
12 } completion:^(BOOL finished) {
13     NSLog(@"fuck");
14 }];
原文地址:https://www.cnblogs.com/zhizunbao/p/5532351.html