2014-11-14 41 views
2

我想獲得鍵盤的UIAnimationCurve,如果存在的話。我使用的代碼如下:鍵盤動畫曲線爲真

if let kbCurve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? Int{ 
       animateCurve = UIViewAnimationCurve. 
      } 

然而,animateCurve,作爲一個UIViewAnimationCurve,不能從一個Int轉換。我如何通過這種方式獲得曲線?

如果我把他們當作數字,而不是UIViewAnimationCurve枚舉,下面的錯誤,當試圖動畫:

//Animated done button with keyboard 
     origDoneFrame = btnDone.frame 
     btnDone.hidden = false 
     UIView.animateWithDuration(
      animateDuration, 
      delay: Numbers.ANIMATE_DELAY, 
      options: nil, 
      animations: { 
       UIView.setAnimationCurve(animateCurve) 
       self.btnDone.frame = CGRectMake(self.btnDone.frame.origin.x + kbHeight, self.btnDone.frame.origin.y, self.btnDone.frame.size.width, self.btnDone.frame.size.height) 
       return Void() 
      }, 
      completion: {finished in 
       return Void() 
      } 
     ) 

有沒有一種方法來設置使用一個int曲線?

企圖INT曲線:

UIView.animateWithDuration(
      animateDuration, 
      delay: Numbers.ANIMATE_DELAY, 
      options: UIViewAnimationOptions(animateCurve << 16), 
      animations: { 
       self.btnDone.frame = CGRectMake(self.btnDone.frame.origin.x + kbHeight, self.btnDone.frame.origin.y, self.btnDone.frame.size.width, self.btnDone.frame.size.height) 
       return Void() 
      }, 
      completion: {finished in 
       return Void() 
      } 
     ) 

但是編譯錯誤的發生是由於UIViewAnimationOptions不是正確的類型。如果我自己運行UIViewAnimationOptions的賦值,那麼會出現編譯器錯誤「無法找到接受提供參數的'init'的重載。」

+0

您是否嘗試過使用'as?而不是UIViewAnimationCurve'? – 2014-11-14 21:33:49

+0

@IanMacDonald不​​能編譯。這個錯誤是不能預見的,因爲我已經注意到XCode經常出現這種情況,但是7與枚舉不匹配(參見下面的nickgraef的回覆) – steventnorris 2014-11-14 21:51:32

回答

12

您可以從原始Int值與

animateCurve = UIViewAnimationCurve(rawValue: kbCurve) 

但是得到一個枚舉值,標準鍵盤採用的7動畫曲線值,這可能不是一個枚舉值匹配,所以animateCurvenil。如果是這種情況,只需將animateCurve定義爲Int並在代碼中使用原始值而不是枚舉值。

此外,快速谷歌搜索打開了這個包裝,這可能對你有用:https://gist.github.com/kristopherjohnson/13d5f18b0d56b0ea9242

更新回答編輯的問題:

您可以在動畫中使用的整動畫曲線值選項通過將其轉換爲UIViewAnimationOptions值:

UIViewAnimationOptions(kbCurve << 16) // where kbCurve: UInt 

更新夫特1.2(6.3的XCode):

Swift 1.2的發行說明表明,具有未記錄值的NS_ENUM類型現在可以從其原始整數值轉換而不被重置爲nil。所以,下面的代碼將現在的工作:

爲雨燕2.2(7.3的XCode) ​​

更新:

if let animationCurveInt = (userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber)?.unsignedIntegerValue { 
    let animationCurve = UIViewAnimationOptions(rawValue: animationCurveInt<<16) 
    ... 
} 
+0

你是對的。匹配一個枚舉。但是,當我嘗試執行上述操作(請參閱編輯後)時,它在UIView.setAnimationCurve上發生錯誤。 – steventnorris 2014-11-14 21:49:53

+0

@steventnorris我已經更新了我的答案,以展示如何使用7值作爲動畫曲線。 – nickgraef 2014-11-14 22:08:03

+0

我試過你的解決方案,但得到了一個編譯器錯誤。我已經調整了上面的帖子。 – steventnorris 2014-11-14 22:12:39

0

你應該使用較舊的API,非基於塊的一個。然後你就可以設置動畫曲線,你將從通知對象中獲得動畫曲線。