Last active
July 24, 2019 05:15
-
-
Save arturdent/633154f1133e4f09c480e317560a6264 to your computer and use it in GitHub Desktop.
prospal vse na svete
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
import UIKit | |
import PlaygroundSupport | |
public class DecoratedButton: UIButton { | |
var radius: CGFloat = 50 { | |
didSet { | |
self.setupDecoratedLayers() | |
} | |
} | |
var lineWidth: CGFloat = 10 { | |
didSet { | |
self.setupDecoratedLayers() | |
} | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setupDecoratedLayers() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
private func setupDecoratedLayers() { | |
layer.cornerRadius = radius | |
clipsToBounds = true | |
let path = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.radius) | |
/// Right Bottom Corner | |
// .--. | |
// | | | |
// .--* | |
let RightBottomCornerLayer = CALayer() | |
RightBottomCornerLayer.frame = CGRect(origin: .zero, size: CGSize(width: self.frame.size.width, height: self.frame.size.height)) | |
RightBottomCornerLayer.backgroundColor = UIColor.yellow.cgColor | |
/// Left Top Corner | |
// *--. | |
// | | | |
// .--. | |
let leftTopCornerLayer = CALayer() | |
leftTopCornerLayer.frame = CGRect(origin: .zero, size: CGSize(width: (self.frame.size.width)/2.0, height: (self.frame.size.height))) | |
leftTopCornerLayer.backgroundColor = UIColor.red.cgColor | |
/// Left Bottom Corner | |
// .--. | |
// | | | |
// *--. | |
let leftBottomCornerLayer = CALayer() | |
leftBottomCornerLayer.frame = CGRect(origin: .zero, size: CGSize(width: (self.frame.size.width)/2.0, height: (self.frame.size.height)/2.0)) | |
leftBottomCornerLayer.backgroundColor = UIColor.green.cgColor | |
/// Right Top Corner | |
// .--* | |
// | | | |
// .--. | |
let RightTopCornerLayer = CALayer() | |
RightTopCornerLayer.frame = CGRect(origin: .zero, size: CGSize(width: (self.frame.size.width), height: (self.frame.size.height)/2.0)) | |
RightTopCornerLayer.backgroundColor = UIColor.blue.cgColor | |
let layers = [RightBottomCornerLayer, leftTopCornerLayer, RightTopCornerLayer, leftBottomCornerLayer] | |
layers.map { layer in | |
let shape = CAShapeLayer() | |
shape.path = path.cgPath | |
shape.lineWidth = self.lineWidth | |
shape.strokeColor = UIColor.black.cgColor | |
shape.fillColor = UIColor.clear.cgColor | |
layer.mask = shape | |
self.layer.addSublayer(layer) | |
} | |
} | |
override public func layoutSubviews() { | |
super.layoutSubviews() | |
setupDecoratedLayers() | |
} | |
} | |
let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500)) | |
view.backgroundColor = #colorLiteral(red: 0.909803926944733, green: 0.47843137383461, blue: 0.643137276172638, alpha: 1.0) | |
let button = DecoratedButton(frame: CGRect(x: 50, y: 100, width: 400, height: 100)) | |
button.backgroundColor = #colorLiteral(red: 0.721568644046783, green: 0.886274516582489, blue: 0.592156887054443, alpha: 1.0) | |
view.addSubview(sub) | |
PlaygroundPage.current.liveView = view |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment