我正在尝试让多个UIImageViews在拖动时移动。我最初把所有动作都放在TouchesBegan下。但是,当我拖动单个对象时,它们都会消失,只有我拖动的对象会移动。
如何拖动每个图像拥有自己的术语(我拖动第一个图像,只移动第一个图像等)?
答案 0 :(得分:1)
我不确定这是不是你要找的,但是我刚刚做过这个,所以希望它有用。
只需确保在XIB文件中设置7个UIImageViews,链接它们就可以了。
@synthesize img1, img2, img3, img4, img5, img6, magnet;
-(void) checkInsertion {
if (CGRectContainsRect(img2.frame, img1.frame)) {img1.center = img2.center;}
if (CGRectContainsRect(img4.frame, img3.frame)) {img3.center = img4.center;}
if (CGRectContainsRect(img6.frame, img5.frame)) {img5.center = img6.center;}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
if (CGRectIntersectsRect(magnet.frame, img1.frame)) {img1.center = magnet.center;}
if (CGRectIntersectsRect(magnet.frame, img2.frame)) {img2.center = magnet.center;}
if (CGRectIntersectsRect(magnet.frame, img3.frame)) {img3.center = magnet.center;}
if (CGRectIntersectsRect(magnet.frame, img4.frame)) {img4.center = magnet.center;}
if (CGRectIntersectsRect(magnet.frame, img5.frame)) {img5.center = magnet.center;}
if (CGRectIntersectsRect(magnet.frame, img6.frame)) {img6.center = magnet.center;}
[UIView commitAnimations];
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [[event allTouches] anyObject];
CGPoint touchLoc = [touch locationInView:touch.view];
if (CGRectContainsPoint(img1.frame, touchLoc)) {img1.center = touchLoc;}
if (CGRectContainsPoint(img3.frame, touchLoc)) {img3.center = touchLoc;}
if (CGRectContainsPoint(img5.frame, touchLoc)) {img5.center = touchLoc;}
if (CGRectContainsPoint(magnet.frame, touchLoc)) {magnet.center = touchLoc;}
[self checkInsertion];
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
答案 1 :(得分:0)
你应该看看UIPanGestureRecognizer
。在负责这些多个图像视图的视图控制器中,为每个图像视图添加平移手势识别器。视图控制器应负责响应平移手势回调并相应地移动图像视图的帧。
值得注意的是,您还应该从中心抓取图像视图中的触摸偏移并存储它。然后,当平移视图时,您需要将偏移量计入计算中。这样,如果视图开始拖动到远离中心点的位置,视图就不会跳到用户的手指上。
请参阅2010年和2011年关于UIScrollView
的WWDC会话视频。特别是从2011年开始。他们将举例说明如何做到这一点。
答案 2 :(得分:0)
我已经开始工作了!
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint location = [touch locationInView:touch.view];
if(CGRectContainsPoint(plusone.frame, location))
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
plusone.center = location;
}
if(CGRectContainsPoint(plustwo.frame, location))
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
plustwo.center = location;
}
if(CGRectContainsPoint(plusthree.frame, location))
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
plusthree.center = location;
}
if(CGRectContainsPoint(minusone.frame, location))
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
minusone.center = location;
}
if(CGRectContainsPoint(minustwo.frame, location))
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
minustwo.center = location;
}
if(CGRectContainsPoint(minusthree.frame, location))
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
minusthree.center = location;
}
但是,当一个图像与另一个图像碰撞时,它会消失! 有没有人解决这个问题?