分享

IOS 多视图UIView左右来回滑动切换效果!

 quasiceo 2017-03-19

IOS 多视图UIView左右来回滑动切换效果!

多视图页面左右来回滑动切换效果!

//

//  ViewController.h

//  demoA0

//

//  Created by yuhang on 13-2-18.

//  Copyright (c) 2013 yuhang. All rights reserved.

//


#import <UIKit/UIKit.h>


#define ViewNumber 10


@interface ViewController : UIViewController

{  

    UIView *dwView;

    UIButton *dwBtn;

    NSMutableArray *viewArray,*titleArray;

}


@property ( nonatomic , assign ) NSInteger dwNum;



@end


///////////////////////////////////////////////////////////////////////////////////////////////////////////////


//

//  ViewController.m

//  demoA0

//

//  Created by yuhang on 13-2-18.

//  Copyright (c) 2013 yuhang. All rights reserved.

//


#import "ViewController.h"

#import <QuartzCore/CATransaction.h>


@interface ViewController ()


@end


@implementation ViewController

@synthesize dwNum;


- ( void )viewDidLoad

{

    

    dwNum = 0 ;

    //button 标答

    titleArray = [[ NSMutableArray alloc ] initWithObjects : @"B0" , @"B1" , @"B2" , @"B3" , @"B4" , @"B5" , @"B6" , @"B7" , @"B8" , @"B9" , nil ];

    viewArray =[[ NSMutableArray alloc ] init ];

    // 动态生成视图并添加按钮

    for ( int i= 0 ; i< ViewNumber ; i++) {

        dwView =[[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , 320 , 460 )];

        dwView . tag =i;

        dwBtn =[ UIButton buttonWithType : UIButtonTypeRoundedRect ];

        dwBtn . frame = CGRectMake ( 100 , 100 , 55 , 55 );

        dwBtn . tag =i;

        [ dwBtn setTitle :[ titleArray objectAtIndex :i] forState : UIControlStateNormal ];        

        [ dwBtn addTarget : self action : @selector (buttonClick:) forControlEvents : UIControlEventTouchUpInside ];

        [ dwView addSubview : dwBtn ];

        [ viewArray addObject : dwView ];        

        [ self . view addSubview : dwView ];

    }

    [ self . view bringSubviewToFront :[ viewArray objectAtIndex : 0

    UISwipeGestureRecognizer *recognizer;

    recognizer = [[ UISwipeGestureRecognizer alloc ] initWithTarget : self action : @selector (handleSwipeFrom:)];

    [recognizer setDirection :( UISwipeGestureRecognizerDirectionRight )];

    [[ self view ] addGestureRecognizer :recognizer];

    [recognizer release ];

    

    recognizer = [[ UISwipeGestureRecognizer alloc ] initWithTarget : self action : @selector (handleSwipeFrom:)];

    [recognizer setDirection :( UISwipeGestureRecognizerDirectionLeft )];

    [[ self view ] addGestureRecognizer :recognizer];

    [recognizer release ];

    

    [ super viewDidLoad ];

}


// 手势

- ( IBAction )handleSwipeFrom:( UISwipeGestureRecognizer *)sender{

       if (sender. direction == UISwipeGestureRecognizerDirectionLeft )

    {

        if ( dwNum < 0 )

            dwNum = 0 ;

        else if ( dwNum != 0 )

            dwNum --;

        [ UIView beginAnimations : nil context : nil ];

        // 持续时间

        [ UIView setAnimationDuration : 1.0 ];

        // 在出动画的时候减缓速度

        [ UIView setAnimationCurve : UIViewAnimationCurveEaseOut ];

        // 添加动画开始及结束的代理

        [ UIView setAnimationDelegate : self ];

        [ UIView setAnimationWillStartSelector : @selector (begin)];

        [ UIView setAnimationDidStopSelector : @selector (stopAni)];

        // 动画效果

        [ UIView setAnimationTransition : UIViewAnimationTransitionCurlUp forView : self . view cache : YES ];

        //View 切换

        for ( int i= 0 ; i< 10 ; i++) {

            if ( dwNum !=i)

                [[ viewArray objectAtIndex :i] removeFromSuperview ];

            else

                [ self . view insertSubview :[ viewArray objectAtIndex :i] atIndex : 0 ];

        }      

        

        [ UIView commitAnimations ];

    }

    else if (sender. direction = UISwipeGestureRecognizerDirectionRight )

    {

        if ( dwNum > 9 )

            dwNum = 9 ;

        else if ( dwNum != 9 )

            dwNum ++;

        [ UIView beginAnimations : nil context : nil ];

        // 持续时间

        [ UIView setAnimationDuration : 1.0 ];

        // 在出动画的时候减缓速度

        [ UIView setAnimationCurve : UIViewAnimationCurveEaseOut ];

        // 添加动画开始及结束的代理

        [ UIView setAnimationDelegate : self ];

        [ UIView setAnimationWillStartSelector : @selector (begin)];

        [ UIView setAnimationDidStopSelector : @selector (stopAni)];

        // 动画效果

        [ UIView setAnimationTransition : UIViewAnimationTransitionCurlUp forView : self . view cache : YES ];

        //View 切换

        for ( int i= 0 ; i< 10 ; i++) {

            if ( dwNum !=i)

                [[ viewArray objectAtIndex :i] removeFromSuperview ];

            else

                [ self . view insertSubview :[ viewArray objectAtIndex :i] atIndex : 0 ];            

        }       

        [ UIView commitAnimations ];        

    }   

}



  

分享

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多