2011-03-18 181 views
3

我想知道如何在我的代碼中平滑UITouch。我能夠檢測到我的UIView上的UItouch,但是當我嘗試使用CGAffineTransform旋轉視圖時,它不會平滑地旋轉。我必須按下或長時間手指觸摸iPhone進行這種旋轉。 如何執行平滑旋轉,如Roambi Visualizer應用程序。 感謝您的幫助。UIView觸控平滑旋轉

+0

UIView的動畫可以讓旋轉非常流暢如你所願,使用動畫的時間長度 – Splendid 2011-03-19 18:33:52

回答

3

transform是的UIView的動畫屬性,所以你可以使用核心動畫製作平滑旋轉:

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

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

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] ; 
+0

如果你想逐漸旋轉它,你應該使用[view setTransform:CGAffineTransformRotate(view.transform,angle)]。否則,這個工程太棒了! – 2011-09-14 14:37:02

1

這可能是也可能不是死多頭,但也有凸輪的回答幾個小問題。

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

}