touchesMoved尝试用两根手指调整视图大小

时间:2014-08-05 16:03:09

标签: objective-c ios7 uiview resize touchesmoved

当我的触摸点位于左上角和右下角时,以下代码可以正常工作。

但如果我的手指处于任何其他位置,如果我在保持与iPad屏幕的接触时停止拖动手指,则视图大小从零宽度/高度来回弹出到所需的宽度/高度,因为触摸点似乎来回切换。

我该如何避免这个问题?

if([fingers count]==2)
    {
        touchPoint1 = [[fingers objectAtIndex:0] locationInView: self.view];
        touchPoint2 = [[fingers objectAtIndex:1] locationInView: self.view];
    }else{
        touchPoint1 = self.dragObject.frame.origin;
        touchPoint2 = [[fingers objectAtIndex:0] locationInView:self.view];
    }
        float rightXposition;
        float leftXposition;
        float topYposition;
        float bottomYposition;
        float wid;
        float hei;

        if(touchPoint1.x>touchPoint2.x)
        {
            leftXposition = touchPoint2.x;
            rightXposition = touchPoint1.x;
        }else{
            leftXposition = touchPoint1.x;
            rightXposition = touchPoint2.x;
        }
        if(touchPoint1.y>touchPoint2.y){
            topYposition = touchPoint2.y;
            bottomYposition = touchPoint1.y;
        }else{
            topYposition = touchPoint1.y;
            bottomYposition = touchPoint2.y;
        }
        wid = rightXposition - leftXposition;
        hei = bottomYposition - topYposition;
        newDragObjectFrame = CGRectMake(leftXposition,topYposition,wid,hei);
        if(newDragObjectFrame.size.height > 0 && newDragObjectFrame.size.width > 0 && self.touchesCount > 0)
         {
             self.dragObject.frame = newDragObjectFrame;
         }

1 个答案:

答案 0 :(得分:0)

我想出是否用最后一个if语句执行此操作:

if(newDragObjectFrame.size.height> 20&& newDragObjectFrame.size.width> 20&& self.touchesCount> 0)

它有效。

问题是,只要我的手指将几个像素卷入正区域,他们就会交换touchPoints。添加20像素的缓冲区可以防止出现此问题。