【UIKit-124-5】#import <UIKit/UIView.h>

本文深入探讨了iOS开发中使用UIView动画与转场的相关API,包括基本动画、带回调的动画、多参数动画、力学模拟弹簧动画、转场动画及动画结束后的操作,提供了丰富的实例和代码说明。

【动画-BLOCK】

@interface UIView(UIViewAnimationWithBlocks)

一般用第二个就差不多了。

1、一般动画

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations;

    [UIView animateWithDuration:1 animations:^{
        [redView setFrame:CGRectMake(10, 300, 80, 100)];
    }];

2、动画完成有回调
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOLfinished))completion
    [UIView animateWithDuration:1 animations:^{
        [redView setFrame:CGRectMake(10, 300, 80, 100)];
        //有动画
    } completion:^(BOOL finished) {
        [redView setFrame:CGRectMake(100, 100, 100, 100)];
        //没动画
    }];

3、多参数动画

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;

[UIView animateWithDuration:1 delay:1 options:UIViewAnimationOptionAllowUserInteraction animations:^{
            [redView setFrame:CGRectMake(10, 300, 80, 100)];

        } completion:^(BOOL finished) {
            [redView setFrame:CGRectMake(100, 100, 100, 100)];

        }];
    
    
    /*
     typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
     
     UIViewAnimationOptionLayoutSubviews //提交动画的时候布局子控件,表示子控件将和父控件一同动画。
     UIViewAnimationOptionAllowUserInteraction //动画时允许用户交流,比如触摸
     UIViewAnimationOptionBeginFromCurrentState //从当前状态开始动画
     UIViewAnimationOptionRepeat //动画无限重复
     UIViewAnimationOptionAutoreverse //执行动画回路,前提是设置动画无限重复
     UIViewAnimationOptionOverrideInheritedDuration //忽略外层动画嵌套的执行时间
     UIViewAnimationOptionOverrideInheritedCurve //忽略外层动画嵌套的时间变化曲线
     UIViewAnimationOptionAllowAnimatedContent //通过改变属性和重绘实现动画效果,如果key没有提交动画将使用快照
     UIViewAnimationOptionShowHideTransitionViews //用显隐的方式替代添加移除图层的动画效果
     UIViewAnimationOptionOverrideInheritedOptions //忽略嵌套继承的选项
     
     //时间函数曲线相关
     UIViewAnimationOptionCurveEaseInOut //时间曲线函数,由慢到快
     UIViewAnimationOptionCurveEaseIn //时间曲线函数,由慢到特别快
     UIViewAnimationOptionCurveEaseOut //时间曲线函数,由快到慢
     UIViewAnimationOptionCurveLinear //时间曲线函数,匀速
     
     
     //转场动画相关的
     UIViewAnimationOptionTransitionNone //无转场动画
     UIViewAnimationOptionTransitionFlipFromLeft //转场从左翻转
     UIViewAnimationOptionTransitionFlipFromRight //转场从右翻转
     UIViewAnimationOptionTransitionCurlUp //上卷转场
     UIViewAnimationOptionTransitionCurlDown //下卷转场
     UIViewAnimationOptionTransitionCrossDissolve //转场交叉消失
     UIViewAnimationOptionTransitionFlipFromTop //转场从上翻转
     UIViewAnimationOptionTransitionFlipFromBottom //转场从下翻转
     
     };

     */


4、力学模拟弹簧

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);

        // 阻尼0.3  速度3 - 物理模拟弹簧的动画
        [UIView animateWithDuration:1 delay:1 usingSpringWithDamping:0.3 initialSpringVelocity:3 options:UIViewAnimationOptionCurveLinear animations:^{
            [redView setFrame:CGRectMake(30, 300, 100, 100)];
            
        } completion:^(BOOL finished) {
            NSLog(@"ok");
            
        }];


5、转场动画,对参数中的 view

+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

    // 一般用作转场,参数使用转场参数
    [UIView transitionWithView:redView duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        
        [greenView removeFromSuperview];// 父视图上原来的remove
        [redView addSubview:blueView];  // 父视图上新增view
        
    } completion:^(BOOL finished) {
        NSLog(@"ok");
    }];



6、转场动画,对整个 View 

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion; 

    // 一般用作转场,参数使用转场参数,// redView remove,greenView  add
    [UIView transitionFromView:redView toView:greenView duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
        NSLog(@"ok");
    }];
    




7、动画结束,删除数组中的控件

+ (void)performSystemAnimation:(UISystemAnimation)animation onViews:(NSArray *)views options:(UIViewAnimationOptions)options animations:(void (^)(void))parallelAnimations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);

    //动画结束时,模糊删除onViews数组里的控件(参数xcode6.4时之有一个)
    [UIView performSystemAnimation:UISystemAnimationDelete onViews:@[redView,greenView] options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{

        [blueView setFrame:CGRectMake(111, 111, 111, 111)];

    } completion:^(BOOL finished) {
        
    }];


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值