gpt4 book ai didi

ios - 将其他 View 拖到 View 上时,还有另一种检测 View 的方法吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:18:33 25 4
gpt4 key购买 nike

我使用平移手势在拖动线中拖动多 View ,类似这样的: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;
}
}

}

我有很多 subview ,所以我不想在 self.view 中循环所有 subview ,我只想检测拖线中的匹配 View 。怎么做?

如果我使用 [gestureRecognizer View ],我只会得到第一个 View 。

提前感谢您的帮助!

最佳答案

如果您只想要更简单的代码,您可以使用 UIView 的 hitTest:withEvent: 方法在触摸位置找到 View :

- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint draggingPoint = [gestureRecognizer locationInView:self.view];
UIView *hitView = [self.view hitTest:draggingPoint withEvent:nil];
if (hitView.superview == self.view)
hitView.center = draggingPoint;
}

这假设 subview 本身没有(交互式) subview 。

关于ios - 将其他 View 拖到 View 上时,还有另一种检测 View 的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14845991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com