我正在制作我的第一个应用程序,就像纸牌游戏一样 - 用户必须移动卡片并将它们堆叠在一起。现在我对Objective-C很新,所以我希望你在我的问题很愚蠢的时候原谅我。
所以我正在尝试解决问题,我有两个UIImageViews
并附加UIPanGestureRecgonizer
。因此,每当用户拖动其中一张卡时,它必须位于另一张卡的顶部。我在我的ViewController.m文件中使用下面的代码,但问题是,当我拖动一张卡然后想要拖动另一张卡时,第一张卡片会快速回到原来的位置,但我希望它留在原处我拖了它。当我注释掉bringSubviewToFront时,这不会发生,但其中一张牌总是留在另一张牌之后。
-(IBAction)dragCard:(UIPanGestureRecognizer *)recognizer {
UIView *cardView = [(UIPanGestureRecognizer *)recognizer view];
[self.view bringSubviewToFront:cardView];
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointZero inView:self.view];}
我无法弄清楚造成这种情况的原因。你能帮帮我吗...
编辑:我记录了self.view.subviews以显示正在发生的事情。我用标签1拖动图像,现在我开始用标签2拖动图像。你可以看到两个坐标都改变了 - 带有标签1的图像移回原始位置,带有标签2的图像也移动了:
2013-05-16 17:02:20.424 (
"<UIImageView: 0x71749e0; frame = (0 0; 320 411); autoresize = TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x7174a40>>",
"<UIImageView: 0x7172ef0; frame = (25 77; 42 58); autoresize = TM+BM; tag = 1; gestureRecognizers = <NSArray: 0x7171700>; layer = <CALayer: 0x7171620>>",
"<UIImageView: 0x71722f0; frame = (88 200; 42 58); autoresize = TM+BM; tag = 2; gestureRecognizers = <NSArray: 0x7171bc0>; layer = <CALayer: 0x7172350>>"
2013-05-16 17:02:20.441 (
"<UIImageView: 0x71749e0; frame = (0 0; 320 411); autoresize = TM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x7174a40>>",
"<UIImageView: 0x7172ef0; frame = (30 177; 42 58); autoresize = TM+BM; tag = 1; gestureRecognizers = <NSArray: 0x7171700>; layer = <CALayer: 0x7171620>>",
"<UIImageView: 0x71722f0; frame = (91 201; 42 58); autoresize = TM+BM; tag = 2; gestureRecognizers = <NSArray: 0x7171bc0>; layer = <CALayer: 0x7172350>>"
第一个ImageView是我的背景图片。