2017-07-14 7 views
0

ホームボタンを押してアプリを再起動すると、アニメーションが停止しました。設定ボタンが回転しなくなり、まばたきのラベルが消えるだけです。ホームボタンを押したときにアニメーションが機能しなくなり、アプリが再起動されます

点滅アニメーション:

extension UILabel { 

    func startBlink() { 
     UIView.animate(withDuration: 0.8, 
         delay:0.0, 
         options:[.autoreverse, .repeat], 
         animations: { 
         self.alpha = 0 

     }, completion: nil) 
    } 
} 

回転アニメーション:ここでは、両方のアニメーションのための私のコードは、私はそれを実行

extension UIButton { 

    func startRotating() { 

     UIView.animate(withDuration: 4.0, delay: 0.0, options:[.autoreverse, .repeat,UIViewAnimationOptions.allowUserInteraction], animations: { 

      self.transform = CGAffineTransform(rotationAngle: CGFloat.pi) 


     }, completion: nil)          
    }  
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    settingsButton.layer.cornerRadius = 0.5 * settingsButton.bounds.size.width 
    settingsButton.clipsToBounds = true 
    settingsButton.imageView?.contentMode = .scaleAspectFit 



    NotificationCenter.default.addObserver(self, selector: #selector(appMovedToForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil) 


} 

func appMovedToForeground() { 
    tapToPlayLabel.startBlink() 
    settingsButton.startRotating() 
    print("DID") 
} 
+3

をCALayerAnimationを追加することができますがあなたの代わりに '' willEnterForeground'のdidBecomeActive'通知を使用してみましたが?私はアニメーションが動作していると仮定していますが、コードは 'willEnterForeground'で呼び出されているので、アニメーションが表示されません – Malik

+0

@マリックあなたは答えとしてあなたのコメントを追加する必要があります – klinger

+0

私はそれがうまくいくと仮定しています。ヘッドアップをありがとう。ちょうど答えとして投稿 – Malik

答えて

1

あなたが持っているあなたのアニメーションを再起動するには以下のことを行うには、以下のコードを確認してください。

チェック延長

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet weak var tapToPlayLabel: UILabel! 
    @IBOutlet weak var settingsButton: UIButton! 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     settingsButton.layer.cornerRadius = settingsButton.frame.size.width/2 
     settingsButton.clipsToBounds = true 
     //settingsButton.imageView?.contentMode = .scaleAspectFit 

     settingsButton.startRotating() 
     tapToPlayLabel.startBlink() 

     NotificationCenter.default.addObserver(self, selector: #selector(appMovedToForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil) 
    } 

    func appMovedToForeground() { 
     self.tapToPlayLabel.startBlink() 
     self.settingsButton.startRotating() 
    } 
} 

extension UILabel { 
    func startBlink() { 
     self.alpha = 1 
     UIView.animate(withDuration: 0.8, 
         delay:0.0, 
         options:[.autoreverse, .repeat], 
         animations: { 
         self.alpha = 0 

     }, completion: nil) 
    } 
} 

extension UIButton { 

    func startRotating() { 
     self.transform = CGAffineTransform(rotationAngle: CGFloat.pi/2) 
     UIView.animate(withDuration: 4.0, delay: 0.0, options:[.autoreverse, .repeat,UIViewAnimationOptions.allowUserInteraction], animations: { 
      self.transform = CGAffineTransform(rotationAngle: CGFloat.pi) 
     }, completion: nil) 
    } 
} 

出力

enter image description here

+0

これはあなたの仕事ですか? –

0

アプリのデリゲートは、実行中にすでに存在しているように私はあなたが少し遅れて、あなたのアニメーションを実行する必要があると思いますアプリはフォアグラウンドに移動しています。

0

それとも、UILabel

extension UILabel { 

    func startBlink() { 

     let scaleAnimation = CAKeyframeAnimation(keyPath: "transform") 
     scaleAnimation.delegate = self as? CAAnimationDelegate 
     let transform: CATransform3D = CATransform3DMakeScale(1.5, 1.5, 1) 
     scaleAnimation.values = [NSValue(caTransform3D: CATransform3DIdentity), NSValue(caTransform3D: transform), NSValue(caTransform3D: CATransform3DIdentity)] 
     scaleAnimation.duration = 0.5 
     scaleAnimation.repeatCount = 100000000 
     self.layer.add(scaleAnimation as? CAAnimation ?? CAAnimation(), forKey: "scaleText") 
    } 
    func startRotating() { 

     let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z") 
     rotation.fromValue = 0 
     rotation.toValue = NSNumber(value: Double.pi * 2) 
     rotation.duration = 2 
     rotation.isCumulative = true 
     rotation.repeatCount = .greatestFiniteMagnitude 
     self.layer.add(rotation, forKey: "rotationAnimation") 

    } 
} 
関連する問題