iOS 简单实现类似于小说阅读滑动分页效果

废话不多说上代码,我们要创建一个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];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王 哪跑!!!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值