xcode iPhone SDK:拖动 UIImageView 时遇到问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1190935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:42:21  来源:igfitidea点击:

iPhone SDK:Trouble Dragging a UIImageView

iphonexcodeuiimageviewdraggabletouch

提问by Sam

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

我正在尝试在我的应用程序中的 iphone 屏幕周围拖动 UIImageView。

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];
}

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.

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

采纳答案by epatel

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

您可以尝试继承 UIImageView 并直接在其上实现touchesBegan:touchesMoved:。然后在 InterfaceBuilder 中为 UIImageView 选择您的子类。

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

替代文字 http://img.skitch.com/20090728-xetretcfubtcj7ujh1yc8165wj.jpg

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

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

回答by Istvan

You need to enclose it in begin / commit Animation sequences at least in your timer thread:

您至少需要在计时器线程中将其包含在开始/提交动画序列中:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.02]  /* 0.02 seconds suration*/

/* move pig coordinates here */

[UIView commitAnimations];

Save the new movement in touchesMoved and then process them in the timer thread.

将新动作保存在 touchesMoved 中,然后在计时器线程中处理它们。

回答by Hamiseixas

You can use an UIView category to make it draggable and define the draggable area: https://github.com/rtoshiro/TXDragAndDrop

您可以使用 UIView 类别使其可拖动并定义可拖动区域:https: //github.com/rtoshiro/TXDragAndDrop