搜索
您的当前位置:首页正文

UIView动画简单小例子

来源:二三娱乐
主要分类.png

例子动画

UIView动画.gif

实现代码

#import "ViewController.h"

//1。弧度转角度
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))
//2。角度转弧度
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)

@interface ViewController ()

@property (nonatomic , strong) UIImageView *imageView;       /**< 图片 */

@property (nonatomic , strong) UIButton *keyframesBtn;       /**< keyframes动画按钮 */



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    imageView.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.height / 2);
    imageView.image = [UIImage imageNamed:@"girl"];
    [self.view addSubview:imageView];
    self.imageView = imageView;
    
    
    //frame 大小动画
    UIButton *frameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    frameBtn.frame = CGRectMake(0, 20, 100, 40);
    frameBtn.backgroundColor = [UIColor orangeColor];
    [frameBtn setTitle:@"frame动画" forState:UIControlStateNormal];
    [frameBtn addTarget:self action:@selector(didFrameBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:frameBtn];
    
    //bounds 拉伸动画
    UIButton *boundsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    boundsBtn.frame = CGRectMake(120, 20, 100, 40);
    boundsBtn.backgroundColor = [UIColor orangeColor];
    [boundsBtn setTitle:@"bounds动画" forState:UIControlStateNormal];
    [boundsBtn addTarget:self action:@selector(didBoundsBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:boundsBtn];
    
    //center 转移动画
    UIButton *centerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    centerBtn.frame = CGRectMake(240, 20, 100, 40);
    centerBtn.backgroundColor = [UIColor orangeColor];
    [centerBtn setTitle:@"center动画" forState:UIControlStateNormal];
    [centerBtn addTarget:self action:@selector(didCenterBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:centerBtn];
    
    //transform 旋转动画
    UIButton *transformBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    transformBtn.frame = CGRectMake(0, 80, 120, 40);
    transformBtn.backgroundColor = [UIColor orangeColor];
    [transformBtn setTitle:@"transform动画" forState:UIControlStateNormal];
    [transformBtn addTarget:self action:@selector(didTransformBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:transformBtn];
    
    //alpha 透明度动画
    UIButton *alphaBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    alphaBtn.frame = CGRectMake(140, 80, 100, 40);
    alphaBtn.backgroundColor = [UIColor orangeColor];
    [alphaBtn setTitle:@"alpha动画" forState:UIControlStateNormal];
    [alphaBtn addTarget:self action:@selector(didAlphaBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:alphaBtn];
    
    //keyframes 动画
    UIButton *keyframesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    keyframesBtn.frame = CGRectMake(260, 80, 120, 40);
    keyframesBtn.backgroundColor = [UIColor orangeColor];
    [keyframesBtn setTitle:@"keyframe动画" forState:UIControlStateNormal];
    [keyframesBtn addTarget:self action:@selector(didKeyframesBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:keyframesBtn];
    self.keyframesBtn = keyframesBtn;
    
    //spring 动画
    UIButton *springBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    springBtn.frame = CGRectMake(0, 140, 120, 40);
    springBtn.backgroundColor = [UIColor orangeColor];
    [springBtn setTitle:@"Spring动画" forState:UIControlStateNormal];
    [springBtn addTarget:self action:@selector(didSpringBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:springBtn];
    
    //transition 动画
    UIButton *transitionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    transitionBtn.frame = CGRectMake(160, 140, 120, 40);
    transitionBtn.backgroundColor = [UIColor orangeColor];
    [transitionBtn setTitle:@"transition动画" forState:UIControlStateNormal];
    [transitionBtn addTarget:self action:@selector(didTransitionBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:transitionBtn];
    
}



/**
 frame动画
 */
- (void)didFrameBtnClick {

    CGRect originalRect = self.imageView.frame;
    CGRect rect = CGRectMake(self.imageView.frame.origin.x - 40, self.imageView.frame.origin.y - 140, 200 + 40 * 2, 50);
    
    [UIView animateWithDuration:1 animations:^{
        self.imageView.frame = rect;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 animations:^{
            self.imageView.frame = originalRect;
        }];
    }];
}


/**
 bounds动画
 */
- (void)didBoundsBtnClick {

    CGRect originalBounds = self.imageView.bounds;
    CGRect rect = CGRectMake(0, 0, 300, 200);
    
    [UIView animateWithDuration:0.5 animations:^{
        self.imageView.bounds = rect;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 animations:^{
            self.imageView.bounds = originalBounds;
        }];
    }];
}


/**
 center动画
 */
- (void)didCenterBtnClick {

    CGPoint originalPoint = self.imageView.center;
    CGPoint point = CGPointMake(self.imageView.center.x, self.imageView.center.y - 170);
    
    [UIView animateWithDuration:1 animations:^{
        self.imageView.center = point;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 animations:^{
            self.imageView.center = originalPoint;
        }];
    }];
}


/**
 transform动画
 */
- (void)didTransformBtn {
//    CGAffineTransform originalTransform = self.imageView.transform;
    
    [UIView animateWithDuration:1 animations:^{
        self.imageView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(45));
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 animations:^{
            self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, DEGREES_TO_RADIANS(-45));
        }];
    }];
}


/**
 alpha动画
 */
- (void)didAlphaBtn {
    [UIView animateWithDuration:1 animations:^{
        self.imageView.alpha = .3f;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 animations:^{
            self.imageView.alpha = 1.f;
        }];
    }];
}


