CATransition自定义UIViewController过渡动画

CATransition

CATransition是CAAnimation的子类,可以通过设置type和subtype实现简单的过渡动画。
type类型:
/* Common transition types. */

CA_EXTERN NSString * const kCATransitionFade
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCATransitionMoveIn
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCATransitionPush
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCATransitionReveal
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);

也可以直接用@“fade”,@"moveIn",@"push",@"reveal"代替,除了这四种还有:@“pageCurl”,@"cube",@"flip"等等。

subtype类型:
/* Common transition subtypes. */

CA_EXTERN NSString * const kCATransitionFromRight
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCATransitionFromLeft
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCATransitionFromTop
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);
CA_EXTERN NSString * const kCATransitionFromBottom
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);

也可以直接使用对应的字符串:@”fromRight“,@"fromLeft",@"fromTop",@"fromBottom"

Present类型过渡动画

设置animated为NO,并将动画添加在self.view.window.layer上
/*** CATransition ***/
//CATransition
- (CATransition *)transition
{
    CATransition *transition = [CATransition animation];
    transition.duration = 1;
    transition.type = @"cube";
    transition.subtype = @"fromRight";
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    return transition;
}

- (void)onClick:(id)sender
{
    ViewController2 *vc2 = [[ViewController2 alloc] init];
    [self presentViewController:vc2 animated:NO completion:nil];
    [self.view.window.layer addAnimation:[self transition] forKey:@"kTransitionAnimation"];
}

Push类型过渡动画

设置animated为NO,并将动画添加在self.view.window.layer或者self.navigationController.view.layer上
/*** CATransition ***/
//CATransition
- (CATransition *)transition
{
    CATransition *transition = [CATransition animation];
    transition.duration = 1;
    transition.type = @"cube";
    transition.subtype = @"fromRight";
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    return transition;
}

- (void)onClick:(id)sender
{
    ViewController2 *vc2 = [[ViewController2 alloc] init];
    [self.navigationController pushViewController:vc2 animated:NO];
    [self.view.window.layer addAnimation:[self transition] forKey:@"kTransitionAnimation"];
    // or [self.navigationController.view.layer addAnimation:[self transition] forKey:kTransitionAnimation];
}


你可能感兴趣的:(CATransition自定义UIViewController过渡动画)