Created
March 17, 2017 15:31
-
-
Save npu3pak/bb350a7a3fd7d5ce0ffd2add41043071 to your computer and use it in GitHub Desktop.
Proof of concept. Индикатор загрузки для любого View
This file contains hidden or 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
//: Playground - noun: a place where people can play | |
import UIKit | |
extension UIView { | |
class Indicator: UIView { | |
} | |
func showIndicator() { | |
guard let superview = superview else { | |
print("Сначала добавьте кнопку") | |
return | |
} | |
let buttonFrame = frame | |
removeFromSuperview() | |
let indicator = self.indicator() | |
superview.addSubview(indicator) | |
indicator.frame = CGRect(x: buttonFrame.minX, y: buttonFrame.minY, width: buttonFrame.width + 20, height: buttonFrame.height + 20) | |
superview.addSubview(self) | |
indicator.center = center | |
//Если поругались констрейнты | |
indicator.translatesAutoresizingMaskIntoConstraints = false | |
} | |
func hideIndicator() { | |
let indicator = superview?.subviews.first(where: {$0 is Indicator}) | |
indicator?.removeFromSuperview() | |
} | |
func indicator() -> Indicator { | |
let indicator = Indicator(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) | |
indicator.backgroundColor = UIColor.yellow | |
return indicator | |
} | |
} | |
let button = UIView(frame: CGRect(x: 30, y: 30, width: 40, height: 40)) | |
button.backgroundColor = UIColor.blue | |
button.layer.cornerRadius = 20 | |
var container = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) | |
container.backgroundColor = UIColor.red | |
//Начинаем | |
container.addSubview(button) | |
container | |
button.showIndicator() | |
container | |
button.hideIndicator() | |
container |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment