当我们在谈论编程的时候,我们在谈论什么iOS开发IOS | MAC

iOS弹簧动画与视图过渡动画

2016-05-25  本文已影响5752人  阳光下慵懒的驴

这两种动画实现起来都很简单,苹果已经为我们封装好了,并且提供了多种好看实用的效果

弹簧动画

弹簧动画
- (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之间的过渡

过渡类型:

速度

过渡视图和速度在动画的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会被添加到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
上一篇下一篇

猜你喜欢

热点阅读