Iphone 触摸从多个UIImageView开始检测到不正确的UIImageView
我在iPad上的触摸检测有问题 我将UIImageView分为以下子类:Iphone 触摸从多个UIImageView开始检测到不正确的UIImageView,iphone,objective-c,ipad,ios,drag-and-drop,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 { _dra
@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
我在控制器视图上并排有多个ImageView(MyUIImageView)控件,但我的ToucheSStart仅在最前面被调用,尽管它们在控制器视图上是分开的
看起来有一个不可见的“检测层”在ImageView之外生成
当我点击并拖动一个ImageView时,被拖动的是左边还是右边,取决于前面哪个。如果我更改z轴,则该行为会重复,但会出现在前面的图像上。我不清楚您的确切要求。如果您只是想检测特定图像视图(即最左侧或最右侧的图像视图)的触摸,请尝试将该对象分配到UITouch,而不是使用任何对象->
UITouch*touch=[触摸任何对象]
如果工作正常,请使用此方法:UITouch*touch=[touch imgViewObject] >对于所描述的情况,应该考虑让控制器(其视图包含所有的IVieview)处理触摸事件。我认为您所描述的“检测层”实际上是您的imageview在触摸事件流开始时占据的屏幕区域。我相信,动态重新定位imageview(拖动)确实由父视图更好地处理
这种方法的一个好处是,从superview的角度来看,您可以更轻松地处理重新排列同级视图、在父视图边界定义行为、处理同级碰撞/重叠等问题。最后,我解决了这个问题。我试图移动的UIImageViews的背景比图像大。这会干扰兄弟图像的触摸检测 在Interface builder中,如果我设置背景色,它会按预期工作,但在运行时它会变大。我通过编程将背景色设置为灰色注意到了这一点
在Interface Builder中更改UIImageViews的自动调整大小解决了这个问题。我最初的方法与您描述的方法相同,但我遇到了完全相同的问题。我将实施您建议的更改,看看它是如何工作的,谢谢。