Cocoa touch 触摸一个UIImageView时禁用其他UIImageView
我有6个UIImageView,比如img1-img6。当我触摸并拖动img1时,它会移动。但当我拖动img1,当它靠近其他UIImageView时,img1停止移动,靠近它的img开始移动。当我快速拖动图像时会发生这种情况,而不是缓慢拖动图像时。而且拖拽也不是那么顺畅( 这是我到目前为止所做的Cocoa touch 触摸一个UIImageView时禁用其他UIImageView,cocoa-touch,uiimageview,Cocoa Touch,Uiimageview,我有6个UIImageView,比如img1-img6。当我触摸并拖动img1时,它会移动。但当我拖动img1,当它靠近其他UIImageView时,img1停止移动,靠近它的img开始移动。当我快速拖动图像时会发生这种情况,而不是缓慢拖动图像时。而且拖拽也不是那么顺畅( 这是我到目前为止所做的 - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [[event allTouche
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if (CGRectContainsPoint([self.firstImg frame], [touch locationInView:nil]))
{
[self.view bringSubviewToFront:self.firstImg];
self.firstImg.center = [touch locationInView:nil];
}
else if (CGRectContainsPoint([self.secondImg frame], [touch locationInView:nil]))
{
[self.view bringSubviewToFront:self.secondImg];
self.secondImg.center = [touch locationInView:nil];
}
else if (CGRectContainsPoint([self.thirdImg frame], [touch locationInView:nil]))
{
[self.view bringSubviewToFront:self.thirdImg];
self.thirdImg.center = [touch locationInView:nil];
}
else if (CGRectContainsPoint([self.fourthImg frame], [touch locationInView:nil]))
{
[self.view bringSubviewToFront:self.fourthImg];
self.fourthImg.center = [touch locationInView:nil];
}
else if (CGRectContainsPoint([self.fifthImg frame], [touch locationInView:nil]))
{
[self.view bringSubviewToFront:self.fifthImg];
self.fifthImg.center = [touch locationInView:nil];
}
else if (CGRectContainsPoint([self.sixthImg frame], [touch locationInView:nil]))
{
[self.view bringSubviewToFront:self.sixthImg];
self.sixthImg.center = [touch locationInView:nil];
}
}
您的实现存在许多问题:
- 您将
作为视图传递到nil
,这意味着如果移动superview或支持界面旋转,您将获得不正确的坐标locationInView:
- 您正在将图像视图的中心设置为触摸位置。因此,当用户第一次触摸视图时,如果触摸未在视图中完全居中,视图将跳到触摸位置的中心。这不是用户期望的行为
- 您总是按固定顺序检查视图,而不是从前到后检查视图。这就是为什么“当它靠近其他UIImageView时,img1停止移动,而靠近它的img开始移动。”如果触摸结束,您的代码将始终移动
,即使用户拖动firstImg
,因为您总是在检查secondImg
之前检查secondImg
firstImg
- 你经常重复你自己。如果你必须写同样的东西两次,你应该考虑把它分解成一个单独的函数或方法。如果你必须写同样的东西三次(或更多),你几乎肯定应该把它分解出来
touchesMoved:withEvent:
和相关方法。相反,在每个图像视图中添加uipangestrerecognizer
:
- (void)makeImageViewDraggable:(UIImageView *)imageView {
imageView.userInteractionEnabled = YES;
UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewPannerDidFire:)];
[imageView addGestureRecognizer:panner];
}
- (void)imageViewPannerDidFire:(UIPanGestureRecognizer *)panner {
UIView *view = panner.view;
[view.superview bringSubviewToFront:view];
CGPoint translation = [panner locationInView:view];
CGPoint center = view.center;
center.x += translation.x;
center.y += translation.y;
view.center = center;
[panner setTranslation:CGPointZero inView:view];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self makeImageViewDraggable:self.firstImg];
[self makeImageViewDraggable:self.secondImg];
[self makeImageViewDraggable:self.thirdImg];
[self makeImageViewDraggable:self.fourthImg];
[self makeImageViewDraggable:self.fifthImg];
[self makeImageViewDraggable:self.sixthImg];
}
拖动imageView1时,请尝试设置其他图像视图的
userInteractionEnabled:NO
。我尝试了…,但没有成功…:(顺便说一句,如果我没弄错的话,你正在尝试实现平移。如果是这样的话,你应该使用uipangestureerecognizer
。是的@Zen你是对的……我使用了rob的代码……它很有效……是的……它很有效……谢谢@rob,但是……你说“你的实现有很多问题”…如果你不介意的话,你能告诉我问题是什么吗…所以,我和任何读过这个问题的人都不会做这些…:)