iPhone SDK:麻烦拖动UIImageView [英] iPhone SDK:Trouble Dragging a UIImageView

查看:107
本文介绍了iPhone SDK:麻烦拖动UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中围绕iphone屏幕拖动UIImageView。

I am trying to drag a UIImageView around the iphone screen in my app.

目前我设置的拖动功能很好,拖动图像会在屏幕上移动它,麻烦的是你不必拖动图像视图要移动它,您还可以拖动屏幕上的任何位置,它将移动图像。
我是这个平台的新手,所以我无法真正思考如何解决这个问题。
我当前拖动图片的代码是:

Currently The drag functionality I have set up is fine and dragging the image does move it around the screen, the trouble is that you don't have to drag the image view to move it, you can also drag anywhere on the screen and it will move the image. I am new to this platform so I can't really think about what to do to solve this issue. My current code to drag the image is:

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


}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    CGPoint pt = [[touches anyObject] locationInView:pig];
    CGRect frame = [pig frame];
    frame.origin.x += pt.x - startLocation.x;
    frame.origin.y += pt.y - startLocation.y;
    [pig setFrame: frame];
}

任何帮助表示感谢。顺便说一下,猪是UIImageView。
另外我注意到如果我将图像视图pig的用户交互设置为启用,则图像不再可拖动,但是当它未设置为启用时它是。

Any help appreciated. By the way pig is the UIImageView. Also I have noticed that if I set the user interaction of the Image View "pig" to enabled the image is no longer drag able,but when it isn't set to enabled It is.

推荐答案

您可以尝试子类化UIImageView并实现 touchesBegan: touchesMoved :直接在它上面。然后在InterfaceBuilder中选择UIImageView的子类。

You can try to subclass UIImageView and implement touchesBegan: and touchesMoved: on it directly. Then in InterfaceBuilder select your subclass for the UIImageView.


alt text http://img.skitch.com/20090728-xetretcfubtcj7ujh1yc8165wj.jpg

哦是的,现在你会需要再次设置用户交互...

Oh yes, now you will need to set the user interaction again...

这篇关于iPhone SDK:麻烦拖动UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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