自带动画_UIView的五个block动画

首先初始化一个label, 添加到视图上, 并给一个初始的frame值. 其实动画的实现过程就是根据frame的变化来实现的.

实现动画的block1

//创建一个动画, 指定动画执行时间
[UIView animateWithDuration:2 animations:^{
   //设置动画结束时, label的frame值
    self.label.frame = CGRectMake(100, 100, 100, 100); 
    
}];

实现动画的Block2

[UILabel animateWithDuration:2 animations:^{
    
    self.label.frame = CGRectMake(100, 100, 100, 100);
    
} completion:^(BOOL finished) {
    //结束后触发的事件
    NSLog(@"%d = 动画结束了", finished);
}];

实现动画的Block3

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

实现动画的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);
}];

实现动画的Block5

//options : 动画效果重复
[UIView animateKeyframesWithDuration:5 delay:0 options:(UIViewKeyframeAnimationOptionRepeat) animations:^{
    
    //relativeDuration: 设置为0.5的话, 0.5 * 5(动画持续时间)-->(相对于Duration:5 来说)
    [UIView  addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
        
        self.label.center = self.view.center;
        
    }];
    
    [UIView  addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
        
        self.label.frame = CGRectMake(100, 100, 100, 100);
        
    }];
    
} completion:^(BOOL finished) {
    
    
}];

你可能感兴趣的:(自带动画_UIView的五个block动画)