我在 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/