0

我有一个图像视图,我希望它能够四处移动,然后捏拉它。一切正常,但是当我开始做任何捏动作时,它有点紧张。该位置将在两个手指之间来回跳跃。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    startLocation = [[touches anyObject] locationInView:mouth_handle];
    if([touches count] == 2) {
        NSArray *twoTouches = [touches allObjects];
        UITouch *first = [twoTouches objectAtIndex:0];
        UITouch *second = [twoTouches objectAtIndex:1];
        initialDistance = distanceBetweenPoints([first locationInView:mouth_handle],[second locationInView:mouth_handle]);
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    CGPoint pt = [[touches anyObject] locationInView:mouth_handle];

    CGRect frame = [mouth_handle frame];

    frame.origin.x += pt.x - startLocation.x;
    frame.origin.y += pt.y - startLocation.y;

    frame.origin.x = (frame.origin.x < 58) ? 58 : frame.origin.x;
    frame.origin.x = (frame.origin.x > (260 - mouth_handle.frame.size.width)) ? (260 - mouth_handle.frame.size.width) : frame.origin.x;
    frame.origin.y = (frame.origin.y < 300) ? 300 : frame.origin.y;
    frame.origin.y = (frame.origin.y > 377) ? 377 : frame.origin.y;

    if(frame.origin.x - prevDistanceX > 2 && frame.origin.x - prevDistanceX < -2)
        frame.origin.x = prevDistanceX;
    if(frame.origin.y - prevDistanceY > 2 && frame.origin.y - prevDistanceY < -2)
        frame.origin.y = prevDistanceY;

    prevDistanceX = frame.origin.x;
    prevDistanceY = frame.origin.y;

    CGFloat handleWidth = mouth_handle.frame.size.width;

    if([touches count] == 2) {
        NSArray *twoTouches = [touches allObjects];
        UITouch *first = [twoTouches objectAtIndex:0];
        UITouch *second = [twoTouches objectAtIndex:1];
        CGFloat currentDistance = distanceBetweenPoints([first locationInView:mouth_handle],[second locationInView:mouth_handle]);

        handleWidth = mouth_handle.frame.size.width + (currentDistance - initialDistance);
        handleWidth = (handleWidth < 60) ? 60 : handleWidth;
        handleWidth = (handleWidth > 150) ? 150 : handleWidth;
        if(initialDistance == 0) {
            initialDistance = currentDistance;
        }

        initialDistance = currentDistance;
    }

    mouth_handle.frame = CGRectMake(frame.origin.x, frame.origin.y, handleWidth, 15);
}

关于如何使这更顺畅的任何想法?

4

2 回答 2

0

您可以使用 UIScrollview 执行此操作;它直接内置在其中。

于 2010-03-26T02:27:56.637 回答
0

指间跳跃,是不是说不能区分食指和食指?

我要做的是计算手指之间的角度。所谓角度,我的意思是如果你画一个圆,一个触摸为中心,另一个在圆的边缘,你可以确定第一次触摸正上方的点和第二次触摸之间的角度。如果两个事件之间的角度差大于 0.5Pi(1/4 圆),您可以假设触摸已切换,您可以对此进行纠正。

我希望你明白我的意思。

于 2010-06-09T18:08:20.280 回答