gpt4 book ai didi

ios - 取消对 View 的多次触摸

转载 作者:行者123 更新时间:2023-11-29 03:32:17 25 4
gpt4 key购买 nike

我正在添加 UIPanGestureRecogniser 以在 View 中拖放图像。我想一次只拖动一个图像。但我可以一次拖动两个图像。这不应该发生。

我从早上起就被这个错误困住了。我尝试了我在谷歌上找到的所有方法。

- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setMultipleTouchEnabled:NO];
for(UIImageView *iView in self.movableArray){
if ([iView isMemberOfClass:[UIImageView class]]){
UIPanGestureRecognizer * recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[iView addGestureRecognizer:recognizer];
[iView setUserInteractionEnabled:YES];
recognizer.delegate = self;
}
}
}



- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
[gameView bringSubviewToFront:[(UIPanGestureRecognizer *)recognizer view]];
CGPoint translation = [recognizer translationInView:gameView];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:gameView];
self.dragObjectImageView = (UIImageView*)recognizer.view;


if(recognizer.state == UIGestureRecognizerStateBegan){
int ind = [self.movableArray indexOfObject:self.dragObjectImageView];
for(int i = 0 ; i < [self.movableArray count] ; i ++){
if( i != ind ){
[[self.movableArray objectAtIndex:i] removeGestureRecognizer:recognizer];
}

}

self.homePosition = self.dragObjectImageView.frame;
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint touchPoint = [recognizer locationInView:gameView];
for (UIImageView *iView in self.staticArray) {
if ([iView isMemberOfClass:[UIImageView class]]) {
if (touchPoint.x > iView.frame.origin.x &&
touchPoint.x < iView.frame.origin.x + iView.frame.size.width &&
touchPoint.y > iView.frame.origin.y &&
touchPoint.y < iView.frame.origin.y + iView.frame.size.height)
{
self.dropTargetImageView = iView;
}
}
}

if(self.dragObjectImageView.tag == self.dropTargetImageView.tag){
self.dragObjectImageView.frame = CGRectMake(self.dropTargetImageView.frame.origin.x, self.dropTargetImageView.frame.origin.y + self.dropTargetImageView.frame.size.height/2 - 15, self.dragObjectImageView.frame.size.width, self.dragObjectImageView.frame.size.height);
[self.dragObjectImageView removeGestureRecognizer:recognizer];
}
}else{
self.dragObjectImageView.frame = self.homePosition;

}
}
}

最佳答案

发生这种情况是因为您为每个 imageViews 添加一个 UIPanGestureRecognizer。尝试仅向您的 self.view 添加一个(并将您的 imageViews 设置为 setUserInteractionEnabled:NO,否则它们会捕获触摸)。还放了

recognizer.maximumNumberOfTouches = 1;

在将其添加到 View 之前。现在您所要做的就是测试应在 handlePan 方法中拖动哪个 ImageView 。您应该检查识别器状态,当它变为 UIGestureRecognizerStateBegan 时,您应该保存正在拖动的 imageView。然后,当状态为 UIGestureRecognizerStateChanged 时,只需拖动 View 即可。您可以找出哪个 View 被触摸的最通用方法(因为我不知道您的完整 View 层次结构)是执行以下操作:

NSUInteger index = [self.movableArray indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop)){
UIView* hitTest = (UIView*)obj;
return [hitTest pointInside:firstTouch withEvent:nil];
}];
if ( index != NSNotFound )
self.draggingView = self.moveableArray[index];
else
self.draggingView = nil;

当然,如果 self.draggingView 为零,当用户平移时您将不执行任何操作。

关于ios - 取消对 View 的多次触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19593170/

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