/**
 keyframes动画
 */
- (void)didKeyframesBtn {
    
    [UIView animateKeyframesWithDuration:9.0 delay:0.f options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.f relativeDuration:1.0 / 4 animations:^{
            self.keyframesBtn.backgroundColor = [UIColor colorWithRed:0.9475 green:0.1921 blue:0.1746 alpha:1.0];
        }];
        [UIView addKeyframeWithRelativeStartTime:1.0 / 4 relativeDuration:1.0 / 4 animations:^{
            self.keyframesBtn.backgroundColor = [UIColor colorWithRed:0.1064 green:0.6052 blue:0.0334 alpha:1.0];
        }];
        [UIView addKeyframeWithRelativeStartTime:2.0 / 4 relativeDuration:1.0 / 4 animations:^{
            self.keyframesBtn.backgroundColor = [UIColor colorWithRed:0.1366 green:0.3017 blue:0.8411 alpha:1.0];
        }];
        [UIView addKeyframeWithRelativeStartTime:3.0 / 4 relativeDuration:1.0 / 4 animations:^{
            self.keyframesBtn.backgroundColor = [UIColor colorWithRed:0.619 green:0.037 blue:0.6719 alpha:1.0];
        }];
        [UIView addKeyframeWithRelativeStartTime:3.0 / 4 relativeDuration:1.0 / 4 animations:^{
            self.keyframesBtn.backgroundColor = [UIColor orangeColor];
        }];
    } completion:^(BOOL finished) {
        NSLog(@"动画结束");
    }];
}


/**
 spring动画
 usingSpringWithDamping:
 弹簧动画的阻尼值,也就是相当于摩擦力的大小,
 该属性的值从0.0到1.0之间,越靠近0,阻尼越小,弹动的幅度越大,
 反之阻尼越大,弹动的幅度越小,如果大道一定程度,会出现弹不动的情况。
 
 initialSpringVelocity:
 弹簧动画的速率,或者说是动力。
 值越小弹簧的动力越小,弹簧拉伸的幅度越小,反之动力越大,弹簧拉伸的幅度越大。这
 里需要注意的是,如果设置为0,表示忽略该属性,由动画持续时间和阻尼计算动画的效果。
 
 */
