那三种动画完毕起来都很简短,苹果已经为大家封装好了,并且提供了三种狼狈实用的功用

弹簧动画

弹簧动画

- (void)viewDidLoad {
    [super viewDidLoad];

    // 创建一个气泡
    UIButton * bubble = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    [self.view addSubview:bubble];
    bubble.layer.cornerRadius = 50;
    bubble.backgroundColor = [UIColor cyanColor];

    [bubble addTarget:self action:@selector(bubbleAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)bubbleAction:(UIButton *)button {
    // 先缩小
    button.transform = CGAffineTransformMakeScale(0.7, 0.7);

    // 弹簧动画,参数分别为:时长,延时,弹性(越小弹性越大),初始速度
    [UIView animateWithDuration: 0.7 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0.3 options:0 animations:^{
        // 放大
        button.transform = CGAffineTransformMakeScale(1, 1);
    } completion:nil];
}

完事

视图过渡动画

视图过渡动画有二种,一种是单个视图的连片动画,另一种是四个UIView之间的连片

连通类型:

  • UIViewAnimationOptionTransitionFlipFromTop由上向下翻跟斗画
  • UIViewAnimationOptionTransitionFlipFromBottom由下向上翻转动画
  • UIViewAnimationOptionTransition克罗丝Dissolve渐现动画
  • UIViewAnimationOptionTransitionCurlDown由上到下翻页动画
  • UIViewAnimationOptionTransitionCurlUp由下到上翻页动画
  • UIViewAnimationOptionTransitionFlipFromRight由左向右翻转
  • UIViewAnimationOptionTransitionFlipFromLeft由右向左翻转

速度

  • UIViewAnimationOptionCurveEaseInOut 缓入缓出,起先和终结时减速
  • UIViewAnimationOptionCurveEaseIn 缓入,开端时候减速
  • UIViewAnimationOptionCurveEaseOut 缓出,停止时减速
  • UIViewAnimationOptionCurveLinear 线性,匀速运动

连通视图和进度在动画的options参数中利用。可是并非使用种种对接类型,真是要难看死了自身的天哪。

options: UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionCurveEaseInOut

Swift写法(感谢熊格智力障碍的评论):

options: [.TransitionCrossDissolve,.CurveEaseInOut]

一. 单个视图的交接

单个视图的卡通

- (void)viewDidLoad {
    [super viewDidLoad];

    // 创建一个按钮
    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
    [self.view addSubview:button];
    button.backgroundColor = [UIColor orangeColor];
    button.layer.cornerRadius = 3;

    [button setTitle:@"👀" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonAction:(UIButton *)button {
    // transition动画:指定一个view,单独为它设置transition的动画
    // option设置动画类型,这里使用翻页动画以及淡出淡入
    [UIView transitionWithView:button duration:0.7 options:UIViewAnimationOptionTransitionCurlDown | UIViewAnimationOptionCurveEaseOut animations:^{
        [button setTitle:@"😝" forState:UIControlStateNormal];
    } completion:nil];
}

再来三个实用的imageView图片渐变效果:

图形渐变

@interface ViewController ()
@property (nonatomic, retain) UIImageView * imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 200)];
    [self.view addSubview:_imageView];
    _imageView.image = [UIImage imageNamed:@"1.jpg"];

    // 1s后换图片
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage) userInfo:nil repeats:NO];
}

- (void)changeImage {
    // 使用渐变效果就够了
    [UIView transitionWithView:_imageView duration:1 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        _imageView.image = [UIImage imageNamed:@"2.jpg"];
    } completion:nil];
}

二. 多少个视图的接入动画

五个视图的连接动画

方法:UIViewtransitionFromView: toView: duration: options:方法
合法解释:toView added to fromView.superview, fromView removed from
its superview

实效:动画的效益范围为fromView的父视图的frame。动画结束后fromView会被删除,toView会被添加到fromView的父视图

@interface ViewController ()
@property (nonatomic, retain) UIView * view1;
@property (nonatomic, retain) UIView * view2;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 创建占用半个屏幕的view,作为过渡view的父视图,测试动画的作用范围
    UIView * bigView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 300)];
    [self.view addSubview:bigView];
    bigView.backgroundColor = [UIColor orangeColor];

    self.view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
    [bigView addSubview:_view1];
    _view1.backgroundColor = [UIColor redColor];

    self.view2 = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
    // 不要加到视图上!看效果
//    [self.view addSubview:_view2];
    _view2.backgroundColor = [UIColor blackColor];

    // 1s后执行
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(animate) userInfo:nil repeats:NO];
}

- (void)animate {
    [UIView transitionFromView:_view1 toView:_view2 duration:1 options:UIViewAnimationOptionTransitionCurlDown completion:^(BOOL finished) {
    }];
}
@end

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图