IOS開發之UI手勢

  1. 點擊手勢和雙擊手勢ui



  2. #import "TapViewController.h"spa


  3. @interface TapViewController ().net


  4. @end 3d


  5. @implementation TapViewControllerorm


  6. - (void)viewDidLoad {對象

  7.     [super viewDidLoad];繼承

  8.     // Do any additional setup after loading the view.事件

  9.     

  10.     //點擊手勢:圖片

  11.      //=================單擊===========ip

  12.     //1.建立一個點擊手勢對象

  13.     //UIGestureRecognizer是全部手勢的父類,通常不會直接使用哪一個它

  14.     //而是使用它的子類

  15.     //參數1:響應手勢的對象;

  16.     //參數2:響應的消息

  17.     //功能:發生點擊動做,對象去響應這個手勢

  18.     UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer

  19.                                            alloc]initWithTarget:self action:@selector(tapGesture:)];

  20.     

  21.     //2.添加手勢到指定的視圖上(任何手勢均可以添加到任何繼承自UIView的類的對象上)

  22.     

  23.     [self.view addGestureRecognizer:tapGesture];

  24.     

  25.     

  26.     //=================雙擊===========

  27.     

  28.     //1.建立點擊對象

  29.     UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]

  30.                                          initWithTarget:self action:@selector(doubleTap:)];

  31.     

  32.     //2.設置點擊次數

  33.     doubleTap.numberOfTapsRequired = 2;

  34.     

  35.     

  36.     //4.忽略其餘手勢

  37.     //單擊的時候忽略雙擊;

  38.     [tapGesture requireGestureRecognizerToFail:doubleTap];

  39.     

  40.     //3.將手勢添加到指定的視圖控制器上

  41.     [self.view addGestureRecognizer:doubleTap];

  42.     

  43.     

  44. }


  45. #pragma mark -單擊手勢響應

  46. - (void)tapGesture:(UITapGestureRecognizer *)tap{

  47.     //獲取點擊手勢的座標

  48.     

  49.     CGPoint point = [tap locationInView:self.view];

  50.     

  51.     // NSLog(@"%@",NSStringFromCGPoint(point));

  52.     

  53.     NSLog(@"單擊");

  54. }




  55. #pragma mark - 雙擊事件響應

  56. - (void)doubleTap:(UIGestureRecognizer *)doubleTap{

  57.     

  58.     //獲取點擊手勢的座標

  59.     

  60.     CGPoint point = [doubleTap locationInView:self.view];

  61.     

  62.     //NSLog(@"%@",NSStringFromCGPoint(point));

  63.     

  64.     NSLog(@"雙擊");

  65. }




  66. @end



長按手勢:關鍵在於設置手勢的時間



#import "LongPressViewController.h"


@interface LongPressViewController ()


@end


@implementation LongPressViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    //1.建立長按手勢對象

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];

    

    //3.設置長按時間

    longPress.minimumPressDuration = 1;

    

    //2.將手勢添加到視圖控制器上;

    [self.view addGestureRecognizer:longPress];

    

    

}


#pragma mark -響應長按手勢

//這個方法在手勢的狀態發生改變的時候調用

- (void)longPress : (UILongPressGestureRecognizer *)longPress{

    

    //手勢的狀態:(針對全部的手勢都適用)

//UIGestureRecognizerStateBegan:手勢開始

//UIGestureRecognizerStateChanged:手勢改變

//UIGestureRecognizerStateEnded:手勢結束

    //longPress setState:<#(UIGestureRecognizerState)#>

    if (longPress.state == UIGestureRecognizerStateBegan) {

        NSLog(@"長按");

    }

    

    

    

    

    

  //  NSLog(@"長按");

    

    

}



@end



滑動手勢:關鍵在於設置滑動的方向



#import "SwipeViewController.h"


@interface SwipeViewController ()


@end


@implementation SwipeViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    //1.建立滑動手勢對象

    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]

                                      initWithTarget:self action:@selector(swipGesture:)];

   //3.滑動方向

    //注意:若是想要能夠兩個方向均可以,滑動方向必須添加一個右劃的手勢

    

    [swip setDirection:UISwipeGestureRecognizerDirectionLeft];

    

    //2.將手勢添加到視圖控制器上

    [self.view addGestureRecognizer:swip];

    //=============右劃默認是從左向右滑動==========

    UISwipeGestureRecognizer *swipRight = [[UISwipeGestureRecognizer alloc]

                                           initWithTarget:self action:@selector(swipGesture:)];

   // [swip setDirection:UISwipeGestureRecognizerDirectionRight];

    [self.view addGestureRecognizer:swipRight];

    

    

   

    

    

    

}

#pragma mark - 滑動手勢響應事件

- (void)swipGesture:(UISwipeGestureRecognizer *)swipGesture{

    if (swipGesture.direction == UISwipeGestureRecognizerDirectionRight) {

        

        NSLog(@"右滑回到下一頁");

    }else{

    NSLog(@"左滑到上一頁");

    }

}



@end



拖動手勢



#import "PanViewController.h"


@interface PanViewController ()


@end


@implementation PanViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    //1.建立拖動手勢對象

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]

                                          initWithTarget:self action:@selector(pan:)];



    

    

    //2.添加到視圖

    [self.imageView addGestureRecognizer:panGesture];

    

}


//這個方法會在拖動過程當中時時調用;

#pragma mark - 拖動手勢方法

- (void)pan:(UIPanGestureRecognizer *)pan{


    self.imageView.center = [pan locationInView:self.view];

    

    NSLog(@"拖動");

    

}


@end


旋轉手勢:



#import "RotationViewController.h"


@interface RotationViewController ()


@end


@implementation RotationViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    //1.建立旋轉手勢對象

    

    UIRotationGestureRecognizer *rotatioin = [[UIRotationGestureRecognizer alloc]

                                              initWithTarget:self action:@selector(rotationG:)];

    //2.添加到視圖上

    [self.imageView addGestureRecognizer:rotatioin];

    

}



#pragma mark - 旋轉手勢事件

- (void) rotationG:(UIRotationGestureRecognizer *)ro{

    static CGFloat lastRotation = 0;

   //® self.imageView.center = [rotation locationInView:self.view];

    

    CGFloat triangle = ro.rotation + lastRotation;

    

    self.imageView.transform = CGAffineTransformMakeRotation(triangle);

    

    if (ro.state == UIGestureRecognizerStateEnded) {

        

        lastRotation = ro.rotation + lastRotation;

        

    }

    

    NSLog(@"旋轉");

    

    

    

}



@end


縮放手勢


#import "PinchViewController.h"


@interface PinchViewController ()


@end


@implementation PinchViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    //1.建立pinch手勢對象

   UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]

                                      initWithTarget:self action:@selector(pinchGesture:)];

    

    //2.添加到視圖上

    [self.view addGestureRecognizer:pinch];

    

    

}


- (void)pinchGesture:(UIPinchGestureRecognizer *)pinch{

    

    static CGFloat lastScale =1;

    

    CGFloat scale = pinch.scale;

    //3.經過手勢的縮放比例去改變圖片的縮放形變;

    

    [self.imageView setTransform:CGAffineTransformMakeScale(scale, scale)];

    

    if (pinch.state == UIGestureRecognizerStateEnded) {

        

        lastScale = pinch.scale * lastScale;

    }

    

    

    NSLog(@"縮放");

    

}



@end

相關文章
相關標籤/搜索