前言:在开始讲解这个类之前,我们回顾一下,在处理触摸屏事件中,还有没有别的方法?在前面讲解截图的那篇博文中,我使用过来自于UIResponder的几个方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

优点:可以响应所有的触屏操作

缺点:甄别不同的手势操作非常麻烦,需要自己计算做不同的手势分辨,记录运行轨迹。

于是,在ios3.2之后,苹果另外提供了一种更简便的方式,就是使用UIGestureRecognizer

 

正文:先来看一下类的继承关系吧!ios UIGestureRecognizer

UIGestureRecognizer是个抽象类,我们主要使用它的子类。

ios的命名非常清晰,从名字中我们就可以看出各个子类的主要作用: Tap(点击)、Pinch(捏合,两指向内或向外捏动)、Rotation(旋转)、Swipe(滑动,快速移动,是用于监测滑动的方向的)、Pan (拖移,慢速移动,是用于监测偏移的量的)以及 LongPress(长按)。

一、使用

定义非常简单,关键就是不要忘了设置delegate,以及给要响应的view加上这个手势。至于点击响应,这个各有不同,我们会一一介绍的。对了,不要忘了,在.h文件里加上协议UIGestureRecognizerDelegate。

注意:如果要给UIImageView 添加手势,那么一定要设置这个imageView.userInteractionEnabled = YES;否则不会有任何反应!

//添加Pinch手势
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchRecognizer:)];
pinchRecognizer.delegate = self;
[self.view addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];

1、Pinch手势的响应:

-(void)handlePinchRecognizer:(id)sender
{
    UIPinchGestureRecognizer *ges = (UIPinchGestureRecognizer *)sender;
    int touchCount = ges.numberOfTouches;//zhege ????????????????
    if(touchCount==2){
        if([ges state]== UIGestureRecognizerStateBegan){
            CGPoint p1 = [ges locationOfTouch:0 inView:self.view];
            CGPoint p2 = [ges locationOfTouch:1 inView:self.view];
            distStart = [self distanceFromPointX:p1 ToPointY:p2];
        }else if([ges state]== UIGestureRecognizerStateEnded){
            CGPoint p1 = [ges locationOfTouch:0 inView:self.view];
            CGPoint p2 = [ges locationOfTouch:1 inView:self.view];
            distEnd = [self distanceFromPointX:p1 ToPointY:p2];
            
            scale = distEnd/distStart; //计算缩放比例,如果scale>1,放大;如果scale<1,缩小
            float newWidth = _imageView.bounds.size.width * scale; //计算新的长、宽
            float newHeight = _imageView.bounds.size.height * scale;
            
            if (newWidth < self.view.bounds.size.width) {
                newWidth = self.view.bounds.size.width;
                newHeight = _imageView.bounds.size.height/_imageView.bounds.size.width * newWidth;
            }
            
            if (newHeight < self.view.bounds.size.height) {
                newHeight = self.view.bounds.size.height;
                newWidth = _imageView.bounds.size.width/_imageView.bounds.size.height * newHeight;
            }
            
            [_imageView setFrame:CGRectMake(0, 0, newWidth, newHeight)];
             _scrollView.contentSize = _imageView.bounds.size;
            [_scrollView setFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        }
    }//touchCount
}
//计算两点之间的距离
-(float)distanceFromPointX:(CGPoint) start ToPointY:(CGPoint) end
{
    float distance;
    CGFloat xDist = end.x - start.x;
    CGFloat yDist = end.y - start.y;
    distance = sqrtf((xDist * xDist) +(yDist * yDist));
    return  distance;
}
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-03
  • 2021-11-20
  • 2021-12-23
  • 2022-12-23
  • 2021-05-10
猜你喜欢
  • 2022-12-23
  • 2021-05-31
  • 2022-12-23
  • 2021-06-07
  • 2021-12-03
相关资源
相似解决方案