UIViewAnimationWithBlocks动画

#import "RootViewController.h"

@interface RootViewController ()
//设置属性
@property (strong, nonatomic) UILabel *label;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.label = [[UILabel alloc] init];
    self.label.bounds = CGRectMake(0, 0, 100, 100);
    self.label.layer.anchorPoint = CGPointMake(0, 0);
    self.label.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.label];
    
    
    self.view.backgroundColor = [UIColor colorWithRed:0.964 green:1.000 blue:0.476 alpha:1.000];
    
}

#pragma mark  ------- 实现动画的五个 block -------
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  
#1. 实现动画的block1
    [UIView animateWithDuration:2 animations:^{
        
        //开始一个动画, block 中写入实现效果
        self.label.frame = CGRectMake(100, 100, 100, 100); 
        
    }];

#2. 实现动画的Block2
    [UILabel animateWithDuration:2 animations:^{
        
        //动画效果
        self.label.frame = CGRectMake(100, 100, 100, 100);
        
    } completion:^(BOOL finished) {
        
        //结束后触发的事件
        NSLog(@"%d = 动画结束了", finished);
    }];
    
#3. 实现动画的Block3
    //options: 用来设置动画的效果(淡入淡出)
    [UIView animateWithDuration:2 delay:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        //动画效果
        self.label.frame = CGRectMake(100, 100, 100, 100);
        
    } completion:^(BOOL finished) {
        //动画结束后触发的事件
        NSLog(@"结束了%d", finished);
    }];

#4. 实现动画的Block4
    //usingSpring: 动画模拟弹簧效果
    //usingSpringWithDamping: 阻尼(0~1)值越小动画越明显
    //initialSpringVelocity: 动画初始变换速率
    [UIView animateWithDuration:5 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        self.label.center = CGPointMake(self.view.center.x, 100);
        
    } completion:^(BOOL finished) {
        
        NSLog(@"动画结束%d", finished);
    }];
    

    
#5. 实现动画的Block5
    //options : 动画效果重复
    [UIView animateKeyframesWithDuration:5 delay:0 options:(UIViewKeyframeAnimationOptionRepeat) animations:^{
        
        # 关键帧1--> 动画效果1
        //relativeDuration: 设置为0.5的话, 0.5 * 5(动画持续时间)-->(相对于Duration:5 来说)
        [UIView  addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
            
            self.label.center = self.view.center;
            
        }];
        # 关键帧2--> 动画效果2
        //注意: 当有多个动画时, 应注意后面动画的开始时间
        [UIView  addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
            
            self.label.frame = CGRectMake(100, 100, 100, 100);
            
        }];
        
    } completion:^(BOOL finished) {
        
        //动画结束方法
    }];
    
    
    
}
@end```
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容