iOS页面切换动画效果

//
//  LAXAnimation.h
//  LAX_OCAnimation
//
//  Created by 冰凉的枷锁 on 2016/11/18.
//  Copyright © 2016年 liuaoxiang. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface LAXAnimation : NSObject

@property (nonatomic, assign) NSTimer *timer;

+ (CATransition *)defaultAnimationWithDuration:(NSTimeInterval)duration target:(UIView *)view;
+ (CATransition *)animationWithDuration:(NSTimeInterval)duration target:(UIView *)view delegate:(id)delegate type:(NSInteger)type subtype:(NSInteger)subtype;

+ (void)addShakeGestureRecognizer:(UIView *)view;
+ (void)stopShakeWithTouchView:(UIView *)contentView shakeView:(UIView *)shakeView touches:(NSSet<UITouch *> *)touches event:(UIEvent *)event;

@end
//
//  LAXAnimation.m
//  LAX_OCAnimation
//
//  Created by 冰凉的枷锁 on 2016/11/18.
//  Copyright © 2016年 liuaoxiang. All rights reserved.
//

#import "LAXAnimation.h"

@implementation LAXAnimation

+ (CATransition *)defaultAnimationWithDuration:(NSTimeInterval)duration target:(UIView *)view {

    //默认动画
    CATransition *defaultAnimation = [CATransition animation];
    defaultAnimation.timingFunction = UIViewAnimationCurveEaseInOut;
    defaultAnimation.duration = duration;
    defaultAnimation.type = @"pageCurl"; 
    defaultAnimation.subtype = kCATransitionFromBottom;

    //defaultAnimation.delegate = self;
    [view.layer addAnimation:defaultAnimation forKey:@"animation"];
    return defaultAnimation;

}

+ (CATransition *)animationWithDuration:(NSTimeInterval)duration target:(UIView *)view delegate:(nullable id)delegate type:(NSInteger)type subtype:(NSInteger)subtype {

    //NSArray *titleArr = @[@"淡化", @"推挤", @"揭开", @"覆盖", @"波纹", @"吸收", @"翻转", @"立方体", @"翻页", @"反翻页", @"镜头开", @"镜头关"];
    NSArray *typeArr = @[kCATransitionFade, kCATransitionPush, kCATransitionReveal, kCATransitionMoveIn, @"rippleEffect", @"suckEffect", @"oglFlip", @"cube", @"pageCurl", @"pageUnCurl", @"cameraIrisHollowOpen", @"cameraIrisHollowClose"];
    NSArray *subtypeArr = @[kCATransitionFromLeft, kCATransitionFromBottom, kCATransitionFromRight, kCATransitionFromTop];

    CATransition *defaultAnimation = [CATransition animation];
    defaultAnimation.delegate = delegate;
    defaultAnimation.duration = duration;
    defaultAnimation.timingFunction = UIViewAnimationCurveEaseInOut;

    if (type >= 0 && type < 12) {
        defaultAnimation.type = typeArr[type];
    }
    if (type >= 0 && type < 4) {
        defaultAnimation.subtype = subtypeArr[subtype];
    }

    [view.layer addAnimation:defaultAnimation forKey:@"animation"];
    return defaultAnimation;

}

+ (void)addShakeGestureRecognizer:(UIView *)view {

    //向view添加长按手势
    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
    longPress.minimumPressDuration = 1;

    [view addGestureRecognizer:longPress];
    view.userInteractionEnabled = YES;

}

+ (void)startShake:(UIView *)view {

    //创建动画对象,绕Z轴旋转
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //设置属性,周期时长
    [animation setDuration:0.08];
    //抖动角度
    animation.fromValue = @(-M_1_PI/2);
    animation.toValue = @(M_1_PI/2);
    //重复次数,无限大
    animation.repeatCount = HUGE_VAL;
    //恢复原样
    animation.autoreverses = YES;
    //锚点设置为图片中心,绕中心抖动
    view.layer.anchorPoint = CGPointMake(0.5, 0.5);

    [view.layer addAnimation:animation forKey:@"rotation"];

}

+ (void)gestureAction:(UILongPressGestureRecognizer *)sender {

    CABasicAnimation *animation = (CABasicAnimation *)[sender.view.layer animationForKey:@"rotation"];

    if (animation == nil) {
        [self startShake:sender.view];
    }else {
        sender.view.layer.speed = 1.0;
    }

}

+ (void)stopShakeWithTouchView:(UIView *)contentView shakeView:(UIView *)shakeView touches:(NSSet<UITouch *> *)touches event:(UIEvent *)event {

    //判断touch点是否在imageView内,在的话,仍然抖动,否则停止抖动
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:contentView];
    CGPoint p = [contentView convertPoint:point toView:shakeView];
    //NSLog(@"%d,%d", p.x, p.y);

    if (![shakeView pointInside:p withEvent:event]) {
        shakeView.layer.speed = 0.0;
    }

}

@end
func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval { return 1.0 } func animateTransition(transitionContext: UIViewControllerContextTransitioning) { let containerView = transitionContext.containerView() let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey) let toView = transitionContext.viewForKey(UITransitionContextToViewKey) //toView?.frame = containerView.frame containerView.addSubview(toView!) toView?.layer.transform = CATransform3DIdentity toView?.alpha = 0 fromView?.layer.transform = CATransform3DMakeTranslation(-188, 0, 0) if self.presenting { UIView.animateWithDuration(1.0, animations: { () -> Void in // fromView?.layer.transform = CATransform3DMakeTranslation(-160, 0, 0) // fromView?.layer.transform = CATransform3DMakeRotation(CGFloat(M_PI_2), 10, 0, 10) fromView?.layer.transform = CATransform3DRotate(CATransform3DMakeTranslation(-188, 0, 0), CGFloat(M_PI_2), 0, 10, 0) toView?.alpha = 1.0 }, completion: { (finished) -> Void in fromView?.layer.transform = CATransform3DIdentity transitionContext.completeTransition(true) }) // if self.presenting { // fromView?.addAnimation(presenting) // UIView.animateWithDuration(1.0, animations: { () -> Void in // toView?.alpha = 1.0 // }, completion: { (finished) -> Void in // transitionContext.completeTransition(true) // }) // UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in // var trans = CGAffineTransformMake(0.1, CGFloat(M_PI_2), CGFloat(M_PI_2), 0.1, -width/2, height/2) // var trans1 = CGAffineTransformMakeScale(0.1, 0.1) // var trans2 = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) // var trans3 = CGAffineTransformMakeTranslation(-width/2, height/2) // fromView?.transform = trans2 // fromView?.alpha = 0 // toView?.alpha = 1 // }) { (finished) -> Void in // if finished { // fromView?.transform = CGAffineTransformIdentity // transitionContext.completeTransition(true) // } // } // } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值