2018正版葡京赌侠诗iOS弹簧动画与视图过渡动画

那二种动画达成起来都很粗大略,苹果已经为我们封装好了,并且提供了多样狼狈实用的功能

弹簧动画

弹簧动画

- (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];
}

完事

视图过渡动画

视图过渡动画有二种,1种是单个视图的联网动画,另一种是三个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

相关文章