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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值