【问题标题】:iPhone SDK:Trouble Dragging a UIImageViewiPhone SDK:拖拽 UIImageView 时出现问题
【发布时间】:2010-11-14 12:36:16
【问题描述】:

我正在尝试在我的应用程序中将 UIImageView 拖到 iPhone 屏幕上。

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

 - (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。 我还注意到,如果我将图像视图“猪”的用户交互设置为启用,则图像不再可以拖动,但是当它未设置为启用时它是。

【问题讨论】:

    标签: iphone xcode uiimageview draggable touch


    【解决方案1】:

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

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.02]  /* 0.02 seconds suration*/
    
    /* move pig coordinates here */
    
    [UIView commitAnimations];
    

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • self.userInteractionEnabled = YES;...总是一样的,而且总是我做错了!感谢 epatel,太棒了:-)
      【解决方案3】:

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

      【讨论】:

      • 请注意 link-only answers 是不鼓励的,所以答案应该是寻找解决方案的终点(与另一个中途停留的参考相比,随着时间的推移往往会变得陈旧)。请考虑在此处添加独立的概要,保留链接作为参考
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2022-09-23
      相关资源
      最近更新 更多