Commit 2e6af65c by Daniel Dahan

issue-7: fully editable MaterialButtons

parent a2d32af3
......@@ -24,15 +24,16 @@ public class BasicCard : MaterialCard {
// :name: layoutConstraints
//
internal lazy var layoutConstraints: Array<NSLayoutConstraint> = Array<NSLayoutConstraint>()
//
// :name: views
//
internal lazy var views: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>()
/**
:name: buttons
*/
public var buttons: Array<MaterialButton>?
//
// :name: horizontalSeparator
//
public lazy var horizontalSeparator: UIView = UIView()
/**
:name: titleLabel
......@@ -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() {
super.prepareCard()
prepareShadow()
......@@ -87,6 +98,21 @@ public class BasicCard : MaterialCard {
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:|[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]))
......@@ -100,38 +126,4 @@ public class BasicCard : MaterialCard {
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,11 +22,19 @@ public class FabButton : MaterialButton {
//
// :name: prepareButton
//
internal override func prepareView() {
super.prepareView()
setTitleColor(MaterialTheme.white.color, forState: .Normal)
backgroundColor = MaterialTheme.red.darken1
}
//
// :name: prepareButton
//
internal override func prepareButton() {
super.prepareButton()
prepareShadow()
backgroundColor = MaterialTheme.red.darken1
backgroundColorView.layer.cornerRadius = bounds.width / 2
backgroundColorView.layer.cornerRadius = bounds.width / 2
}
//
......
......@@ -20,13 +20,20 @@ import UIKit
public class FlatButton : MaterialButton {
//
// :name: prepareButton
// :name: prepareView
//
internal override func prepareButton() {
super.prepareButton()
internal override func prepareView() {
super.prepareView()
setTitleColor(MaterialTheme.indigo.darken1, forState: .Normal)
pulseColor = MaterialTheme.indigo.darken1
backgroundColor = MaterialTheme.clear.color
}
//
// :name: prepareButton
//
internal override func prepareButton() {
super.prepareButton()
backgroundColorView.layer.cornerRadius = 3
}
......@@ -36,8 +43,8 @@ public class FlatButton : MaterialButton {
internal override func pulseBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.pulseBegan(touches, withEvent: event)
UIView.animateWithDuration(0.3, animations: {
self.pulseView!.transform = CGAffineTransformMakeScale(10, 10)
self.transform = CGAffineTransformMakeScale(1.05, 1.1)
self.pulseView!.transform = CGAffineTransformMakeScale(4, 4)
self.transform = CGAffineTransformMakeScale(1.05, 1.05)
})
}
}
......@@ -33,15 +33,18 @@ public class MaterialButton : UIButton {
:name: backgroundColor
*/
public override var backgroundColor: UIColor? {
didSet {
backgroundColorView.backgroundColor = backgroundColor
get {
return backgroundColorView.backgroundColor
}
set(value) {
backgroundColorView.backgroundColor = value
}
}
/**
:name: pulseColor
*/
public var pulseColor: UIColor = MaterialTheme.white.color
public var pulseColor: UIColor? = MaterialTheme.white.color
/**
:name: init
......@@ -66,14 +69,6 @@ public class MaterialButton : UIButton {
self.init(frame: CGRectZero)
}
//
// :name: prepareView
//
private func prepareView() {
setTranslatesAutoresizingMaskIntoConstraints(false)
prepareBackgroundColorView()
}
/**
:name: touchesBegan
*/
......@@ -109,13 +104,19 @@ public class MaterialButton : UIButton {
}
//
// :name: prepareButton
// :name: prepareView
//
internal func prepareButton() {
internal func prepareView() {
setTranslatesAutoresizingMaskIntoConstraints(false)
prepareBackgroundColorView()
}
//
// :name: prepareButton
//
internal func prepareButton() {}
//
// :name: prepareShadow
//
internal func prepareShadow() {
......@@ -132,7 +133,7 @@ public class MaterialButton : UIButton {
pulseView = UIView(frame: CGRectMake(0, 0, bounds.height, bounds.height))
pulseView!.layer.cornerRadius = bounds.height / 2
pulseView!.center = (touches.first as! UITouch).locationInView(self)
pulseView!.backgroundColor = pulseColor.colorWithAlphaComponent(0.5)
pulseView!.backgroundColor = pulseColor?.colorWithAlphaComponent(0.5)
backgroundColorView.addSubview(pulseView!)
}
......@@ -157,7 +158,7 @@ public class MaterialButton : UIButton {
let context = UIGraphicsGetCurrentContext()
CGContextSaveGState(context);
CGContextAddEllipseInRect(context, rect)
CGContextSetFillColorWithColor(context, UIColor.clearColor().CGColor)
CGContextSetFillColorWithColor(context, MaterialTheme.clear.color.CGColor)
CGContextFillPath(context)
CGContextRestoreGState(context);
}
......
......@@ -33,8 +33,11 @@ public class MaterialCard : UIView {
:name: backgroundColor
*/
public override var backgroundColor: UIColor? {
didSet {
backgroundColorView.backgroundColor = backgroundColor
get {
return backgroundColorView.backgroundColor
}
set(value) {
backgroundColorView.backgroundColor = value
}
}
......@@ -66,14 +69,6 @@ public class MaterialCard : UIView {
self.init(frame: CGRectZero)
}
//
// :name: prepareView
//
private func prepareView() {
setTranslatesAutoresizingMaskIntoConstraints(false)
prepareCard()
}
/**
:name: touchesBegan
*/
......@@ -101,13 +96,20 @@ public class MaterialCard : UIView {
}
//
// :name: prepareCard
// :name: prepareView
//
internal func prepareCard() {
internal func prepareView() {
setTranslatesAutoresizingMaskIntoConstraints(false)
prepareBackgroundColorView()
prepareCard()
}
//
// :name: prepareCard
//
internal func prepareCard() {}
//
// :name: prepareShadow
//
internal func prepareShadow() {
......@@ -166,6 +168,7 @@ public class MaterialCard : UIView {
backgroundColorView.setTranslatesAutoresizingMaskIntoConstraints(false)
backgroundColorView.layer.cornerRadius = 2
backgroundColorView.layer.masksToBounds = true
backgroundColorView.clipsToBounds = true
backgroundColorView.userInteractionEnabled = false
insertSubview(backgroundColorView, atIndex: 0)
Layout.expandToParentSize(self, child: backgroundColorView)
......
......@@ -21,7 +21,7 @@ import UIKit
public struct MaterialTheme {
// clear
public struct clear {
public static let color: UIColor = UIColor.clearColor()
public static let color: UIColor = white.color.colorWithAlphaComponent(0)
}
// white
......
......@@ -22,11 +22,18 @@ public class RaisedButton : MaterialButton {
//
// :name: prepareButton
//
internal override func prepareButton() {
super.prepareButton()
prepareShadow()
internal override func prepareView() {
super.prepareView()
setTitleColor(MaterialTheme.white.color, forState: .Normal)
backgroundColor = MaterialTheme.indigo.darken1
}
//
// :name: prepareButton
//
internal override func prepareButton() {
super.prepareButton()
prepareShadow()
backgroundColorView.layer.cornerRadius = 3
}
......@@ -36,8 +43,8 @@ public class RaisedButton : MaterialButton {
internal override func pulseBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.pulseBegan(touches, withEvent: event)
UIView.animateWithDuration(0.3, animations: {
self.pulseView!.transform = CGAffineTransformMakeScale(10, 10)
self.transform = CGAffineTransformMakeScale(1.05, 1.1)
self.pulseView!.transform = CGAffineTransformMakeScale(4, 4)
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