Iphone UIImageView的平滑移动和收缩

Iphone UIImageView的平滑移动和收缩,iphone,uiimageview,gesture,Iphone,Uiimageview,Gesture,我有一个图像视图,我希望能够四处移动,并通过捏来拉伸它。这一切都在起作用,但当我开始做任何挤压动作时,感觉有点紧张。该位置将在两个手指之间来回跳跃 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { startLocation = [[touches anyObject] locationInView:mouth_handle]; if([touches count] == 2) {

我有一个图像视图,我希望能够四处移动,并通过捏来拉伸它。这一切都在起作用,但当我开始做任何挤压动作时,感觉有点紧张。该位置将在两个手指之间来回跳跃

- (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);
}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event
{
startLocation=[[触摸任何对象]位置查看:嘴\手柄];
如果([触摸计数]==2){
NSArray*TwoTouchs=[Touchs AllObject];
UITouch*first=[TwoTouchs对象索引:0];
UITouch*second=[TwoTouchs对象索引:1];
initialDistance=点之间的距离([第一个位置查看:嘴把手],[第二个位置查看:嘴把手]);
}
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
CGPoint pt=[[toucheanyobject]locationInView:mouth_handle];
CGRect框架=[口部/手柄框架];
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\u handle.frame.size.width))?(260-mouth\u 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-prevdancex>2&&frame.origin.x-prevdancex<-2)
frame.origin.x=prevDistanceX;
if(frame.origin.y-prevdancey>2&&frame.origin.y-prevdancey<-2)
frame.origin.y=前距离;
prevDistanceX=frame.origin.x;
prevDistanceY=frame.origin.y;
CGFloat handleWidth=口部\手柄.框架.尺寸.宽度;
如果([触摸计数]==2){
NSArray*TwoTouchs=[Touchs AllObject];
UITouch*first=[TwoTouchs对象索引:0];
UITouch*second=[TwoTouchs对象索引:1];
CGFloat currentDistance=点之间的距离([第一个位置查看:嘴把手],[第二个位置查看:嘴把手]);
handleWidth=嘴\把手框架.尺寸.宽度+(当前距离-初始距离);
handleWidth=(handleWidth<60)?60:handleWidth;
handleWidth=(handleWidth>150)?150:handleWidth;
如果(initialDistance==0){
初始距离=当前距离;
}
初始距离=当前距离;
}
mouth_handle.frame=CGRectMake(frame.origin.x,frame.origin.y,handleWidth,15);
}

关于如何使其更平滑,您有什么想法吗?

您可以使用UIScrollview;它直接嵌入其中。

在手指之间跳跃,是不是意味着你无法区分第一个手指和第二个手指

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


我希望你明白我的意思。

我会将图像粘贴到UIWebView中,而不会费心重新发明收缩缩放功能。不要相信@Felix的话。这绝对不是webview的目的。您应该浏览
UIScrollView
。正如我在下面的评论中提到的,我没有使用UIScrollView,因为我只想修改图像的宽度,并且只修改图像的一部分。如果我使用UIScrollView,它将缩放整个图像。Jonathan:看看这个方法:-(void)loadData:(NSData*)data MIMEType:(NSString*)MIMEType textcencodingname:(NSString*)encodingName baseURL:(NSURL*)baseURL我认为UIWebView非常适合这种情况。当然,我不知道Jacob还需要完成什么,但是如果它像一个具有收缩缩放功能的图像一样简单,那么这可以用大约5行代码来完成。我不想只为我的图像提供缩放功能。我只想能够调整它的宽度。我也不想仅仅变换宽度,因为我不想扭曲图像的末端。图像有一个长而窄的部分,末端有两个球。当我挤压时,我不想缩放整个图像,我只想拉伸图像的宽度,这样做不会扭曲末端的球。所以,我使用的是一个LeftCapWidth的可拉伸图像。我想要UIScrollView的平滑度,但不想缩放整个图像。