- (void)didSpringBtn {

    CGRect originalRect = self.imageView.frame;
    CGRect rect = CGRectMake(self.imageView.frame.origin.x-80, self.imageView.frame.origin.y, 200, 200);
    
    [UIView animateWithDuration:.4f delay:.0f usingSpringWithDamping:0.2 initialSpringVelocity:0.6 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.imageView.frame = rect;
    } completion:^(BOOL finished) {
        self.imageView.frame = originalRect;
    }];
}


/**
 transition动画
 */
- (void)didTransitionBtn {
    
    [UIView transitionWithView:self.imageView duration:2.0 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        
    } completion:^(BOOL finished) {
        [UIView transitionWithView:self.imageView duration:2.0 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
           
        } completion:^(BOOL finished) {
            
        }];
    }];
}

//UIViewAnimationOptions 参数选择

//UIViewAnimationOptionLayoutSubviews            //进行动画时布局子控件
//UIViewAnimationOptionAllowUserInteraction      //进行动画时允许用户交互
//UIViewAnimationOptionBeginFromCurrentState     //从当前状态开始动画
//UIViewAnimationOptionRepeat                    //无限重复执行动画
//UIViewAnimationOptionAutoreverse               //执行动画回路
//UIViewAnimationOptionOverrideInheritedDuration //忽略嵌套动画的执行时间设置
//UIViewAnimationOptionOverrideInheritedCurve    //忽略嵌套动画的曲线设置
//UIViewAnimationOptionAllowAnimatedContent      //转场:进行动画时重绘视图
//UIViewAnimationOptionShowHideTransitionViews   //转场:移除(添加和移除图层的)动画效果
//UIViewAnimationOptionOverrideInheritedOptions  //不继承父动画设置
//
//UIViewAnimationOptionCurveEaseInOut            //时间曲线,慢进慢出(默认值)
//UIViewAnimationOptionCurveEaseIn               //时间曲线,慢进
//UIViewAnimationOptionCurveEaseOut              //时间曲线,慢出
//UIViewAnimationOptionCurveLinear               //时间曲线,匀速
//
//UIViewAnimationOptionTransitionNone            //转场,不使用动画
//UIViewAnimationOptionTransitionFlipFromLeft    //转场,从左向右旋转翻页
//UIViewAnimationOptionTransitionFlipFromRight   //转场,从右向左旋转翻页
//UIViewAnimationOptionTransitionCurlUp          //转场,下往上卷曲翻页
//UIViewAnimationOptionTransitionCurlDown        //转场,从上往下卷曲翻页
//UIViewAnimationOptionTransitionCrossDissolve   //转场,交叉消失和出现
//UIViewAnimationOptionTransitionFlipFromTop     //转场,从上向下旋转翻页
//UIViewAnimationOptionTransitionFlipFromBottom  //转场,从下向上旋转翻页


//UIViewKeyframeAnimationOptions 参数选择

//UIViewAnimationOptionLayoutSubviews           //进行动画时布局子控件
//UIViewAnimationOptionAllowUserInteraction     //进行动画时允许用户交互
//UIViewAnimationOptionBeginFromCurrentState    //从当前状态开始动画
//UIViewAnimationOptionRepeat                   //无限重复执行动画
//UIViewAnimationOptionAutoreverse              //执行动画回路
//UIViewAnimationOptionOverrideInheritedDuration //忽略嵌套动画的执行时间设置
//UIViewAnimationOptionOverrideInheritedOptions //不继承父动画设置
//
//UIViewKeyframeAnimationOptionCalculationModeLinear     //运算模式 :连续
//UIViewKeyframeAnimationOptionCalculationModeDiscrete   //运算模式 :离散
//UIViewKeyframeAnimationOptionCalculationModePaced      //运算模式 :均匀执行
//UIViewKeyframeAnimationOptionCalculationModeCubic      //运算模式 :平滑
//UIViewKeyframeAnimationOptionCalculationModeCubicPaced //运算模式 :平滑均匀

@end

Top