6

我正在使用 UIPanGestureRecognizer 进行一些拖动和旋转计算。旋转角度正确,拖动位置几乎正确。问题是当你绕过盒子的中心时,需要根据角度进行调整,我不知道怎么做。

我已经包含了 180 度旋转的图片,但是在旋转过程中手指在哪里。我只是不知道如何调整以使块适当地停留在您的手指上。这是一个视频,只是为了澄清,因为这是奇怪的行为。http://tinypic.com/r/mhx6a1/5

编辑:这是一个真实世界的视频,讲述了应该发生的事情。问题在于,在 iPad 视频中,您的手指正在移动,而在现实世界中,您的手指将固定在移动项目的特定位置。所需的数学运算是沿着与实际中心不同的角度调整您的触摸位置。我只是无法弄清楚数学。http://tinypic.com/r/4vptnk/5

第一枪

第二枪

第三枪

非常感谢!

- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        // set original center so we know where to put it back if we have to.
        originalCenter = dragView.center;

    } else if (gesture.state == UIGestureRecognizerStateChanged) {
        [dragView setCenter:CGPointMake( originalCenter.x + [gesture translationInView:self.view].x , originalCenter.y + [gesture translationInView:self.view].y )];

        CGPoint p1 = button.center;
        CGPoint p2 = dragView.center;

        float adjacent = p2.x-p1.x;
        float opposite = p2.y-p1.y;

        float angle = atan2f(adjacent, opposite); 

        [dragView setTransform:CGAffineTransformMakeRotation(angle*-1)];

    }
}
4

2 回答 2

8

我终于解决了这个问题并让它完美运行。坚持是对的吗??

这是解决方案的代码,并带有一些注释来解释更改。

- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        // Get the location of the touch in the view we're dragging.
        CGPoint location = [gesture locationInView:dragView];

        // Now to fix the rotation we set a new anchor point to where our finger touched. Remember AnchorPoints are 0.0 - 1.0 so we need to convert from points to that by dividing
        [dragView.layer setAnchorPoint:CGPointMake(location.x/dragView.frame.size.width, location.y/dragView.frame.size.height)];


    } else if (gesture.state == UIGestureRecognizerStateChanged) {
        // Calculate Our New Angle
        CGPoint p1 = button.center;
        CGPoint p2 = dragView.center;

        float adjacent = p2.x-p1.x;
        float opposite = p2.y-p1.y;

        float angle = atan2f(adjacent, opposite); 

        // Get the location of our touch, this time in the context of the superview.
        CGPoint location = [gesture locationInView:self.view];

        // Set the center to that exact point, We don't need complicated original point translations anymore because we have changed the anchor point.
        [dragView setCenter:CGPointMake(location.x, location.y)];

        // Rotate our view by the calculated angle around our new anchor point.
        [dragView setTransform:CGAffineTransformMakeRotation(angle*-1)];

    }
}

希望我一个月以上的奋斗和解决方案能在未来帮助别人。快乐编码:)

于 2012-06-04T13:26:10.797 回答
3

基于触摸事件 https://github.com/kirbyt/KTOneFingerRotationGestureRecognizer

帮助我解决了类似的问题

于 2014-01-20T07:38:18.873 回答