Rya*_*los 6 drag-and-drop objective-c rotation uigesturerecognizer ios
我正在使用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)];
}
}
Run Code Online (Sandbox Code Playgroud)
我终于解决了这个问题并让它完美运行.坚持我是对的?
以下是解决方案的代码,其中包含一些注释以解释更改.
- (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)];
}
}
Run Code Online (Sandbox Code Playgroud)
希望我的一个月+奋斗和解决方案将来帮助别人.快乐编码:)
| 归档时间: |
|
| 查看次数: |
5624 次 |
| 最近记录: |