2017-06-05 27 views
1

カスタムビューをプログラムで作成していますが、正常に動作しています。次に、ビューをアニメーション化しようとしています。いくつかの奇妙な理由から、ビューは全くアニメーション化されていますが、私はなぜそれがわかりません。コードは次のとおりです。カスタムビューをプログラムで作成してからアニメーションを作成する

override func viewDidLoad() { 
    super.viewDidLoad() 


    let customView = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100)) 

    customView.backgroundColor = UIColor.blue 
    customView.layer.cornerRadius = 25 
    customView.layer.borderWidth = 8 
    customView.layer.borderColor = UIColor.red.cgColor 

    self.view.addSubview(customView) 

    UIView.animate(withDuration: 4, animations: { 

     customView.transform.translatedBy(x: 40, y: 60) 
     customView.transform.rotated(by: CGFloat.pi/2) 
     customView.transform.scaledBy(x: 0, y: 0.5) 

    }) 
    } 

答えて

1

このアニメーションブロックをviewDidAppearで呼び出すと、少し時間がかかることがあります。

import UIKit 

class ViewController: UIViewController { 

    var customView = UIView() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     customView = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100)) 
     customView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap))) 

     customView.backgroundColor = UIColor.blue 
     customView.layer.cornerRadius = 25 
     customView.layer.borderWidth = 8 
     customView.layer.borderColor = UIColor.red.cgColor 

     self.view.addSubview(customView) 
    } 

    override func viewDidAppear(_ animated: Bool) { 
     UIView.animate(withDuration: 5, delay: 1, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { 
      self.customView.transform = CGAffineTransform(scaleX: 1, y: 0.5) 
      self.customView.transform = CGAffineTransform(rotationAngle: CGFloat.pi/2) 
      self.customView.transform = CGAffineTransform(translationX: 40, y: 60) 
     }, completion: nil) 
    } 

    func handleTap() { 
     UIView.animate(withDuration: 5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { 
      self.customView.transform = CGAffineTransform(scaleX: 1, y: 0.5) 
      self.customView.transform = CGAffineTransform(rotationAngle: CGFloat.pi/2) 
      self.customView.transform = CGAffineTransform(translationX: 40, y: 60) 
     }, completion: nil) 
    } 

} 
+0

本当に助けてくれた!ありがとう!! –

0

あなたの質問は正しくありません。あなたのビューはアニメーション化されていません。

なぜですか?

リターン関数を使用しています。カスタムビューは、このように変更されたバージョンを取得する必要があります。

customView.transform = customView.transform.translatedBy(x: 40, y: 60) 

コードはこのようになります。

てみましょうのCustomView =のUIView(フレーム:CGRect(X:10、Y:10、幅:100、高さ:100))

customView.backgroundColor = UIColor.blue 
    customView.layer.cornerRadius = 25 
    customView.layer.borderWidth = 8 
    customView.layer.borderColor = UIColor.red.cgColor 

    self.view.addSubview(customView) 

    UIView.animate(withDuration: 4, animations: { 

     customView.transform = customView.transform.translatedBy(x: 40, y: 60) 
     //customView.transform = customView.transform.rotated(by: CGFloat.pi/2) 
     //customView.transform = customView.transform.scaledBy(x: 0, y: 0.5) 

    }) 

は、オンラインリソースがたくさんありますどのようにグループanimation.Justグーグルをしますそれ:

関連する問題