objective-c - 使用 UIPanGestureRecognizer 拖动 + 旋转触摸偏离轨道

标签 objective-c ios drag-and-drop rotation uigesturerecognizer

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

我提供了 180 度旋转的图片,但旋转过程中手指所在的位置。我只是不知道如何调整才能使积木适本地留在您的手指上。这里有一段视频只是为了澄清,因为这是一种奇怪的行为。 http://tinypic.com/r/mhx6a1/5

编辑:这是一个真实世界的视频,说明应该发生什么。问题在于,在 iPad 视频中,您的手指正在移动,而在现实世界中,您的手指会粘在移动的项目的特定位置。所需的数学运算是沿着与实际中心不同的角度调整您的触摸位置。我只是想不通数学。 http://tinypic.com/r/4vptnk/5

first shot

second shot

third shot

非常感谢!

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

    }
}

最佳答案

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

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

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

    }
}

希望我的 month+ 奋斗和解决方案在未来能帮助到其他人。快乐编码:)

关于objective-c - 使用 UIPanGestureRecognizer 拖动 + 旋转触摸偏离轨道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10181255/

相关文章:

IOS 使用 CGSize 弃用方法创建文本大小

objective-c - Cocoa 如何显示信息性消息

ios - UIScreen 上的 snapshotViewAfterScreenUpdates 在设备上提供空白快照

javascript - 如何将 JQuery UI 的可拖动和可排序与 div 元素混合在一起?

jquery - 防止可拖动项目落在特定元素上

ios - 使用 iOS 9 的多任务处理,我的应用程序可以接收来自另一个应用程序的拖动内容吗?

objective-c - 查找 char 是否在 objective-c 中的字符串中

objective-c - OS X Mac 和编写 twain 扫描应用程序

ios - 寻找有关使用 Appium 自动化真实 iOS 设备的教程

ios - 在大型 UIView 上绘制占用大量内存