平滑移动和捏住UIImageView [英] Smooth move and pinch of UIImageView

查看:74
本文介绍了平滑移动和捏住UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像视图,我希望它可以四处移动并捏住以拉伸它.一切正常,但是当我开始做捏动作时有点跳动.该位置将在两个手指之间来回跳跃.

I have an image view that I want to be able to move around, and pinch to stretch it. It's all working, but it's kinda jumpy when I start to do any pinch movements. The position will jump back and forth between the two fingers.

- (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);
}

关于如何使它更平滑的任何想法?

Any thoughts on how to make this smoother?

推荐答案

通过在手指之间跳跃,您是说您无法区分第一根和第二根手指吗?

By jumping between the fingers, do you mean that you can't distinguish between the first and the second finger?

我要做的是计算手指之间的角度.角度是指如果您以一个触摸为中心绘制一个圆,另一个在圆的边缘绘制一个圆,则可以确定第一次触摸上方的点与第二次触摸之间的角度. 如果两个事件之间的角度差大于.5Pi(1/4圈),则可以假定触摸已切换,您可以对此进行校正.

What I would do is calculate the angle between the fingers. By angle I mean that if you would draw a circle with one touch as the center and the other on the edge of the circle, you could determine the angle between the point right above the first touch and the second touch. If the angle difference between two events is bigger than .5Pi (1/4 circle) you can assume the touches got switched and you could correct for that.

希望您能理解我的意思.

I hope you understand what I mean.

这篇关于平滑移动和捏住UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