gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:40:12 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com