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

完事

视图过渡动画

视图过渡动画有有限种,一栽是单科视图的连结动画,另一样种植是简单单UIView之间的接入

通类型:

  • UIViewAnimationOptionTransitionFlipFromTop由上向下翻转动画
  • UIViewAnimationOptionTransitionFlipFromBottom由下向上翻转动画
  • UIViewAnimationOptionTransitionCrossDissolve渐现动画
  • UIViewAnimationOptionTransitionCurlDown由臻顶下翻页动画
  • UIViewAnimationOptionTransitionCurlUp由下及上翻页动画
  • UIViewAnimationOptionTransitionFlipFromRight由左向右掉
  • UIViewAnimationOptionTransitionFlipFromLeft由右向左翻转

速度

  • UIViewAnimationOptionCurveEaseInOut 缓入缓出,开始跟得了时减速
  • UIViewAnimationOptionCurveEaseIn 缓入,开始时候减速
  • UIViewAnimationOptionCurveEaseOut 缓出,结束时减速
  • UIViewAnimationOptionCurveLinear 线性,匀速运动

连接视图和速度在动画的options参数中采取。但是不用动多交接类型,真是要难看死了自之天哪。

options: UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionCurveEaseInOut

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

options: [.TransitionCrossDissolve,.CurveEaseInOut]

1. 么视图的连

单个视图的卡通

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

2. 星星单视图的属动画

星星只视图的衔接动画

方法:UIViewtransitionFromView: toView: duration: options:方法
官说:toView added to fromView.superview, fromView removed from
its superview

实际效果:动画的作用范围吗fromView的父视图的frame。动画结束晚fromView见面叫剔除,toView会见让补充2018正版葡京赌侠诗加到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

发表评论

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