iOS-手势

手势使用方法

1.创建手势
2.添加手势
3.实现手势方法

添加点按手势

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
//手势也可以设置代理
tap.delegate = self;
//添加手势
[self.imageV addGestureRecognizer:tap];

代理方法:
是否允许接收手指

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    让图片的左边不可以点击,
    获取当前手指所在的点.是在图片的左边还是在图片的右边.
    CGPoint curP = [touch locationInView:self.imageV];
    if (curP.x > self.imageV.bounds.size.width * 0.5) {
        在图片的右侧
        return YES;
    }else{
        在图片的左侧
        return NO;
    }
    return YES;
}

添加长按手势

UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longP:)];  
[self.imageV addGestureRecognizer:longP];

//当长按时调用.
//这个方法会调用很多次, 当手指长按在上面不松,来回移动时,会持续调用.
//所以要判断它的状态.
- (void)longP:(UILongPressGestureRecognizer *)longP{
    if(longP.state == UIGestureRecognizerStateBegan){
        NSLog(@"开始长按");
    }else if(longP.state == UIGestureRecognizerStateChanged){
        NSLog(@"长按时手指移动");
    }else if(longP.state == UIGestureRecognizerStateEnded){
        NSLog(@"手指离开屏幕");
    }
}

添加轻扫手势

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
 //轻扫手势默认是向右边称轻扫
 //可以设置轻扫的方法.
 //一个轻扫手势只能设置一个方法的轻扫.想要让它有多个方向的手势,必须得要设置的
  swipe.direction =  UISwipeGestureRecognizerDirectionLeft;
  [self.imageV addGestureRecognizer:swipe];
   
//添加轻扫手势
UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
//轻扫手势默认是向右边称轻扫
//可以设置轻扫的方法.
//一个轻扫手势只能设置一个方法的轻扫.想要让它有多个方向的手势,必须得要设置的
  swipe2.direction =  UISwipeGestureRecognizerDirectionUp;
  [self.imageV addGestureRecognizer:swipe2];


- (void)swipe:(UISwipeGestureRecognizer *)swipe{
     //判断的轻扫的方向
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"向左轻扫");
    }else if(swipe.direction == UISwipeGestureRecognizerDirectionUp){
        NSLog(@"向上轻扫");
    }
}

添加平移手势

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.imageV addGestureRecognizer:pan];

//当手指拖动时调用
- (void)pan:(UIPanGestureRecognizer *)pan{
   
    //拖动手势也有状态
    if(pan.state == UIGestureRecognizerStateBegan){
         // 开始拖动  
    }else if(pan.state == UIGestureRecognizerStateChanged){
       // 可能获取不当前手指移动的举例
        //是相对于最原始的点
        CGPoint transP = [pan translationInView:self.imageV];
       
        //会清空上一次的形变
        self.imageV.transform = CGAffineTransformMakeTranslation(transP.x,transP.y);
        self.imageV.transform = CGAffineTransformTranslate(self.imageV.transform, transP.x, transP.y);

       // 复位,让它相对于上一次.
        [pan setTranslation:CGPointZero inView:self.imageV];
    }else if(pan.state == UIGestureRecognizerStateEnded){
       // 结束拖动       
    }
}

添加捏合手势

UIPinchGestureRecognizer *pinGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinGes:)];
//设置代理使其能够同时支持多个手势
pinGes.delegate = self;
[self.imageV addGestureRecognizer:pinGes];


//旋转时调用
- (void)pinGes:(UIPinchGestureRecognizer *)pin{
    self.imageV.transform = CGAffineTransformScale(self.imageV.transform, pin.scale, pin.scale);
    //复位
    [pin setScale:1];
   
}

添加旋转手势

 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
 //设置代理使其能够同时支持多个手势
 rotation.delegate = self;
 [self.imageV addGestureRecognizer:rotation];


//当手指开始旋转时调用.
- (void)rotation:(UIRotationGestureRecognizer *)rotation{
    self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, rotation.rotation);
    //复位.
    [rotation setRotation:0];
}

你可能感兴趣的:(iOS-手势)