2017-05-18 10 views
0

ビューを画面の下からスライドさせる方法をアニメーション化したいと思います。しかし、私は代わりにこれを行うことはできませんが、私は中心からジオメトリを変更するアニメーションを取得します。制約の間違ったアニメーション

class Notes: UIView { 

var topConstraint: NSLayoutConstraint? 

lazy var overlay: UIView = { 
    let view = UIView(frame: UIScreen.main.bounds) 
    view.backgroundColor = .black 
    view.alpha = 0.7 
    view.autoresizingMask = [.flexibleWidth, .flexibleHeight] 
    return view 
}() 

lazy var contentView: UIView = { 
    let view = UIView() 
    view.backgroundColor = .white 
    view.alpha = 1 
    view.translatesAutoresizingMaskIntoConstraints = false 
    view.layer.cornerRadius = 5 
    return view 
}() 

override init(frame: CGRect) { 
    super.init(frame: UIScreen.main.bounds) 

    setupViews() 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

func setupViews() { 
    addSubview(overlay) 
    addSubview(contentView) 

    contentView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 16).isActive = true 
    contentView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -16).isActive = true 
    contentView.heightAnchor.constraint(equalToConstant: 400).isActive = true 
    topConstraint = contentView.topAnchor.constraint(equalTo: self.topAnchor, constant: 800) 

    topConstraint?.isActive = true 
} 

func animate() { 

    self.topConstraint?.constant = 80 

    UIView.animate(withDuration: 0.3, animations: { 
     self.contentView.layoutIfNeeded() 
    }) 
} 

}

次これは私が間違って何をやっている

guard let window = UIApplication.shared.keyWindow else { return } 
let notes = Notes() 
window.addSubview(notes) 
notes.animate() 

をキックオフする方法ですこれは、ビューのこの

https://i.stack.imgur.com/aIYwC.gif

コードのように見えますか?

答えて

0

あなたの制約もsetupViews()機能の終わりに再び

class Notes: UIView { 

var topConstraint: NSLayoutConstraint? 

lazy var overlay: UIView = { 
    let view = UIView(frame: UIScreen.main.bounds) 
    view.backgroundColor = .black 
    view.alpha = 0.7 
    view.autoresizingMask = [.flexibleWidth, .flexibleHeight] 
    return view 
}() 

lazy var contentView: UIView = { 
    let view = UIView() 
    view.backgroundColor = .green 
    view.alpha = 1 
    view.translatesAutoresizingMaskIntoConstraints = false 
    view.layer.cornerRadius = 5 
    return view 
}() 

override init(frame: CGRect) { 
    super.init(frame: UIScreen.main.bounds) 

    setupViews() 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

func setupViews() { 
    addSubview(overlay) 
    addSubview(contentView) 

    topConstraint = contentView.topAnchor.constraint(equalTo: self.topAnchor, constant: 800) 
    // you can activate your constraints directly like this 
    // I changed left and right to leading and trailing but that's a matter of choice 
    // remember if your supporting left to right languages in your app then not use left and right better use Leading and Trailing 
    NSLayoutConstraint.activate([ 
     contentView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 16), 
     contentView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -16), 
     contentView.heightAnchor.constraint(equalToConstant: 400), 
     topConstraint! 
     ]) 

} 

func animate() { 

    self.topConstraint?.constant = 80 

    UIView.animate(withDuration: 0.3, animations: { 
     // here you should call self.layoutIfNeeded so self which is a UIView should layout again 
     self.layoutIfNeeded() 
    }) 
    } 



} 
0
  1. コールself.layoutIfNeeded()をレイアウトする間違ったビューを呼び出す正しい方法を活性化されていません。
  2. コンテンツビューではなく、機能self.layoutIfNeeded()をアニメートします。

すべてが正常です。

class Notes: UIView { 

    var topConstraint: NSLayoutConstraint? 

    lazy var overlay: UIView = { 
     let view = UIView(frame: UIScreen.main.bounds) 
     view.backgroundColor = .black 
     view.alpha = 0.7 
     view.autoresizingMask = [.flexibleWidth, .flexibleHeight] 
     return view 
    }() 

    lazy var contentView: UIView = { 
     let view = UIView() 
     view.backgroundColor = .white 
     view.alpha = 1 
     view.translatesAutoresizingMaskIntoConstraints = false 
     view.layer.cornerRadius = 5 
     return view 
    }() 

    override init(frame: CGRect) { 
     super.init(frame: UIScreen.main.bounds) 

     setupViews() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    func setupViews() { 
     addSubview(overlay) 
     addSubview(contentView) 

     contentView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 16).isActive = true 
     contentView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -16).isActive = true 
     contentView.heightAnchor.constraint(equalToConstant: 400).isActive = true 
     topConstraint = contentView.topAnchor.constraint(equalTo: self.topAnchor, constant: 800) 

     topConstraint?.isActive = true 
     self.layoutIfNeeded() 
    } 

    func animate() { 

     self.topConstraint?.constant = 80 

     UIView.animate(withDuration: 0.3, animations: { 
      self.layoutIfNeeded() 
     }) 
    } 
} 
関連する問題