2017-03-05 5 views
0

ボタンを押しても回転を止めようとするまで、回転し続けたい画像があります。私はこれらのウェブサイトを試した:https://www.andrewcbancroft.com/2014/10/15/rotate-animation-in-swift/私は代表団にチャンスを与えなければならなかった、それを変更しようとするとクラッシュする とhttps://bencoding.com/2015/07/27/spinning-uiimageview-using-swift/は、ありがとうございました。Swiftでの回転イメージのアニメーション

Andrewcbancroft.com::次のコードはここにある

extension UIView { 
    func rotate360Degrees(duration: CFTimeInterval = 1.0, completionDelegate: AnyObject? = nil) { 
     let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation") 
     rotateAnimation.fromValue = 0.0 
     rotateAnimation.toValue = CGFloat(M_PI * 2.0) 
     rotateAnimation.duration = duration 

     if let delegate: AnyObject = completionDelegate { 
      rotateAnimation.delegate = delegate //<- error "Cannot assign value of type 'AnyObject' to type 'CAAnimationDelegate?'" 
     } 
     self.layer.add(rotateAnimation, forKey: nil) 
    } 
} 

のようにデリゲートをキャストしよう! CAAnimationDelegateがエラーコードでアプリケーションをクラッシュする画像を回転しようとすると 'test.ViewController'(0x1074a0a60)型の値を 'CAAnimationDelegate'(0x10cede480)にキャストできませんでした。

Bending.com:

extension UIView { 
    func startRotating(duration: Double = 1) { 
     let kAnimationKey = "rotation" 

     if self.layer.animation(forKey: kAnimationKey) == nil { 
      let animate = CABasicAnimation(keyPath: "transform.rotation") 
      animate.duration = duration 
      animate.repeatCount = Float.infinity 
      animate.fromValue = 0.0 
      animate.toValue = Float(M_PI * 2.0) 
      self.layer.add(animate, forKey: kAnimationKey) 
     } 
    } 
    func stopRotating() { 
     let kAnimationKey = "rotation" 

     if self.layer.animation(forKey: kAnimationKey) != nil { 
      self.layer.removeAnimation(forKey: kAnimationKey) 
     } 
    } 
} 

私のviewDidLoadメソッドに回転を開始するために私の画像ビューを呼び出すようにしようと、何も起こりません。

+0

誰もこれらのサイトのコードを読んでコードを書くつもりはありません。試したコードとエラーメッセージを投稿してください。 – Frankie

+0

Ok更新しました – Petravd1994

答えて

0

エラーは次のように説明し

あなたはCAAnimationDelegateに準拠していない

AnyObject

func rotate360Degrees(duration: CFTimeInterval = 1.0, completionDelegate: AnyObject? = nil) 

AnyObjectようにデリゲートを渡しているので、あなたは、デリゲートとして使用することはできません。

あなたは適切な委任することができ、コントローラに渡している特定している場合は、デリゲートの正しい種類にキャスト:あなたはこれをやっているし、それがクラッシュするなら

if let delegate = completionDelegate as? CAAnimationDelegate { 
    rotateAnimation.delegate = delegate 
} 

:その後

rotateAnimation.delegate = completionDelegate as! CAAnimationDelegate 

あなたはCAAnimationDelegate

に準拠したコントローラを渡していないことは全くでデリゲートを渡し、それをアウトを割り当てない方が簡単かもしれません拡張のide。