【发布时间】:2010-12-01 14:26:42
【问题描述】:
我在 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
我在控制器视图上并排有多个 ImageView(MyUIImageView) 控件,但我的 touchesBegan 仅在最前面被调用,尽管它们在控制器视图上是分开的。
看起来有一个不可见的“检测层”在 ImageViews 之外产生。
当我单击并拖动一个 ImageView 时,被拖动的是左边或右边的那个,具体取决于哪个在前面。如果我更改 z 轴,则行为会重复,但在前面的图像上。
【问题讨论】:
标签: iphone objective-c ipad ios drag-and-drop