iOS 簡單實作類別似於小說閱讀滑動分頁效果

來源:互聯網
上載者:User

廢話不多說上代碼,我們要建立一個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];

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.