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

相关文章
相关标签/搜索