Skip to content

Instantly share code, notes, and snippets.

@Eluss
Created November 6, 2016 13:28
Show Gist options
  • Save Eluss/d696cfbcc8e62d2c62d2ecbc7fb7b7e0 to your computer and use it in GitHub Desktop.
Save Eluss/d696cfbcc8e62d2c62d2ecbc7fb7b7e0 to your computer and use it in GitHub Desktop.

Revisions

  1. Eluss created this gist Nov 6, 2016.
    36 changes: 36 additions & 0 deletions MoveAnchor.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    var buttonLeftConstraint: NSLayoutConstraint?

    func pureLayout() {
    buttonLeftConstraint = animateButton.autoPinEdge(toSuperviewEdge: .left, withInset: 20)
    animateButton.autoPinEdge(toSuperviewEdge: .top, withInset: 50)
    animateButton.autoSetDimensions(to: CGSize(width: 100, height: 100))
    }

    func animate() {
    UIView.animate(withDuration: 1) {
    let random: Double = Double(arc4random_uniform(200))
    self.buttonLeftConstraint?.constant = random
    self.view.layoutIfNeeded()
    }
    }

    ///

    var buttonLeftConstraint: NSLayoutConstraint?

    func anchorLayout() {
    animateButton.translatesAutoresizingMaskIntoConstraints = false
    buttonLeftConstraint = animateButton.leftAnchor.constraint(lessThanOrEqualTo: view.leftAnchor, constant: 20)
    buttonLeftConstraint?.isActive = true
    animateButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
    animateButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
    animateButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
    }

    func animate() {
    UIView.animate(withDuration: 1) {
    let random: CGFloat = CGFloat(arc4random_uniform(200))
    self.buttonLeftConstraint?.constant = random
    self.view.layoutIfNeeded()
    }
    }