【发布时间】:2013-02-13 03:37:30
【问题描述】:
我使用平移手势在拖动线中拖动多视图,例如:iOS - Drag and drop collision detection How to detect when your selected item drags over another subview?
我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
for (UIView *aView in self.view.subviews) {
[self addGestureRecognizersToPiece:aView];
}
}
- (void)addGestureRecognizersToPiece:(UIView *)piece
{
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)];
[piece addGestureRecognizer:panGesture];
}
- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint dragingPoint = [gestureRecognizer locationInView:self.view];
for (UIView *aView in self.view.subviews) {
if (CGRectContainsPoint([aView frame], dragingPoint)) {
aView.center = dragingPoint;
}
}
}
我有这么多子视图,所以我不想循环 self.view 中的所有子视图,我只想检测拖动线中的匹配视图。怎么做?
如果我使用 [gestureRecognizer 视图],我只会得到第一个视图。
提前感谢您的帮助!
【问题讨论】:
-
嗯,正如我所看到的,您想找到正确的视图而不实际找到它。我的意思是,如果确实存在一些内置函数,它会在引擎盖下“循环所有子视图”以找到正确的。您可以做的是将不同的手势识别器附加到不同的视图,这还不够。您也可以尝试使用
touchesBegan:withEvent:和touchesMoved:withEvent:。像在视图的touchesBegan中设置一些像“我被感动”这样的标志,然后(在PanPiece中)只需搜索带有该标志的视图。您仍将循环浏览视图,但效率更高。
标签: ios uiview uigesturerecognizer