3

我想知道如何UITouch在我的代码中平滑 a 。我能够UItouch在我的 上检测到UIView,但是当我尝试使用 旋转视图时CGAffineTransform,它不会平滑旋转。我必须在 iPhone 上按下或长指触摸才能进行这种旋转。如何执行平滑旋转,例如 Roambi Visualizer 应用程序。谢谢你的帮助。

4

3 回答 3

3

transform是 UIView 的动画属性,所以你可以使用 Core Animation 来使旋转平滑:

CGAffineTransform newTransform = //...construct your desired transform here...
[UIView animateWithDuration:0.2
                 animations:^{view.transform = newTransform;}];
于 2011-03-18T16:36:53.523 回答
1

大家好,我在touchesMoved中找到了我的问题的以下解决方案及其对我的工作......

这是代码....

UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:self.superview];
CGPoint pastLocation = [touch previousLocationInView:self.superview];
currentLocation.x = currentLocation.x - self.center.x;
currentLocation.y = self.center.y - currentLocation.y;
pastLocation.x = pastLocation.x - self.center.x;
pastLocation.y = self.center.y - currentLocation.y;
CGFloat angle = atan2(pastLocation.y, pastLocation.x) - atan2(currentLocation.y, currentLocation.x); 
CGAffineTransform transform = CGAffineTransformMakeRotation(angle);

// Apply the affine transform

[[self.superview viewWithTag:ROTATE_VIEW_TAG] setTransform:transform] ;
于 2011-03-24T10:41:18.997 回答
1

这可能会死也可能不会死太久,但是 cam 的答案存在一些小问题。

 pastLocation.y = self.center.y - currentLocation.y;

需要是

 pastLocation.y = self.center.y - pastLocation.y;

如果您想快速执行此操作,我使用 Cam 的答案来确定以下内容:

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    var touch: UITouch = touches.first as! UITouch

    var currentTouch = touch.locationInView(self.view)
    var previousTouch = touch.previousLocationInView(self.view)

    currentTouch.x = currentTouch.x - self.view.center.x
    currentTouch.y = self.view.center.y - currentTouch.y

    previousTouch.x = previousTouch.x - self.view.center.x
    previousTouch.y = self.view.center.y - previousTouch.y

    var angle = atan2(previousTouch.y, previousTouch.x) - atan2(currentTouch.y, currentTouch.x)

    UIView.animateWithDuration(0.1, animations: { () -> Void in
        bigCircleView?.transform = CGAffineTransformRotate(bigCircleView!.transform, angle)
    })

}
于 2015-07-09T02:14:10.173 回答