Instantly share code, notes, and snippets.
Created
October 24, 2018 13:12
-
Star
(1)
1
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save DominatorVbN/0dac2826f683eb90b3ac4fc5d79a3f3b to your computer and use it in GitHub Desktop.
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
// | |
// PushButton.swift | |
// Flo | |
// | |
// Created by mac on 24/10/18. | |
// Copyright © 2018 Dominator. All rights reserved. | |
// | |
import UIKit | |
@IBDesignable | |
class PushButton: UIButton { | |
@IBInspectable var plusLineWidth : CGFloat = 3.0{ | |
didSet{ | |
setNeedsDisplay() | |
} | |
} | |
@IBInspectable var plusButtonScale : CGFloat = 0.6{ | |
didSet{ | |
setNeedsDisplay() | |
} | |
} | |
@IBInspectable var halfPointShift : CGFloat = 0.5{ | |
didSet{ | |
setNeedsDisplay() | |
} | |
} | |
@IBInspectable var fillColor: UIColor = UIColor.green | |
@IBInspectable var isAddButton: Bool = true | |
private var halfWidth: CGFloat { | |
return bounds.width / 2 | |
} | |
private var halfHeight: CGFloat { | |
return bounds.height / 2 | |
} | |
override func draw(_ rect: CGRect) { | |
let path = UIBezierPath(ovalIn: rect) | |
fillColor.setFill() | |
path.fill() | |
//set up the width and height variables | |
//for the horizontal stroke | |
let plusWidth: CGFloat = min(bounds.width, bounds.height) * self.plusButtonScale | |
let halfPlusWidth = plusWidth / 2 | |
//create the path | |
let plusPath = UIBezierPath() | |
//set the path's line width to the height of the stroke | |
plusPath.lineWidth = self.plusLineWidth | |
//move the initial point of the path | |
//to the start of the horizontal stroke | |
plusPath.move(to: CGPoint( | |
x: halfWidth - halfPlusWidth - halfPointShift + self.halfPointShift, | |
y: halfHeight + self.halfPointShift)) | |
//add a point to the path at the end of the stroke | |
plusPath.addLine(to: CGPoint( | |
x: halfWidth + halfPlusWidth + self.halfPointShift, | |
y: halfHeight + self.halfPointShift)) | |
if isAddButton { | |
plusPath.move(to: CGPoint( | |
x: halfHeight - self.halfPointShift , | |
y: halfWidth - halfPlusWidth + self.halfPointShift)) | |
plusPath.addLine(to: CGPoint( | |
x: halfHeight - self.halfPointShift, | |
y: halfWidth + halfPlusWidth + self.halfPointShift)) | |
} | |
//set the stroke color | |
UIColor.white.setStroke() | |
//draw the stroke | |
plusPath.stroke() | |
setup() | |
} | |
@IBInspectable open var ripplePercent: Float = 0.8 { | |
didSet { | |
setupRippleView() | |
} | |
} | |
@IBInspectable open var rippleColor: UIColor = UIColor(white: 0.9, alpha: 1) { | |
didSet { | |
rippleView.backgroundColor = rippleColor | |
} | |
} | |
@IBInspectable open var rippleBackgroundColor: UIColor = .clear { | |
didSet { | |
rippleBackgroundView.backgroundColor = rippleBackgroundColor | |
} | |
} | |
@IBInspectable open var rippleOverBounds: Bool = false | |
@IBInspectable open var shadowRippleRadius: Float = 1 | |
@IBInspectable open var shadowRippleEnable: Bool = true | |
@IBInspectable open var trackTouchLocation: Bool = false | |
@IBInspectable open var touchUpAnimationTime: Double = 0.6 | |
let rippleView = UIView() | |
let rippleBackgroundView = UIView() | |
fileprivate var tempShadowRadius: CGFloat = 0 | |
fileprivate var tempShadowOpacity: Float = 0 | |
fileprivate var touchCenterLocation: CGPoint? | |
fileprivate var rippleMask: CAShapeLayer? { | |
get { | |
if !rippleOverBounds { | |
let maskLayer = CAShapeLayer() | |
maskLayer.path = UIBezierPath(roundedRect: bounds, | |
cornerRadius: layer.cornerRadius).cgPath | |
return maskLayer | |
} else { | |
return nil | |
} | |
} | |
} | |
fileprivate func setup() { | |
setupRippleView() | |
rippleBackgroundView.backgroundColor = rippleBackgroundColor.withAlphaComponent(0.5) | |
rippleBackgroundView.frame = bounds | |
rippleBackgroundView.layer.cornerRadius = bounds.height/2 | |
rippleBackgroundView.layer.masksToBounds = true | |
rippleBackgroundView.addSubview(rippleView) | |
rippleBackgroundView.alpha = 0 | |
addSubview(rippleBackgroundView) | |
layer.shadowRadius = 0 | |
layer.shadowOffset = CGSize(width: 0, height: 1) | |
layer.shadowColor = UIColor(white: 0.0, alpha: 0.5).cgColor | |
} | |
fileprivate func setupRippleView() { | |
let size: CGFloat = bounds.width * CGFloat(ripplePercent) | |
let x: CGFloat = (bounds.width/2) - (size/2) | |
let y: CGFloat = (bounds.height/2) - (size/2) | |
let corner: CGFloat = size/2 | |
rippleView.backgroundColor = rippleColor | |
rippleView.frame = CGRect(x: x, y: y, width: size, height: size) | |
rippleView.layer.cornerRadius = corner | |
} | |
override open func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { | |
if trackTouchLocation { | |
touchCenterLocation = touch.location(in: self) | |
} else { | |
touchCenterLocation = nil | |
} | |
UIView.animate(withDuration: 0.1, delay: 0, options: UIView.AnimationOptions.allowUserInteraction, animations: { | |
self.rippleBackgroundView.alpha = 1 | |
}, completion: nil) | |
rippleView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5) | |
UIView.animate(withDuration: 0.7, delay: 0, options: [UIView.AnimationOptions.curveEaseOut, UIView.AnimationOptions.allowUserInteraction], | |
animations: { | |
self.rippleView.transform = CGAffineTransform.identity | |
}, completion: nil) | |
if shadowRippleEnable { | |
tempShadowRadius = layer.shadowRadius | |
tempShadowOpacity = layer.shadowOpacity | |
let shadowAnim = CABasicAnimation(keyPath:"shadowRadius") | |
shadowAnim.toValue = shadowRippleRadius | |
let opacityAnim = CABasicAnimation(keyPath:"shadowOpacity") | |
opacityAnim.toValue = 1 | |
let groupAnim = CAAnimationGroup() | |
groupAnim.duration = 0.7 | |
groupAnim.fillMode = CAMediaTimingFillMode.forwards | |
groupAnim.isRemovedOnCompletion = false | |
groupAnim.animations = [shadowAnim, opacityAnim] | |
layer.add(groupAnim, forKey:"shadow") | |
} | |
return super.beginTracking(touch, with: event) | |
} | |
override open func cancelTracking(with event: UIEvent?) { | |
super.cancelTracking(with: event) | |
animateToNormal() | |
} | |
override open func endTracking(_ touch: UITouch?, with event: UIEvent?) { | |
super.endTracking(touch, with: event) | |
animateToNormal() | |
} | |
fileprivate func animateToNormal() { | |
UIView.animate(withDuration: 0.1, delay: 0, options: UIView.AnimationOptions.allowUserInteraction, animations: { | |
self.rippleBackgroundView.alpha = 1 | |
}, completion: {(success: Bool) -> () in | |
UIView.animate(withDuration: self.touchUpAnimationTime, delay: 0, options: UIView.AnimationOptions.allowUserInteraction, animations: { | |
self.rippleBackgroundView.alpha = 0 | |
}, completion: nil) | |
}) | |
UIView.animate(withDuration: 0.7, delay: 0, | |
options: [.curveEaseOut, .beginFromCurrentState, .allowUserInteraction], | |
animations: { | |
self.rippleView.transform = CGAffineTransform.identity | |
let shadowAnim = CABasicAnimation(keyPath:"shadowRadius") | |
shadowAnim.toValue = self.tempShadowRadius | |
let opacityAnim = CABasicAnimation(keyPath:"shadowOpacity") | |
opacityAnim.toValue = self.tempShadowOpacity | |
let groupAnim = CAAnimationGroup() | |
groupAnim.duration = 0.7 | |
groupAnim.fillMode = CAMediaTimingFillMode.forwards | |
groupAnim.isRemovedOnCompletion = false | |
groupAnim.animations = [shadowAnim, opacityAnim] | |
self.layer.add(groupAnim, forKey:"shadowBack") | |
}, completion: nil) | |
} | |
override open func layoutSubviews() { | |
super.layoutSubviews() | |
setupRippleView() | |
if let knownTouchCenterLocation = touchCenterLocation { | |
rippleView.center = knownTouchCenterLocation | |
} | |
rippleBackgroundView.layer.frame = bounds | |
rippleBackgroundView.layer.mask = rippleMask | |
} | |
} |
Author
DominatorVbN
commented
Oct 24, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment