Commit 2e6af65c by Daniel Dahan

issue-7: fully editable MaterialButtons

parent a2d32af3
...@@ -24,15 +24,16 @@ public class BasicCard : MaterialCard { ...@@ -24,15 +24,16 @@ public class BasicCard : MaterialCard {
// :name: layoutConstraints // :name: layoutConstraints
// //
internal lazy var layoutConstraints: Array<NSLayoutConstraint> = Array<NSLayoutConstraint>() internal lazy var layoutConstraints: Array<NSLayoutConstraint> = Array<NSLayoutConstraint>()
// //
// :name: views // :name: views
// //
internal lazy var views: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>() internal lazy var views: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>()
/** //
:name: buttons // :name: horizontalSeparator
*/ //
public var buttons: Array<MaterialButton>? public lazy var horizontalSeparator: UIView = UIView()
/** /**
:name: titleLabel :name: titleLabel
...@@ -60,8 +61,18 @@ public class BasicCard : MaterialCard { ...@@ -60,8 +61,18 @@ public class BasicCard : MaterialCard {
} }
} }
public var horizontalSeparator: UIView? /**
:name: buttons
*/
public var buttons: Array<MaterialButton>? {
didSet {
prepareCard()
}
}
//
// :name: prepareCard
//
internal override func prepareCard() { internal override func prepareCard() {
super.prepareCard() super.prepareCard()
prepareShadow() prepareShadow()
...@@ -87,6 +98,21 @@ public class BasicCard : MaterialCard { ...@@ -87,6 +98,21 @@ public class BasicCard : MaterialCard {
views["detailTextLabel"] = detailTextLabel! views["detailTextLabel"] = detailTextLabel!
} }
if nil != buttons {
var horizontalFormat: String = "H:|"
var buttonViews: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>()
for var i: Int = 0, l: Int = buttons!.count; i < l; ++i {
let button: MaterialButton = buttons![i]
addSubview(button)
buttonViews["button\(i)"] = button
views["button\(i)"] = button as AnyObject
horizontalFormat += "-(20)-[button\(i)]"
verticalFormat += "-(20)-[button\(i)]"
}
horizontalFormat += "-(20)-|"
layoutConstraints += Layout.constraint(horizontalFormat, options: nil, metrics: nil, views: buttonViews)
}
// addConstraints(Layout.constraint("H:|-(20)-[detailTextLabel]-(20)-|", options: nil, metrics: nil, views: ["detailTextLabel": detailTextLabel])) // addConstraints(Layout.constraint("H:|-(20)-[detailTextLabel]-(20)-|", options: nil, metrics: nil, views: ["detailTextLabel": detailTextLabel]))
// addConstraints(Layout.constraint("H:|[horizontalSeparator]|", options: nil, metrics: nil, views: ["horizontalSeparator": horizontalSeparator])) // addConstraints(Layout.constraint("H:|[horizontalSeparator]|", options: nil, metrics: nil, views: ["horizontalSeparator": horizontalSeparator]))
// addConstraints(Layout.constraint("H:|-(10)-[cancelButton(80)]-(10)-[otherButton(80)]", options: nil, metrics: nil, views: ["cancelButton": cancelButton, "otherButton": otherButton])) // addConstraints(Layout.constraint("H:|-(10)-[cancelButton(80)]-(10)-[otherButton(80)]", options: nil, metrics: nil, views: ["cancelButton": cancelButton, "otherButton": otherButton]))
...@@ -100,38 +126,4 @@ public class BasicCard : MaterialCard { ...@@ -100,38 +126,4 @@ public class BasicCard : MaterialCard {
NSLayoutConstraint.activateConstraints(layoutConstraints) NSLayoutConstraint.activateConstraints(layoutConstraints)
} }
} }
//
// private func prepareDetailTextLabel() {
// detailTextLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
// detailTextLabel.font = Roboto.lightWithSize(16.0)
// detailTextLabel.textColor = UIColor.whiteColor()
// detailTextLabel.text = "I am a very simple card. I am good at containing small bits of information. I am convenient because I require little markup to use effectively."
// detailTextLabel.numberOfLines = 0
// detailTextLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
// addSubview(detailTextLabel)
// }
//
// private func prepareHorizontalSeparator() {
// horizontalSeparator.setTranslatesAutoresizingMaskIntoConstraints(false)
// horizontalSeparator.backgroundColor = UIColor.whiteColor()
// horizontalSeparator.alpha = 0.2
// addSubview(horizontalSeparator)
// }
//
// private func prepareCancelButton() {
// cancelButton.setTranslatesAutoresizingMaskIntoConstraints(false)
// cancelButton.setTitle("Cancel", forState: .Normal)
// cancelButton.setTitleColor(buttonColor, forState: .Normal)
// cancelButton.pulseColor = buttonColor
// addSubview(cancelButton)
// }
//
// private func prepareOtherButton() {
// otherButton.setTranslatesAutoresizingMaskIntoConstraints(false)
// otherButton.setTitle("Confirm", forState: .Normal)
// otherButton.setTitleColor(buttonColor, forState: .Normal)
// otherButton.pulseColor = buttonColor
// addSubview(otherButton)
// }
} }
...@@ -22,10 +22,18 @@ public class FabButton : MaterialButton { ...@@ -22,10 +22,18 @@ public class FabButton : MaterialButton {
// //
// :name: prepareButton // :name: prepareButton
// //
internal override func prepareView() {
super.prepareView()
setTitleColor(MaterialTheme.white.color, forState: .Normal)
backgroundColor = MaterialTheme.red.darken1
}
//
// :name: prepareButton
//
internal override func prepareButton() { internal override func prepareButton() {
super.prepareButton() super.prepareButton()
prepareShadow() prepareShadow()
backgroundColor = MaterialTheme.red.darken1
backgroundColorView.layer.cornerRadius = bounds.width / 2 backgroundColorView.layer.cornerRadius = bounds.width / 2
} }
......
...@@ -20,13 +20,20 @@ import UIKit ...@@ -20,13 +20,20 @@ import UIKit
public class FlatButton : MaterialButton { public class FlatButton : MaterialButton {
// //
// :name: prepareButton // :name: prepareView
// //
internal override func prepareButton() { internal override func prepareView() {
super.prepareButton() super.prepareView()
setTitleColor(MaterialTheme.indigo.darken1, forState: .Normal) setTitleColor(MaterialTheme.indigo.darken1, forState: .Normal)
pulseColor = MaterialTheme.indigo.darken1 pulseColor = MaterialTheme.indigo.darken1
backgroundColor = MaterialTheme.clear.color backgroundColor = MaterialTheme.clear.color
}
//
// :name: prepareButton
//
internal override func prepareButton() {
super.prepareButton()
backgroundColorView.layer.cornerRadius = 3 backgroundColorView.layer.cornerRadius = 3
} }
...@@ -36,8 +43,8 @@ public class FlatButton : MaterialButton { ...@@ -36,8 +43,8 @@ public class FlatButton : MaterialButton {
internal override func pulseBegan(touches: Set<NSObject>, withEvent event: UIEvent) { internal override func pulseBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.pulseBegan(touches, withEvent: event) super.pulseBegan(touches, withEvent: event)
UIView.animateWithDuration(0.3, animations: { UIView.animateWithDuration(0.3, animations: {
self.pulseView!.transform = CGAffineTransformMakeScale(10, 10) self.pulseView!.transform = CGAffineTransformMakeScale(4, 4)
self.transform = CGAffineTransformMakeScale(1.05, 1.1) self.transform = CGAffineTransformMakeScale(1.05, 1.05)
}) })
} }
} }
...@@ -33,15 +33,18 @@ public class MaterialButton : UIButton { ...@@ -33,15 +33,18 @@ public class MaterialButton : UIButton {
:name: backgroundColor :name: backgroundColor
*/ */
public override var backgroundColor: UIColor? { public override var backgroundColor: UIColor? {
didSet { get {
backgroundColorView.backgroundColor = backgroundColor return backgroundColorView.backgroundColor
}
set(value) {
backgroundColorView.backgroundColor = value
} }
} }
/** /**
:name: pulseColor :name: pulseColor
*/ */
public var pulseColor: UIColor = MaterialTheme.white.color public var pulseColor: UIColor? = MaterialTheme.white.color
/** /**
:name: init :name: init
...@@ -66,14 +69,6 @@ public class MaterialButton : UIButton { ...@@ -66,14 +69,6 @@ public class MaterialButton : UIButton {
self.init(frame: CGRectZero) self.init(frame: CGRectZero)
} }
//
// :name: prepareView
//
private func prepareView() {
setTranslatesAutoresizingMaskIntoConstraints(false)
prepareBackgroundColorView()
}
/** /**
:name: touchesBegan :name: touchesBegan
*/ */
...@@ -109,13 +104,19 @@ public class MaterialButton : UIButton { ...@@ -109,13 +104,19 @@ public class MaterialButton : UIButton {
} }
// //
// :name: prepareButton // :name: prepareView
// //
internal func prepareButton() { internal func prepareView() {
setTranslatesAutoresizingMaskIntoConstraints(false)
prepareBackgroundColorView() prepareBackgroundColorView()
} }
// //
// :name: prepareButton
//
internal func prepareButton() {}
//
// :name: prepareShadow // :name: prepareShadow
// //
internal func prepareShadow() { internal func prepareShadow() {
...@@ -132,7 +133,7 @@ public class MaterialButton : UIButton { ...@@ -132,7 +133,7 @@ public class MaterialButton : UIButton {
pulseView = UIView(frame: CGRectMake(0, 0, bounds.height, bounds.height)) pulseView = UIView(frame: CGRectMake(0, 0, bounds.height, bounds.height))
pulseView!.layer.cornerRadius = bounds.height / 2 pulseView!.layer.cornerRadius = bounds.height / 2
pulseView!.center = (touches.first as! UITouch).locationInView(self) pulseView!.center = (touches.first as! UITouch).locationInView(self)
pulseView!.backgroundColor = pulseColor.colorWithAlphaComponent(0.5) pulseView!.backgroundColor = pulseColor?.colorWithAlphaComponent(0.5)
backgroundColorView.addSubview(pulseView!) backgroundColorView.addSubview(pulseView!)
} }
...@@ -157,7 +158,7 @@ public class MaterialButton : UIButton { ...@@ -157,7 +158,7 @@ public class MaterialButton : UIButton {
let context = UIGraphicsGetCurrentContext() let context = UIGraphicsGetCurrentContext()
CGContextSaveGState(context); CGContextSaveGState(context);
CGContextAddEllipseInRect(context, rect) CGContextAddEllipseInRect(context, rect)
CGContextSetFillColorWithColor(context, UIColor.clearColor().CGColor) CGContextSetFillColorWithColor(context, MaterialTheme.clear.color.CGColor)
CGContextFillPath(context) CGContextFillPath(context)
CGContextRestoreGState(context); CGContextRestoreGState(context);
} }
......
...@@ -33,8 +33,11 @@ public class MaterialCard : UIView { ...@@ -33,8 +33,11 @@ public class MaterialCard : UIView {
:name: backgroundColor :name: backgroundColor
*/ */
public override var backgroundColor: UIColor? { public override var backgroundColor: UIColor? {
didSet { get {
backgroundColorView.backgroundColor = backgroundColor return backgroundColorView.backgroundColor
}
set(value) {
backgroundColorView.backgroundColor = value
} }
} }
...@@ -66,14 +69,6 @@ public class MaterialCard : UIView { ...@@ -66,14 +69,6 @@ public class MaterialCard : UIView {
self.init(frame: CGRectZero) self.init(frame: CGRectZero)
} }
//
// :name: prepareView
//
private func prepareView() {
setTranslatesAutoresizingMaskIntoConstraints(false)
prepareCard()
}
/** /**
:name: touchesBegan :name: touchesBegan
*/ */
...@@ -101,13 +96,20 @@ public class MaterialCard : UIView { ...@@ -101,13 +96,20 @@ public class MaterialCard : UIView {
} }
// //
// :name: prepareCard // :name: prepareView
// //
internal func prepareCard() { internal func prepareView() {
setTranslatesAutoresizingMaskIntoConstraints(false)
prepareBackgroundColorView() prepareBackgroundColorView()
prepareCard()
} }
// //
// :name: prepareCard
//
internal func prepareCard() {}
//
// :name: prepareShadow // :name: prepareShadow
// //
internal func prepareShadow() { internal func prepareShadow() {
...@@ -166,6 +168,7 @@ public class MaterialCard : UIView { ...@@ -166,6 +168,7 @@ public class MaterialCard : UIView {
backgroundColorView.setTranslatesAutoresizingMaskIntoConstraints(false) backgroundColorView.setTranslatesAutoresizingMaskIntoConstraints(false)
backgroundColorView.layer.cornerRadius = 2 backgroundColorView.layer.cornerRadius = 2
backgroundColorView.layer.masksToBounds = true backgroundColorView.layer.masksToBounds = true
backgroundColorView.clipsToBounds = true
backgroundColorView.userInteractionEnabled = false backgroundColorView.userInteractionEnabled = false
insertSubview(backgroundColorView, atIndex: 0) insertSubview(backgroundColorView, atIndex: 0)
Layout.expandToParentSize(self, child: backgroundColorView) Layout.expandToParentSize(self, child: backgroundColorView)
......
...@@ -21,7 +21,7 @@ import UIKit ...@@ -21,7 +21,7 @@ import UIKit
public struct MaterialTheme { public struct MaterialTheme {
// clear // clear
public struct clear { public struct clear {
public static let color: UIColor = UIColor.clearColor() public static let color: UIColor = white.color.colorWithAlphaComponent(0)
} }
// white // white
......
...@@ -22,11 +22,18 @@ public class RaisedButton : MaterialButton { ...@@ -22,11 +22,18 @@ public class RaisedButton : MaterialButton {
// //
// :name: prepareButton // :name: prepareButton
// //
internal override func prepareView() {
super.prepareView()
setTitleColor(MaterialTheme.white.color, forState: .Normal)
backgroundColor = MaterialTheme.indigo.darken1
}
//
// :name: prepareButton
//
internal override func prepareButton() { internal override func prepareButton() {
super.prepareButton() super.prepareButton()
prepareShadow() prepareShadow()
setTitleColor(MaterialTheme.white.color, forState: .Normal)
backgroundColor = MaterialTheme.indigo.darken1
backgroundColorView.layer.cornerRadius = 3 backgroundColorView.layer.cornerRadius = 3
} }
...@@ -36,8 +43,8 @@ public class RaisedButton : MaterialButton { ...@@ -36,8 +43,8 @@ public class RaisedButton : MaterialButton {
internal override func pulseBegan(touches: Set<NSObject>, withEvent event: UIEvent) { internal override func pulseBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.pulseBegan(touches, withEvent: event) super.pulseBegan(touches, withEvent: event)
UIView.animateWithDuration(0.3, animations: { UIView.animateWithDuration(0.3, animations: {
self.pulseView!.transform = CGAffineTransformMakeScale(10, 10) self.pulseView!.transform = CGAffineTransformMakeScale(4, 4)
self.transform = CGAffineTransformMakeScale(1.05, 1.1) self.transform = CGAffineTransformMakeScale(1.05, 1.05)
}) })
} }
} }
......
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