简体   繁体   English

在UIImageView上测试捏(不是捏和缩放-只是测试捏)

[英]Test for a pinch on UIImageView (not pinch and zoom - just test for pinch)

This isn't another one of "those" questions, don't worry. 不用担心,这不是“那些”问题中的另一个。

Basically I want to test for a pinch and then run an animation. 基本上,我想测试一下捏后再运行动画。 Here is the code I currently have: 这是我目前拥有的代码:

// test for a pinch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSSet *allTouches = [event allTouches];
    if([allTouches count] == 2){
        //there are two touches...
        UITouch *firstTouch = [[allTouches allObjects] objectAtIndex:0];
        UITouch *secondTouch = [[allTouches allObjects] objectAtIndex:1];
        CGPoint firstPoint = [firstTouch locationInView:self.superview];
        CGPoint secondPoint = [secondTouch locationInView:self.superview];

        int X1 = firstPoint.x;
        int Y1 = firstPoint.y;
        int X2 = secondPoint.x;
        int Y2 = secondPoint.y;

        // lets work out the distance:
        // sqrt( (x2-x1)^2 + (y2-y1)^2 );
        preDistance = sqrtf( (float)pow((X2-X1),2) + (float)pow((Y2-Y1),2) );

    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSSet *allTouches = [event allTouches];
    if([allTouches count] == 2){
        //there are two touches...
        UITouch *firstTouch = [[allTouches allObjects] objectAtIndex:0];
        UITouch *secondTouch = [[allTouches allObjects] objectAtIndex:1];
        CGPoint firstPoint = [firstTouch locationInView:self.superview];
        CGPoint secondPoint = [secondTouch locationInView:self.superview];

        int X1 = firstPoint.x;
        int Y1 = firstPoint.y;
        int X2 = secondPoint.x;
        int Y2 = secondPoint.y;

        // lets work out the distance:
        // sqrt( (x2-x1)^2 + (y2-y1)^2 );
        postDistance = sqrtf( (float)pow((X2-X1),2) + (float)pow((Y2-Y1),2) );

        // lets test now
        // if the post distance is LESS than the pre distance then
        // there has been a pinch INWARDS
        if(preDistance > postDistance){
            NSLog(@"Pinch INWARDS");
            // so we need to move it back to its small frame
            if(CGRectEqualToRect(self.frame, largeFrame) && !CGRectIsEmpty(self.smallFrame)){
                // we can go inwards
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.5];
                self.frame = smallFrame;
                [UIView commitAnimations];
            }
        }
        else {
            NSLog(@"Pinch OUTWARDS");
            // so we need to move it back to its small frame
            if(!CGRectEqualToRect(self.frame, largeFrame)){
                // we can go outwards
                // set org frame
                smallFrame = self.frame; // so we can go back later
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.5];
                self.frame = largeFrame;
                [UIView commitAnimations];
            }
        }

    }
}

All it does is measure the distance between the two touches if there is a multi touch, then it measures the distance again when the touches end. 它所做的只是在存在多点触摸时测量两次触摸之间的距离,然后在触摸结束时再次测量距离。 If the difference between the two is positive then it was an outwards pinch, and if it was negative it was an inwards pinch. 如果两者之间的差异为正,则表示向外捏,如果为负,则为向内捏。

Depending on the direction of the pinch the image view will scale bigger or smaller to and from set CGRects (this is why I don't want the "normal" pinch and zoom functionality). 根据缩放的方向,图像视图会在设置的 CGRects之间来回缩放(这就是为什么我不希望使用“正常”的缩放和缩放功能)。 I just want it to be a gesture to scale an image up and down again. 我只是希望它是一种可以上下缩放图像的手势。

However, this isn't working very well... it doesn't always pick up pinches. 但是,这种方法不能很好地工作……它并不总是会遇到麻烦。 I don't know if this is because the view its in is also detecting touches (only single pinches), but still... 我不知道这是否是因为其视图也正在检测触摸(仅单捏),但是仍然...

Anyway, I was wondering if anyone has a better way of detecting pinches? 无论如何,我想知道是否有人有更好的检测捏的方法? I've currently subclassed the UIImageView to create my ImageController class which has these touch methods in them. 我目前已将UIImageView子类化为创建我的ImageController类,该类中包含这些touch方法。 All I need to know is a) they are touching this image and b) which direction they've pinched in. 我需要知道的是a)他们正在触摸这张图片,b)他们往哪个方向捏。

Thanks 谢谢

Check out UIPinchGestureRecognizer in the documentation. 请查阅文档中的UIPinchGestureRecognizer I haven't used it, but seems like it's just what you need. 我没有用过,但似乎正是您所需要的。

Anyway, I was wondering if anyone has a better way of detecting pinches? 无论如何,我想知道是否有人有更好的检测捏的方法?

If you're targeting iOS 3.2 and upwards you can use gesture recognizers - there's no good reason not to. 如果您的目标是iOS 3.2及更高版本,则可以使用手势识别器-没有充分的理由不这样做。 If you need to target pre 3.2, then your above approach (or varients therein, using touchesBegin, etc) is the only way, so it's just tweaking the algorithm. 如果您需要定位3.2之前的版本,那么您的上述方法(或其中的变量,使用touchesBegin等)是唯一的方法,因此它只是在调整算法。

If you can , I'd strongly recommend using UIPinchGestureRecognizer . 如果可以的话 ,我强烈建议您使用UIPinchGestureRecognizer It will make your life considerably easier, as it handles all the details. 处理所有细节,将使您的生活变得更加轻松。 If you want to detect both the scale and direction of the pinch you can combine UIPinchGestureRecognizer with a UIRotationGestureRecognizer - the former will just give you the scale of a pinch, not the relative angle. 如果要同时检测捏的比例和方向,可以将UIPinchGestureRecognizerUIRotationGestureRecognizer结合使用-前者仅会提供捏的比例,而不是相对角度。

If you can live without 3.1.x devices I would recommend that you use the UIPinchGestureRecognizer . 如果没有3.1.x设备,我建议您使用UIPinchGestureRecognizer It's very simple to use, but the problem is that you're only going to be able to run it on iOS devices running 3.2 and upwards. 它非常简单易用,但是问题在于您将只能在运行3.2及更高版本的iOS设备上运行它。

For some reason you are using locationInView to identify the position of the touches. 由于某些原因,您正在使用locationInView标识触摸的位置。 Have you tried identifying the touches location in the current view (the actual image view)? 您是否尝试过在当前视图(实际图像视图)中标识触摸位置? Calling superview means that you will get the position of the touches in the view that contains your image view. 调用superview意味着您将在包含图像视图的视图中获得触摸的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM