Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.83 KB | None | 0 0
  1. //
  2. //  GradientButton.swift
  3. //  DreamJay
  4. //
  5. //  Created by Mateusz Nadolski on 24/02/2017.
  6. //  Copyright © 2017 Tooploox. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class GradientButton: Button {
  12.    
  13.     let buttonSize: CGFloat
  14.     let fillSize: CGFloat
  15.    
  16.     private let fillView = ViewsFactory.gradientView(withColors: [UIColor.basic1, UIColor.basic9])
  17.     private let innerBackgroundView = ViewsFactory.gradientView(
  18.         withColors: [UIColor.basic1.withAlphaComponent(0.05), UIColor.basic9.withAlphaComponent(0.05)]
  19.     )
  20.     private let outerBackgroundView = ViewsFactory.gradientView(
  21.         withColors: [UIColor.basic1.withAlphaComponent(0.03), UIColor.basic9.withAlphaComponent(0.03)]
  22.     )
  23.    
  24.     init(buttonSize: CGFloat, fillSize: CGFloat) {
  25.         self.buttonSize = buttonSize
  26.         self.fillSize = fillSize
  27.         super.init(frame: .zero)
  28.         addSubviews()
  29.         setupConstraints()
  30.     }
  31.    
  32.     required init?(coder aDecoder: NSCoder) {
  33.         fatalError("init(coder:) has not been implemented")
  34.     }
  35.    
  36.     private func addSubviews() {
  37.         addSubview(outerBackgroundView)
  38.         addSubview(innerBackgroundView)
  39.         addSubview(fillView)
  40.     }
  41.    
  42.     private func setupConstraints() {
  43.         let constraints = Array([
  44.             constraintsForFillView(),
  45.             constraintsForInnerBackgroundView(),
  46.             constraintsForOuterBackgroundView()
  47.         ].joined())
  48.         NSLayoutConstraint.activate(constraints)
  49.     }
  50.    
  51.     private func constraintsForFillView() -> [NSLayoutConstraint] {
  52.         return [
  53.             fillView.widthAnchor.constraint(equalToConstant: fillSize),
  54.             fillView.heightAnchor.constraint(equalToConstant: fillSize),
  55.             fillView.centerXAnchor.constraint(equalTo: centerXAnchor),
  56.             fillView.centerYAnchor.constraint(equalTo: centerYAnchor)
  57.         ]
  58.     }
  59.    
  60.     private func constraintsForInnerBackgroundView() -> [NSLayoutConstraint] {
  61.         let size = (buttonSize - fillSize) / 2.0 + fillSize
  62.         return [
  63.             innerBackgroundView.widthAnchor.constraint(equalToConstant: size),
  64.             innerBackgroundView.heightAnchor.constraint(equalToConstant: size),
  65.             innerBackgroundView.centerXAnchor.constraint(equalTo: centerXAnchor),
  66.             innerBackgroundView.centerYAnchor.constraint(equalTo: centerYAnchor)
  67.         ]
  68.     }
  69.    
  70.     private func constraintsForOuterBackgroundView() -> [NSLayoutConstraint] {
  71.         return [
  72.             outerBackgroundView.widthAnchor.constraint(equalToConstant: buttonSize),
  73.             outerBackgroundView.heightAnchor.constraint(equalToConstant: buttonSize),
  74.             outerBackgroundView.centerXAnchor.constraint(equalTo: centerXAnchor),
  75.             outerBackgroundView.centerYAnchor.constraint(equalTo: centerYAnchor)
  76.         ]
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement