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

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

问题描述

我正在使用UIPanGestureRecognizer进行一些拖动和旋转计算。旋转角度正确,拖动位置几乎正确。问题是,当你围绕箱子的中心需要根据角度进行调整,我无法弄清楚如何。



我已经包括图片180度的旋转是什么样的,但手指在旋转过程中的位置。我只是不知道如何调整,使块保持适当的手指。并且只是为了澄清一个视频,因为它是奇怪的行为。 http://tinypic.com/r/mhx6a1/5



编辑:这是一个真实世界的应用程序视频。问题是,在iPad视频中,您的手指正在移动在现实世界中,您的手指将固定在项目移动的特定位置。所需的数学是沿着与实际中心不同的角度调整您的触摸位置。我只是想不出数学。 http://tinypic.com/r/4vptnk/5









非常感谢!

   - (void)handlePan :( UIPanGestureRecognizer *)手势
{
if(gesture.state == UIGestureRecognizerStateBegan ){
//设置原始中心,所以我们知道如果我们必须把它放回来。
originalCenter = dragView.center;

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

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

float adjacent = p2.x-p1.x;
float对面= p2.y-p1.y;

float angle = atan2f(adjacent,opposite);

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

}
}


解决方案

我终于解决了这个问题,使其工作得很好。坚持我是对的?



以下是解决方案的代码,并附有几条意见来解释更改。

   - (void)handlePan :( UIPanGestureRecognizer *)手势
{
if(gesture.state == UIGestureRecognizerStateBegan){
//获取位置在我们拖动的视野中的触摸。
CGPoint location = [gesture locationInView:dragView];

//现在要修复旋转,我们在手指触摸的地方设置一个新的锚点。记住AnchorPoints是0.0 - 1.0,所以我们需要通过划分
[dragView.layer setAnchorPoint:CGPointMake(location.x / dragView.frame.size.width,location.y / dragView.frame.size 。高度)];


} else if(gesture.state == UIGestureRecognizerStateChanged){
//计算我们的新角度
CGPoint p1 = button.center;
CGPoint p2 = dragView.center;

float adjacent = p2.x-p1.x;
float对面= p2.y-p1.y;

float angle = atan2f(adjacent,opposite);

//获取我们触摸的位置,这次是在超级视图的上下文中。
CGPoint location = [手势locationInView:self.view];

//将中心设为确切点,我们不再需要复杂的原始点翻译,因为我们改变了锚点。
[dragView setCenter:CGPointMake(location.x,location.y)];

//以我们的新锚点周围的计算角度旋转我们的视图。
[dragView setTransform:CGAffineTransformMakeRotation(angle * -1)];

}
}

希望我的月份+未来的其他人快乐编码:)


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.

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

EDIT: 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

Thanks very much!

- (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天全站免登陆