使用XCode 4.2并尝试掌握UIGestureRecognisers。到目前为止,一切似乎进展顺利,但我仍然遇到一些问题。
当我使用滑动手势识别器时,一切都很好,它会识别所有不同方向的滑动,并且会持续进行。我现在的问题是,当使用平移手势识别器时,它会识别出第一次平移扫描,但之后只是拒绝接受任何进一步的手势。所以我可以根据需要移动一次物品,但在那之后,什么都不做。
我按照以下方式设置了手势:
UIPanGestureRecognizer *panBody = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panBody:)];
[bodyGestureView addGestureRecognizer:panBody];
然后这是我处理它的'panBody'方法:
- (void)panBody:(UIPanGestureRecognizer *)recognizer
{
CGPoint translate = [recognizer translationInView:self.view];
CGRect bodyPanelFrame = bodyPanel.frame;
bodyPanelFrame.origin.x += translate.x;
bodyPanelFrame.origin.y += translate.y;
recognizer.view.frame = bodyPanelFrame;
CGRect topPanelFrame = topPanel.frame;
topPanelFrame.origin.x += translate.x;
topPanelFrame.origin.y += translate.y;
recognizer.view.frame = topPanelFrame;
CGRect sidePanelFrame = sidePanel.frame;
sidePanelFrame.origin.x += translate.x;
sidePanelFrame.origin.y += translate.y;
recognizer.view.frame = sidePanelFrame;
NSLog(@"Panning");
if (recognizer.state == UIGestureRecognizerStateEnded)
{
bodyPanel.frame = bodyPanelFrame;
if((topPanel.frame.origin.x + translate.x) <= 193)
{
topPanel.frame = CGRectMake(topPanelFrame.origin.x, topPanel.frame.origin.y, topPanel.frame.size.width, topPanel.frame.size.height);
}
else
{
topPanel.frame = CGRectMake(193, 0, topPanel.frame.size.width, topPanel.frame.size.height);
NSLog(@"Top panel not in frame");
}
if((sidePanel.frame.origin.y + translate.y) < 57)
{
sidePanel.frame = CGRectMake(sidePanel.frame.origin.x, sidePanelFrame.origin.y, sidePanel.frame.size.width, sidePanel.frame.size.height);
}
else
{
sidePanel.frame = CGRectMake(0, 56, sidePanel.frame.size.width, sidePanel.frame.size.height);
NSLog(@"Side panel not in frame");
}
}
}
bodyPanel,topPanel和sidePanel是链接到UIView的IBOutlets,覆盖在我的界面顶部.xib
如果有人能够对这些信息有所了解,那将是很好的因为我完全不知道发生了什么!!
谢谢,
马特
答案 0 :(得分:1)
首先我会检查
if (recognizer.state == UIGestureRecognizerStateChanged)
在进行翻译之前(还有许多其他可能的状态不能证明你采取任何行动)。我也会在每次回调时重置翻译,因为你正在使用UIPanGestureRecognizer方法累积它们
- (void)setTranslation:(CGPoint)translation inView:(UIView *)view
如果手势识别器停止,则可能是另一个手势识别器正在干扰它。你还有一个活跃的UISwipeGestureRecognizer吗?如果是这样,您应该停用其中一个。您还可以查看此方法
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer
允许您指定应优先考虑哪个识别器。