Commit ae8f95f3 by Daniel Dahan

progressive cleanup

parent b04f0258
...@@ -19,38 +19,49 @@ ...@@ -19,38 +19,49 @@
import UIKit import UIKit
public class FabButton : MaterialButton { public class FabButton : MaterialButton {
/**
var lineWidth: CGFloat = 2.0 :name: lineWidth
*/
public var lineWidth: CGFloat = 2.0
/**
:name: init
*/
public required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}
public convenience init() { /**
self.init(frame: CGRectZero) :name: init
*/
public required init(frame: CGRect) {
super.init(frame: frame)
initialize()
} }
/**
:name: drawRect
*/
public override func drawRect(rect: CGRect) { public override func drawRect(rect: CGRect) {
setupContext(rect) prepareContext(rect)
setupBackgroundColorView() prepareBackgroundColorView()
setupPlus() preparePlus()
}
public required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
applyShadow()
}
public required override init(frame: CGRect) {
super.init(frame: frame)
initialize()
applyShadow()
} }
func initialize() { //
color = UIColor.redColor() // :name: initialize
pulseColor = UIColor.whiteColor() //
internal func initialize() {
color = .redColor()
pulseColor = .whiteColor()
setTranslatesAutoresizingMaskIntoConstraints(false) setTranslatesAutoresizingMaskIntoConstraints(false)
} }
func setupContext(rect: CGRect) { //
// :name: prepareContext
//
private func prepareContext(rect: CGRect) {
let context = UIGraphicsGetCurrentContext() let context = UIGraphicsGetCurrentContext()
CGContextSaveGState(context); CGContextSaveGState(context);
CGContextAddEllipseInRect(context, rect) CGContextAddEllipseInRect(context, rect)
...@@ -58,10 +69,13 @@ public class FabButton : MaterialButton { ...@@ -58,10 +69,13 @@ public class FabButton : MaterialButton {
CGContextFillPath(context) CGContextFillPath(context)
CGContextRestoreGState(context); CGContextRestoreGState(context);
} }
//
// :name: prepareBackgroundColorView
//
// We need this view so we can use the masksToBounds // We need this view so we can use the masksToBounds
// so the pulse doesn't animate off the button // so the pulse doesn't animate off the button
func setupBackgroundColorView() { func prepareBackgroundColorView() {
backgroundColorView = UIView() backgroundColorView = UIView()
backgroundColorView!.frame = bounds backgroundColorView!.frame = bounds
backgroundColorView!.layer.cornerRadius = bounds.width / 2.0 backgroundColorView!.layer.cornerRadius = bounds.width / 2.0
...@@ -70,90 +84,29 @@ public class FabButton : MaterialButton { ...@@ -70,90 +84,29 @@ public class FabButton : MaterialButton {
backgroundColorView!.userInteractionEnabled = false backgroundColorView!.userInteractionEnabled = false
insertSubview(backgroundColorView!, atIndex: 0) insertSubview(backgroundColorView!, atIndex: 0)
} }
//
// :name: preparePlus
//
// I make the + with two views because // I make the + with two views because
// The label is not actually vertically and horizontally aligned // The label is not actually vertically and horizontally aligned
// Quick hack instead of subclassing UILabel and override drawTextInRect // Quick hack instead of subclassing UILabel and override drawTextInRect
func setupPlus() { private func preparePlus() {
setupVerticalLine() prepareVerticalLine()
setupHorizontalLine() prepareHorizontalLine()
} }
func setupVerticalLine() { private func prepareVerticalLine() {
verticalLine = UIView(frame: CGRectMake(0, 0, lineWidth, CGRectGetHeight(backgroundColorView!.frame) / 3.0)) verticalLine = UIView(frame: CGRectMake(0, 0, lineWidth, CGRectGetHeight(backgroundColorView!.frame) / 3.0))
verticalLine!.backgroundColor = UIColor.whiteColor() verticalLine!.backgroundColor = UIColor.whiteColor()
verticalLine!.center = backgroundColorView!.center verticalLine!.center = backgroundColorView!.center
backgroundColorView!.addSubview(verticalLine!) backgroundColorView!.addSubview(verticalLine!)
} }
func setupHorizontalLine() { private func prepareHorizontalLine() {
horizontalLine = UIView(frame: CGRectMake(0, 0, CGRectGetWidth(backgroundColorView!.frame) / 3.0, lineWidth)) horizontalLine = UIView(frame: CGRectMake(0, 0, CGRectGetWidth(backgroundColorView!.frame) / 3.0, lineWidth))
horizontalLine!.backgroundColor = UIColor.whiteColor() horizontalLine!.backgroundColor = UIColor.whiteColor()
horizontalLine!.center = backgroundColorView!.center horizontalLine!.center = backgroundColorView!.center
backgroundColorView!.addSubview(horizontalLine!) backgroundColorView!.addSubview(horizontalLine!)
} }
func applyShadow() {
layer.shadowOffset = CGSizeMake(1, 1)
layer.shadowColor = UIColor.blackColor().CGColor
layer.shadowOpacity = 0.5
layer.shadowRadius = 5
}
public override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
pulseTouches(touches)
}
public override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
shrink()
removePulse()
}
public override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
super.touchesCancelled(touches, withEvent: event)
shrink()
removePulse()
}
func pulseTouches(touches: NSSet) {
let touch = touches.allObjects.last as! UITouch
let touchLocation = touch.locationInView(self)
pulseView = UIView()
pulseView!.frame = CGRectMake(0, 0, bounds.width, bounds.height)
pulseView!.layer.cornerRadius = bounds.width / 2.0
pulseView!.center = touchLocation
pulseView!.backgroundColor = pulseColor!.colorWithAlphaComponent(0.5)
backgroundColorView!.addSubview(pulseView!)
UIView.animateWithDuration(0.3,
animations: {
self.pulseView!.transform = CGAffineTransformMakeScale(3, 3)
self.transform = CGAffineTransformMakeScale(1.1, 1.1)
},
completion: nil
)
}
func shrink() {
UIView.animateWithDuration(0.3,
delay: 0.0,
usingSpringWithDamping: 0.2,
initialSpringVelocity: 10,
options: nil,
animations: {
self.transform = CGAffineTransformIdentity
},
completion: nil
)
}
func removePulse() {
UIView.animateWithDuration(0.3, animations: { _ in
self.pulseView?.alpha = 0.0
}) { (finished) -> Void in
self.pulseView?.removeFromSuperview()
self.pulseView = nil
}
}
} }
...@@ -26,4 +26,111 @@ public class MaterialButton : UIButton { ...@@ -26,4 +26,111 @@ public class MaterialButton : UIButton {
internal var verticalLine: UIView? internal var verticalLine: UIView?
internal var horizontalLine: UIView? internal var horizontalLine: UIView?
internal var backgroundColorView: UIView? internal var backgroundColorView: UIView?
/**
:name: init
*/
public required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
prepareShadow()
}
/**
:name: init
*/
public required override init(frame: CGRect) {
super.init(frame: frame)
prepareShadow()
}
/**
:name: init
*/
public convenience init() {
self.init(frame: CGRectZero)
}
func prepareShadow() {
layer.shadowOffset = CGSizeMake(1, 1)
layer.shadowColor = UIColor.blackColor().CGColor
layer.shadowOpacity = 0.5
layer.shadowRadius = 5
}
/**
:name: touchesBegan
*/
public override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
pulseTouches(touches)
}
/**
:name: touchesEnded
*/
public override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
shrink()
removePulse()
}
/**
:name: touchesCancelled
*/
public override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
super.touchesCancelled(touches, withEvent: event)
shrink()
removePulse()
}
//
// :name: pulseTouches
//
internal func pulseTouches(touches: NSSet) {
let touch = touches.allObjects.last as! UITouch
let touchLocation = touch.locationInView(self)
pulseView = UIView()
pulseView!.frame = CGRectMake(0, 0, bounds.width, bounds.height)
pulseView!.layer.cornerRadius = bounds.width / 2.0
pulseView!.center = touchLocation
pulseView!.backgroundColor = pulseColor!.colorWithAlphaComponent(0.5)
backgroundColorView!.addSubview(pulseView!)
UIView.animateWithDuration(0.3,
animations: {
self.pulseView!.transform = CGAffineTransformMakeScale(3, 3)
self.transform = CGAffineTransformMakeScale(1.1, 1.1)
},
completion: nil
)
}
//
// :name: shrink
//
internal func shrink() {
UIView.animateWithDuration(0.3,
delay: 0.0,
usingSpringWithDamping: 0.2,
initialSpringVelocity: 10,
options: nil,
animations: {
self.transform = CGAffineTransformIdentity
},
completion: nil
)
}
//
// :name: removePulse
//
internal func removePulse() {
UIView.animateWithDuration(0.3,
animations: { _ in
self.pulseView?.alpha = 0.0
}
) { _ in
self.pulseView?.removeFromSuperview()
self.pulseView = nil
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment