带我飞5程序员IOS开发

iOS开发之手势识别

2016-03-05  本文已影响809人  李小南

如果这篇文章帮助到了您,希望您能点击一下喜欢或者关注我,有什么想法大家也可用一起交流,进步!

手势识别

UIGestureRecognizer手势识别器

手势使用的方法

UITapGestureRecognizer(点按手势)

实现一张图片的左边可用点击,右边不能点击

添加点按手势
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(长按)

添加长按手势
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(轻扫手势)

添加轻扫手势
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(拖拽手势)

  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 = CGAffineTransformTranslate(self.imageV.transform, transP.x, transP.y);

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

UIPinchGestureRecognizer(捏合手势,用于缩放)与UIRotationGestureRecognizer(旋转手势)

添加捏合手势
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]; 
}

是否允许支持多个手势----代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
上一篇 下一篇

猜你喜欢

热点阅读