我使用下面的代码(如下所示)来实现拖放功能。在几个不同的地方徘徊,过去一直对我很好。
现在我遇到了问题而且不知道为什么。只要按下按钮我的设备(或模拟器)导向肖像,它就可以完美地工作。但是在任何其他三个方向中,当手指拖动视图移动一个方向时,“拖动”视图移动不同的方向。
如下图所示,我记录了每次移动的翻译价值。在原始方向中,当我从屏幕中间向左下角拖动时,翻译的值为:
- ,+
如果我向左旋转,再次执行此操作:
- , -
再次向左旋转:
+, -
再次向左旋转:
+,+
我完全没有得到这里发生的事情,特别是因为这段代码似乎在其他视图控制器中运行良好。
任何建议都将受到赞赏。
- (void) didMakePanGesture:(UIPanGestureRecognizer *)panGesture
{
if (panGesture.state == UIGestureRecognizerStateBegan)
{
[self setDropTargetsCoordinates]; // saves correct drop target & its coordinates
dragViewStartLocation = receptiveClassificationImageView.center; // save center in case we have to snap back
receptiveClassificationImageView.transform = CGAffineTransformMakeScale(0.40f, 0.40f); // make the image smaller for dragging
receptiveClassificationImageView.layer.cornerRadius = 12.0f; // we lose the rounded corners in the scaling; this to fix
}
else if (panGesture.state == UIGestureRecognizerStateChanged)
{
//
// Adjust the location of the dragged view whenever state changes
//
CGPoint translation = [panGesture translationInView:nil];
CGAffineTransform transform = receptiveClassificationImageView.transform;
transform.tx = translation.x;
transform.ty = translation.y;
receptiveClassificationImageView.transform = transform;
NSLog(@"Translation=%f,%f" translation.x, translation.y);
}
else if (panGesture.state == UIGestureRecognizerStateEnded)
{
// do stuff when dropped
}
答案 0 :(得分:0)
以防有人稍后看到此问题,以上代码的以下更改解决了此问题:
if (panGesture.state == UIGestureRecognizerStateBegan)
{
[self setDropTargetsCoordinates]; // saves correct drop target & its coordinates
dragViewStartLocation = receptiveClassificationImageView.center; // save center in case we have to snap back
**receptiveClassificationImageView.center = [panGesture locationInView:receptiveClassificationImageView.superview]; // re-center the view before scaling**
receptiveClassificationImageView.transform = CGAffineTransformMakeScale(0.40f, 0.40f); // make the image smaller for dragging
receptiveClassificationImageView.layer.cornerRadius = 12.0f; // we lose the rounded corners in the scaling; this to fix
}
else if (panGesture.state == UIGestureRecognizerStateChanged)
{
//
// Adjust the location of the dragged view whenever state changes
//
CGPoint translation = [panGesture translationInView:**self.view**];
CGAffineTransform transform = receptiveClassificationImageView.transform;
transform.tx = translation.x;
transform.ty = translation.y;
receptiveClassificationImageView.transform = transform;
}