| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- //
- // ViewExtensions.swift
- // Sirani
- //
- // Created by Anhar Solehudin on 30/03/19.
- // Copyright © 2019 Anhar Solehudin. All rights reserved.
- //
- import UIKit
- extension UIView {
-
- // show / hide view
- func show() {
- isHidden = false
- }
-
- func hide() {
- isHidden = true
- }
-
- // add auto layout contraints on view
- func anchor(top: NSLayoutYAxisAnchor?, bottom: NSLayoutYAxisAnchor?,
- leading: NSLayoutXAxisAnchor?, trailing: NSLayoutXAxisAnchor?,
- padding: UIEdgeInsets = .zero, size: CGSize = .zero) {
-
- translatesAutoresizingMaskIntoConstraints = false
-
- if size.width > 0 {
- widthAnchor.constraint(equalToConstant: size.width).isActive = true
- }
-
- if size.height > 0 {
- heightAnchor.constraint(equalToConstant: size.height).isActive = true
- }
-
- if let top = top {
- topAnchor.constraint(equalTo: top, constant: padding.top).isActive = true
- }
- if let bottom = bottom {
- bottomAnchor.constraint(equalTo: bottom, constant: -padding.bottom).isActive = true
- }
- if let leading = leading {
- leadingAnchor.constraint(equalTo: leading, constant: padding.left).isActive = true
- }
- if let trailing = trailing {
- trailingAnchor.constraint(equalTo: trailing, constant: -padding.right).isActive = true
- }
-
- }
-
- // rotate view
- func rotate(_ toValue: CGFloat, duration: CFTimeInterval = 0.3) {
- let animation = CABasicAnimation(keyPath: "transform.rotation")
- animation.toValue = toValue
- animation.duration = duration
- animation.isRemovedOnCompletion = false
- animation.fillMode = CAMediaTimingFillMode.forwards
- self.layer.add(animation, forKey: nil)
- }
-
- }
|