iphone - touchesbegan 与多个 UIImageView 检测到不正确的 UIImageView

标签 iphone objective-c ipad ios drag-and-drop

我在 iPad 上遇到触摸检测问题。

我像这样子类化了 UIImageView:

@interface MyUIImageView : UIImageView 
{
    BOOL _dragActive;
    CGPoint originalLocation;
}

@end

@implementation MyUIImageView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{   
    _dragActive = YES;
    originalLocation = self.center;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    if (_dragActive) 
    {
        UITouch* touch = [touches anyObject];
        self.center = [touch locationInView:self.superview];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    if (_dragActive)
    {
        self.center = originalLocation;
    }

    _dragActive = NO;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{
    if (_dragActive) 
    {
        self.center = originalLocation;
    }

    _dragActive = NO;
}

@end

我在 Controller View 上并排有多个 ImageView(MyUIImageView) 控件,但我的 touchesBegan 只在最前面被调用,尽管它们在 Controller View 上是分开的。

看起来有一个不可见的“检测层”在 ImageView 之外生成。

当我单击并拖动 ImageView 时,被拖动的是位于左侧或右侧的 ImageView ,具体取决于哪个在前面。如果我更改 z 轴,那么行为会重复,但会在前面的图像上重复。

最佳答案

我不清楚你的确切要求。如果你只是想检测特定 imageView(即最左边或最右边的 imageview)的触摸,而不是尝试将该对象分配给 UITouch 而不是使用任何对象 ->< br/> UITouch* touch = [触摸任何对象];
如果可行,请使用这种方式 UITouch* touch = [touches imgViewObject];

关于iphone - touchesbegan 与多个 UIImageView 检测到不正确的 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4325333/

相关文章:

ios - subview 添加到导航栏 ios

iphone - UIWebView 超时 由于互联网不可用而出现问题?

iphone - 无法下载应用程序 - "APP"此时无法下载

iphone - 为什么在 Default.png 期间状态栏仍然显示?

ios - 过滤/搜索 NSMutableDictionary

iphone - iOS : Audio Unit RemoteIO AudioBuffer manipulation (i.麦克风音效)

ios - 当我删除应用程序时推送通知

iphone - Webkit iphone/ipad 的多点触摸问题

objective-c - 使用内存中的 UIWebView 在 PhoneGap 中生成 PDF

用于本地通知的 iOS 后台获取