拖动+旋转使用UIPanGestureRecognizer触摸下车轨道
我正在使用UIPanGestureRecognizer进行拖动和旋转计算。 旋转angular度是正确的,拖动位置几乎是正确的。 问题是,当你走在箱子的中心需要根据angular度进行调整,我不知道如何。
我已经包含了旋转过程中180度旋转的图像,但手指在哪里。 我只是不知道如何调整,使块适当地留在你的手指。 inheritance人一个video只是为了澄清,因为这是奇怪的行为。 http://tinypic.com/r/mhx6a1/5
编辑:这是一个真实的世界video应该发生什么。 问题是,在iPadvideo中,您的手指正在移动,在真实世界的任何地方,您的手指会粘在物品移动的特定位置。 math需要的是调整你的触摸位置的angular度与实际中心的差异。 我只是无法弄清楚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)]; } }
我终于解决了这个问题,并完美地工作。 坚持我是吗?
这里是解决scheme的代码和一些注释来解释变化。
- (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)]; } }
希望我的月份+奋斗和解决scheme可以帮助未来的其他人。 快乐编码:)
基于触摸事件https://github.com/kirbyt/KTOneFingerRotationGestureRecognizer
帮助我解决类似的问题
- iOS 11 dropInteraction执行文件的Drop
- iOS – 拖放碰撞检测如何检测您select的项目拖到另一个子视图上?
- iOS >>拖动视图跳回原始位置>>自动布局与UIPanGestureRecognizer问题结合
- 是否有可能使用iOS 11的拖放来重新sorting多个项目/单元格在UITableView?
- 如何pipe理在iOS上的MKAnnotationView的拖放?
- 如何编程拖放到Objective-C的IOS区域
- iOS – UIPanGestureRecognizer:在animation中拖动
- 拖动注释时callback
- iPhone / iOS的:如何实现拖放滚动视图的子视图?