使用 UIPanGestureRecognizer 拖动 + 旋转触摸下车 [英] Drag + Rotation using UIPanGestureRecognizer touch getting off track

查看:22
本文介绍了使用 UIPanGestureRecognizer 拖动 + 旋转触摸下车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am doing some drag and rotation calculations using UIPanGestureRecognizer. The rotation angle is correct, and the drag location is almost correct. The problem is that as you go around the center of the box needs to be adjusted according to the angle and I can't figure out how.

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

I've included pictures of what a 180 rotation looks like but where the finger is during the rotation. I just don't know how to adjust to make the block stay with your finger appropriately. And heres a video just to clarify because it is strange behavior. http://tinypic.com/r/mhx6a1/5

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

Here is a real world video of what should be happening. The problem being that in the iPad video your finger is moving where in the real world your finger would be cemented in a particular place on the item moving. The math needed is to adjust your touch location along the angle with a difference from the actual center. I just can't figure out the math. 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)];

    }
}

推荐答案

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

I've finally solved this issue and have it working perfectly. Persistence am I right??

这是解决方案的代码,其中包含一些注释来解释更改.

Here is the code for the solution with a few comments to explain the changes.

- (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)];

    }
}

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

Hope my month+ struggle and solution helps someone else in the future. Happy Coding :)

这篇关于使用 UIPanGestureRecognizer 拖动 + 旋转触摸下车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