iPhone SDK:麻烦拖动UIImageView

时间:2009-07-27 22:09:47

标签: iphone xcode uiimageview draggable touch

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

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

 - (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”的用户交互设置为启用,则图像不再可拖动,但是当它未设置为启用时它就是。

2 个答案:

答案 0 :(得分:0)

你需要至少在你的计时器线程中将它包含在begin / commit动画序列中:

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

/* move pig coordinates here */

[UIView commitAnimations];

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

答案 1 :(得分:0)

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

  

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

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