Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // GradientButton.swift
- // DreamJay
- //
- // Created by Mateusz Nadolski on 24/02/2017.
- // Copyright © 2017 Tooploox. All rights reserved.
- //
- import UIKit
- class GradientButton: Button {
- let buttonSize: CGFloat
- let fillSize: CGFloat
- private let fillView = ViewsFactory.gradientView(withColors: [UIColor.basic1, UIColor.basic9])
- private let innerBackgroundView = ViewsFactory.gradientView(
- withColors: [UIColor.basic1.withAlphaComponent(0.05), UIColor.basic9.withAlphaComponent(0.05)]
- )
- private let outerBackgroundView = ViewsFactory.gradientView(
- withColors: [UIColor.basic1.withAlphaComponent(0.03), UIColor.basic9.withAlphaComponent(0.03)]
- )
- init(buttonSize: CGFloat, fillSize: CGFloat) {
- self.buttonSize = buttonSize
- self.fillSize = fillSize
- super.init(frame: .zero)
- addSubviews()
- setupConstraints()
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- private func addSubviews() {
- addSubview(outerBackgroundView)
- addSubview(innerBackgroundView)
- addSubview(fillView)
- }
- private func setupConstraints() {
- let constraints = Array([
- constraintsForFillView(),
- constraintsForInnerBackgroundView(),
- constraintsForOuterBackgroundView()
- ].joined())
- NSLayoutConstraint.activate(constraints)
- }
- private func constraintsForFillView() -> [NSLayoutConstraint] {
- return [
- fillView.widthAnchor.constraint(equalToConstant: fillSize),
- fillView.heightAnchor.constraint(equalToConstant: fillSize),
- fillView.centerXAnchor.constraint(equalTo: centerXAnchor),
- fillView.centerYAnchor.constraint(equalTo: centerYAnchor)
- ]
- }
- private func constraintsForInnerBackgroundView() -> [NSLayoutConstraint] {
- let size = (buttonSize - fillSize) / 2.0 + fillSize
- return [
- innerBackgroundView.widthAnchor.constraint(equalToConstant: size),
- innerBackgroundView.heightAnchor.constraint(equalToConstant: size),
- innerBackgroundView.centerXAnchor.constraint(equalTo: centerXAnchor),
- innerBackgroundView.centerYAnchor.constraint(equalTo: centerYAnchor)
- ]
- }
- private func constraintsForOuterBackgroundView() -> [NSLayoutConstraint] {
- return [
- outerBackgroundView.widthAnchor.constraint(equalToConstant: buttonSize),
- outerBackgroundView.heightAnchor.constraint(equalToConstant: buttonSize),
- outerBackgroundView.centerXAnchor.constraint(equalTo: centerXAnchor),
- outerBackgroundView.centerYAnchor.constraint(equalTo: centerYAnchor)
- ]
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement