UIView触控平滑旋转

UIView触控平滑旋转

问题描述:

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

+0

UIView的动画可以让旋转非常流畅如你所愿,使用动画的时间长度 – Splendid 2011-03-19 18:33:52

transform是的UIView的动画属性,所以你可以使用核心动画制作平滑旋转:

CGAffineTransform newTransform = //...construct your desired transform here... 
[UIView animateWithDuration:0.2 
       animations:^{view.transform = newTransform;}]; 
+0

谢谢你的完美答案! – 2012-05-15 02:09:45

大家好,我发现以下解决我的问题,它在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] ; 
+0

如果你想逐渐旋转它,你应该使用[view setTransform:CGAffineTransformRotate(view.transform,angle)]。否则,这个工程太棒了! – 2011-09-14 14:37:02

这可能是也可能不是死多头,但也有凸轮的回答几个小问题。

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

需求是

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

如果您正在寻找这样做在迅速,我用凸轮的答案找出以下几点:

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) 
    }) 

}