2016-04-17 42 views
2

SWIFT - OSXNSImageView的連續旋轉(因此它似乎是動畫)

我有一堆我Main.storyboard設置imageViews的。我試圖在應用程序啓動時讓它們旋轉,並且我會無限期地讓它們旋轉。我遇到了roateByAngle(角度:CGFloat),但是這不會對它進行動畫處理,而只是跳到新的角度。

我想做兩個函數,spinClockwise()和spinAntiClockwise(),所以我可以在viewDidLoad中調用它們,它們將繼續轉動。

我一直在玩CATransform3DMakeRotation,但似乎無法得到我想要的結果

 let width = myImg.frame.width/2 
     let height = myImg.frame.height/2 
     myImg.layer?.transform = CATransform3DMakeRotation(180, width, height, 1) 

讓我知道,如果我可以更具體。 感謝

回答

2

您可以添加這樣的UIView或的UIImageView的擴展:

extension UIView { 

    ///The less is the timeToRotate, the more fast the animation is ! 
    func spinClockwise(timeToRotate: Double) { 
     startRotate(CGFloat(M_PI_2), timeToRotate: timeToRotate) 
    } 

    ///The less is the timeToRotate, the more fast the animation is ! 
    func spinAntiClockwise(timeToRotate: Double) { 
     startRotate(CGFloat(-M_PI_2), timeToRotate: timeToRotate) 
    } 

    func startRotate(angle: CGFloat, timeToRotate: Double) { 
     UIView.animateWithDuration(timeToRotate, delay: 0.0, options:[UIViewAnimationOptions.CurveLinear, UIViewAnimationOptions.Repeat], animations: { 
      self.transform = CGAffineTransformMakeRotation(angle) 
      }, completion: nil) 
     print("Start rotating") 
    } 

    func stopAnimations() { 
     self.layer.removeAllAnimations() 
     print("Stop rotating") 
    } 
} 

所以,當你想旋轉你的myImg,你只需要調用:

myImg.spinClockwise(3)

而且當你想停止它:

myImg.stopAnimations()

注: 我加了playground只是讓你可以測試一下;)

乾杯!

編輯:

我的壞,這是一個NSView的例子:

extension NSView { 

    ///The less is the timeToRotate, the more fast the animation is ! 
    func spinClockwise(timeToRotate: Double) { 
     startRotate(CGFloat(-1 * M_PI * 2.0), timeToRotate: timeToRotate) 
    } 

    ///The less is the timeToRotate, the more fast the animation is ! 
    func spinAntiClockwise(timeToRotate: Double) { 
     startRotate(CGFloat(M_PI * 2.0), timeToRotate: timeToRotate) 
    } 

    func startRotate(angle: CGFloat, timeToRotate: Double) { 

     let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation") 
     rotateAnimation.fromValue = 0.0 
     rotateAnimation.toValue = angle 
     rotateAnimation.duration = timeToRotate 
     rotateAnimation.repeatCount = .infinity 

     self.layer?.addAnimation(rotateAnimation, forKey: nil) 

     Swift.print("Start rotating") 
    } 

    func stopAnimations() { 
     self.layer?.removeAllAnimations() 
     Swift.print("Stop rotating") 
    } 
} 

重要提示:現在,經過我的測試中,我注意到,你必須設置你的NSView的錨點在中間,以便它可以圍繞其中心旋轉:

view.layer?.anchorPoint = CGPointMake(0.5, 0.5) 

我增加了一個新的平臺,與OSX example

+1

啊是的,這是我後面的事情,除了它需要是NSView,雖然NSView沒有'animateWithDuration'。有任何想法嗎? – Steve

+1

@Steve:我更新了我的答案,讓我知道它是否適合你! – Mindhavok

+1

ohhh soo close,圖像正在旋轉,但我無法將它放到最初放置的框架的中心,'myImg.layer?.anchorPoint = CGPointMake(0.5,0.5)',但它的類型移動到了左下角 – Steve

0

對我來說,我無法改變定位點。它正在旋轉(0,0)左下方。我將錨點移至(0.5,0.5),但仍然沒有運氣。然後我與this answer交叉。我修改了我的代碼,如下所示,並開始圍繞自身旋轉。然而,我觀察到一個缺點,視圖的位置不知何故發生了變化,但它可以通過反覆試驗來修復,試圖將它放到正確的位置。

extension NSView { 

    func startRotating(duration: Double = 1) { 

     let kAnimationKey = "rotation" 

     //self.wantsLayer = true 
     if layer?.animation(forKey: kAnimationKey) == nil { 
      var oldFrame = self.frame 
      self.layer?.anchorPoint = CGPoint(x: 1, y: 1) 
      self.frame = oldFrame 

      let animate = CABasicAnimation(keyPath: "transform.rotation") 
      animate.duration = duration 
      animate.repeatCount = Float.infinity 
      animate.fromValue = 0.0 
      animate.toValue = Double.pi * 2.0 
      self.layer?.add(animate, forKey: kAnimationKey) 

      oldFrame = self.frame 
      self.layer?.anchorPoint = CGPoint(x: 0.5, y: 0.5) 
      self.frame = oldFrame 
     } 


    } 
} 
+0

謝謝。 :)我只是試過這個,它仍然圍繞其中一個角落旋轉。你如何定位你的'NSView'?你使用自動佈局嗎?它是否被包裹在另一個相同大小的「NSView」中? –

+0

是的,我使用自動佈局。在故事板上,我的視圖(即NSImageView)被放置在堆棧視圖中。 – Burak