UIView 动画总结

UIViewAnimation

这是最基本的动画

- (void)startAnimating{
    
                                                  //__bridge 不移交控制权
    [UIView beginAnimations:@"animation" context:(__bridge void *)(self)];
    
    [UIView setAnimationDuration:0.4];
    _square.center = CGPointMake(_square.center.x + 100, _square.center.y);
    [UIView setAnimationDelegate:self];
    
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView commitAnimations];
    
}

UIViewAnimationWithBlocks

对基本UIViewAnimation的block封装

- (void)startBlockAnimation{

    UIViewAnimationOptions options = UIViewAnimationOptionAutoreverse | UIViewAnimationOptionTransitionCrossDissolve ;
    
    [UIView animateWithDuration:0.4 delay:0 options:options animations:^{
        
        _square.center = CGPointMake(_square.center.x + 100, _square.center.y);
        
    } completion:^(BOOL finished) {
        
    }];
    
    
}

+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); //翻转动画

UIViewKeyframeAnimations

设置动画里面的几个关键帧, 系统自动构建出完整的动画


//实现_square 按正方形规矩运动
- (void)startKeyFrameAnimation{
    
    UIViewKeyframeAnimationOptions options = UIViewKeyframeAnimationOptionRepeat;
    
    [UIView animateKeyframesWithDuration:4 delay:0 options:options animations:^{
        
        
        double frameDuration = 1.0 / 4;
        
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:frameDuration animations:^{
            
            _square.center = CGPointMake(_square.center.x + 100, _square.center.y);
            
        }];
        
        [UIView addKeyframeWithRelativeStartTime:frameDuration  relativeDuration:frameDuration animations:^{
            
            _square.center = CGPointMake(_square.center.x , _square.center.y + 100);
            
        }];
        
        [UIView addKeyframeWithRelativeStartTime:frameDuration * 2 relativeDuration:frameDuration animations:^{
            
            _square.center = CGPointMake(_square.center.x - 100, _square.center.y);
            
        }];
        
        [UIView addKeyframeWithRelativeStartTime:frameDuration * 3 relativeDuration:frameDuration animations:^{
            
            _square.center = CGPointMake(_square.center.x , _square.center.y -100);
            
        }];
        
        
        
    } completion:nil];
    
    
}

你可能感兴趣的:(UIView 动画总结)