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

0 個評論

要回覆文章請先登錄註冊