转场动画CATransition应用

2017-08-14  本文已影响52人  被吹落的风

CATransition类实现层的转场动画。你可以从一组预定义的转换或者通过提供定制的CIFilter实例来指定转场效果。

重点说明以下两个属性:

type:转场动画的类型,默认类型为kCATransitionFade。

subtype:转场动画将要去往的方向。

代码示例:

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
        
    UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    yellowView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:yellowView];
        
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"改变1" forState:UIControlStateNormal];
    button.frame = CGRectMake(10, 10, 300, 40);
    [button addTarget:self action:@selector(changeUIView1) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button2 setTitle:@"改变2" forState:UIControlStateNormal];
    button2.frame = CGRectMake(10, 120, 300, 40);
    [button2 addTarget:self action:@selector(changeUIView2) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
}

下面采用了两种动画方式,一种UIViewAnimation的形式,一种CATransition的形式。

- (void)changeUIView1
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:1.5f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
    [self.view exchangeSubviewAtIndex:3 withSubviewAtIndex:2];
    [UIView commitAnimations];
}
- (void)changeUIView2
{
    CATransition *transition = [CATransition animation];
    transition.delegate = self;
    transition.duration = 1.5f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    transition.type = @"moveIn";//另一种设置动画效果方法
    transition.subtype = kCATransitionFromLeft;
    [self.view exchangeSubviewAtIndex:3 withSubviewAtIndex:2];
    [self.view.layer addAnimation:transition forKey:@"animation"];
}
上一篇 下一篇

猜你喜欢

热点阅读