廢話不多說上代碼,我們要建立一個UIViewController
typedef enum : NSUInteger {
Fade = 1, //淡入淡出
Push, //推擠
Reveal, //揭開
MoveIn, //覆蓋
Cube, //立方體
SuckEffect, //吮吸
OglFlip, //翻轉
RippleEffect, //波紋
PageCurl, //翻頁
PageUnCurl, //反翻頁
CameraIrisHollowOpen, //開鏡頭
CameraIrisHollowClose, //關鏡頭
CurlDown, //下翻頁
CurlUp, //上翻頁
FlipFromLeft, //左翻轉
FlipFromRight, //右翻轉
} AnimationType;
各種分頁效果。
@interface ViewController (){
NSInteger _count;//翻到的第幾頁
NSInteger _sum;//頁面總數
NSArray *_arr;//設定頁面背景色
}
- (void)viewDidLoad {
[super viewDidLoad];
_sum = 20;
self.view.backgroundColor = [UIColor whiteColor];
_arr = @[[UIColor redColor],[UIColor greenColor],[UIColor blueColor]];
NSLog(@"------------------?------------第%ld頁",(long)_count);
[self rootViewAddUISwipeGestureRecognizer];
}
-(void)rootViewAddUISwipeGestureRecognizer{
UISwipeGestureRecognizer *fromRightSwip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage)];
fromRightSwip.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:fromRightSwip];
UISwipeGestureRecognizer *fromLeftSwip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(forwardPage)];
fromLeftSwip.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:fromLeftSwip];
}
//實現滑動事件
-(void)nextPage{
self.view.backgroundColor = _arr[arc4random()%3];
if (_count<_sum-1) {
NSString *subtypeString;
subtypeString = kCATransitionFromRight;
[self transitionWithType:@"push" WithSubtype:subtypeString ForView:self.view];
_count = _count + 1;
} else {
_count = _sum - 1;
}
NSLog(@"+++++++++++++++++++?+++++++++++++++第%ld頁",(long)_count);
}
-(void)forwardPage{
self.view.backgroundColor = _arr[arc4random()%3];
if (_count>0 ) {
NSString *subtypeString;
subtypeString = kCATransitionFromLeft;
[self transitionWithType:@"push" WithSubtype:subtypeString ForView:self.view];
_count = _count - 1;
} else {
_count = 0;
}
NSLog(@"------------------?------------第%ld頁",(long)_count);
}
#pragma mark CATransition動畫實現
/**
* 動畫效果實現
*/
- (void) transitionWithType:(NSString *) type WithSubtype:(NSString *) subtype ForView : (UIView *) rootview {
//建立CATransition對象
CATransition *animation = [CATransition animation];
animation.duration = 0.5f;
animation.type = type;
if (subtype != nil) {
animation.subtype = subtype;
}
animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
[rootview.layer addAnimation:animation forKey:@"animation"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}