Created
February 28, 2018 18:10
-
-
Save PaulSolt/3717590ec8bc626a82117d8073971c62 to your computer and use it in GitHub Desktop.
Animate Auto Layout Constraints Swift 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// SwiftButton | |
// | |
// Created by Paul Solt on 7/21/15. | |
// Copyright (c) 2015 Paul Solt. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
var updateTimer: Timer? | |
var startDate: Date! | |
var buttonOn: Bool = false | |
var button1: UIButton! | |
var button2: UIButton! | |
var bottomButtonConstraint: NSLayoutConstraint! | |
@IBOutlet weak var bottomPanelConstraint: NSLayoutConstraint! | |
// Storyboard connections | |
@IBOutlet weak var timeLabel: UILabel! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
updateTimer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(updateTimer(timer:)), userInfo: nil, repeats: true) | |
startDate = Date() | |
setupButton() | |
// button1.alpha = 0 | |
setupAnimateButton() | |
} | |
func setupButton() { | |
button1 = UIButton(type: .system) | |
button1.setTitle("Animate Auto Layout", for: .normal) | |
button1.bounds = CGRect(x: 0, y: 0, width: 300, height: 80) | |
button1.center = CGPoint(x: view.bounds.width / 2, y: 80 + 20 + 40 + 8 + 8) // 80 point button, 20 point status bar, 40 point half button, 8 point margins | |
button1.setBackgroundImage(UIImage(named: "BlueButton"), for: .normal) | |
button1.titleLabel?.font = UIFont.systemFont(ofSize: 17) | |
button1.setTitleColor(.white, for: .normal) | |
button1.addTarget(self, action: #selector(animateButtonPressed(sender:)), for: .touchUpInside) | |
view.addSubview(button1) | |
} | |
func setupAnimateButton() { | |
button2 = UIButton(type: .system) | |
button2.setTitle("Auto Layout Slide", for: .normal) | |
button2.setBackgroundImage(UIImage(named: "BlueButton"), for: .normal) | |
button2.titleLabel?.font = UIFont.systemFont(ofSize:17) | |
button2.setTitleColor(.white, for: .normal) | |
view.addSubview(button2) | |
// Auto Layout | |
// Disable Auto Resizing constraints | |
button2.translatesAutoresizingMaskIntoConstraints = false | |
let margins = view.layoutMarginsGuide | |
button2.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true | |
button2.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true | |
// Store a reference to the constraint to manage it | |
bottomButtonConstraint = button2.topAnchor.constraint(equalTo: margins.bottomAnchor, constant: 0) | |
bottomButtonConstraint.isActive = true | |
// offscreen + invisible | |
// button2.alpha = 0 | |
bottomButtonConstraint.constant = 0 | |
} | |
func showButton() { | |
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 15, options: [], animations: { () -> Void in | |
self.button2.alpha = 1 | |
self.bottomButtonConstraint.constant = -100 | |
self.bottomPanelConstraint.constant = self.button2.frame.height + 20 + 8 // button height + bottom spacing + in-between spacing | |
self.view.layoutIfNeeded() | |
}, completion: nil) | |
} | |
func hideButton() { | |
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 15, options: [], animations: { () -> Void in | |
self.button2.alpha = 0 | |
self.bottomButtonConstraint.constant = 0 // 100 | |
self.bottomPanelConstraint.constant = 20 | |
self.view.layoutIfNeeded() | |
}, completion: nil) | |
} | |
@IBAction func addButtonPressed(_ sender: Any) { | |
print("add") | |
UIView.animate(withDuration: 0.3, animations: { () -> Void in | |
if self.button1.alpha == 0 { | |
self.button1.alpha = 1 // show | |
} else { | |
self.button1.alpha = 0 // hide | |
} | |
}) | |
} | |
@objc func animateButtonPressed(sender: AnyObject) { | |
print("animate") | |
if buttonOn { | |
hideButton() | |
} else { | |
showButton() | |
} | |
buttonOn = !buttonOn | |
} | |
// MARK: Timer Logic | |
@objc func updateTimer(timer: Timer) { | |
let seconds = -startDate.timeIntervalSinceNow | |
let dateComponentFormatter = DateComponentsFormatter() | |
dateComponentFormatter.unitsStyle = .positional | |
dateComponentFormatter.allowedUnits = [.minute, .second, .hour] | |
dateComponentFormatter.zeroFormattingBehavior = .pad | |
let timeString = dateComponentFormatter.string(from: seconds) | |
timeLabel.text = timeString | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment