Skip to content

Commit

Permalink
Issue #54: Fix buttonImage change bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kciter committed Aug 2, 2016
1 parent f3f02fd commit eeb194d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
44 changes: 26 additions & 18 deletions KCFloatingActionButton/KCFloatingActionButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ public class KCFloatingActionButton: UIView {
/**
Button image.
*/
@IBInspectable public var buttonImage: UIImage? = nil
@IBInspectable public var buttonImage: UIImage? = nil {
didSet {
self.setNeedsDisplay()
}
}

/**
Plus icon color inside button.
Expand Down Expand Up @@ -254,12 +258,8 @@ public class KCFloatingActionButton: UIView {
usingSpringWithDamping: 0.55,
initialSpringVelocity: 0.3,
options: [.CurveEaseInOut], animations: { () -> Void in
if self.buttonImage == nil {
self.plusLayer.transform = CATransform3DMakeRotation(self.degreesToRadians(self.rotationDegrees), 0.0, 0.0, 1.0)
}
else {
self.buttonImageView.transform = CGAffineTransformMakeRotation(self.degreesToRadians(self.rotationDegrees))
}
self.plusLayer.transform = CATransform3DMakeRotation(self.degreesToRadians(self.rotationDegrees), 0.0, 0.0, 1.0)
self.buttonImageView.transform = CGAffineTransformMakeRotation(self.degreesToRadians(self.rotationDegrees))
self.overlayView.alpha = 1
}, completion: nil)

Expand Down Expand Up @@ -292,12 +292,8 @@ public class KCFloatingActionButton: UIView {
usingSpringWithDamping: 0.6,
initialSpringVelocity: 0.8,
options: [], animations: { () -> Void in
if self.buttonImage == nil {
self.plusLayer.transform = CATransform3DMakeRotation(self.degreesToRadians(0), 0.0, 0.0, 1.0)
}
else {
self.buttonImageView.transform = CGAffineTransformMakeRotation(self.degreesToRadians(0))
}
self.plusLayer.transform = CATransform3DMakeRotation(self.degreesToRadians(0), 0.0, 0.0, 1.0)
self.buttonImageView.transform = CGAffineTransformMakeRotation(self.degreesToRadians(0))
self.overlayView.alpha = 0
}, completion: {(f) -> Void in
self.overlayView.removeFromSuperview()
Expand Down Expand Up @@ -363,7 +359,7 @@ public class KCFloatingActionButton: UIView {
/**
Add item with title and icon.
*/
public func addItem(title: String, icon: UIImage) -> KCFloatingActionButtonItem {
public func addItem(title: String, icon: UIImage?) -> KCFloatingActionButtonItem {
let item = KCFloatingActionButtonItem()
itemDefaultSet(item)
item.title = title
Expand All @@ -372,10 +368,22 @@ public class KCFloatingActionButton: UIView {
return item
}

/**
Add item with title and handler.
*/
public func addItem(title title: String, handler: ((KCFloatingActionButtonItem) -> Void)) -> KCFloatingActionButtonItem {
let item = KCFloatingActionButtonItem()
itemDefaultSet(item)
item.title = title
item.handler = handler
addItem(item: item)
return item
}

/**
Add item with title, icon or handler.
*/
public func addItem(title: String, icon: UIImage, handler: ((KCFloatingActionButtonItem) -> Void)) -> KCFloatingActionButtonItem {
public func addItem(title: String, icon: UIImage?, handler: ((KCFloatingActionButtonItem) -> Void)) -> KCFloatingActionButtonItem {
let item = KCFloatingActionButtonItem()
itemDefaultSet(item)
item.title = title
Expand All @@ -388,7 +396,7 @@ public class KCFloatingActionButton: UIView {
/**
Add item with icon.
*/
public func addItem(icon icon: UIImage) -> KCFloatingActionButtonItem {
public func addItem(icon icon: UIImage?) -> KCFloatingActionButtonItem {
let item = KCFloatingActionButtonItem()
itemDefaultSet(item)
item.icon = icon
Expand All @@ -399,7 +407,7 @@ public class KCFloatingActionButton: UIView {
/**
Add item with icon and handler.
*/
public func addItem(icon: UIImage, handler: ((KCFloatingActionButtonItem) -> Void)) -> KCFloatingActionButtonItem {
public func addItem(icon icon: UIImage?, handler: ((KCFloatingActionButtonItem) -> Void)) -> KCFloatingActionButtonItem {
let item = KCFloatingActionButtonItem()
itemDefaultSet(item)
item.icon = icon
Expand Down Expand Up @@ -468,7 +476,7 @@ public class KCFloatingActionButton: UIView {

private func setPlusLayer() {
plusLayer.removeFromSuperlayer()
plusLayer.frame = CGRectMake(circleLayer.frame.origin.x , circleLayer.frame.origin.y, size, size)
plusLayer.frame = CGRectMake(0, 0, size, size)
plusLayer.lineCap = kCALineCapRound
plusLayer.strokeColor = plusColor.CGColor
plusLayer.lineWidth = 2.0
Expand Down
25 changes: 13 additions & 12 deletions Sample/KCFloatingActionButton/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,42 @@ class ViewController: UIViewController, KCFloatingActionButtonDelegate {
override func viewDidLoad() {
super.viewDidLoad()

layoutFAB(shouldUseCustomImage: false)
layoutFAB()
}

@IBAction func endEditing() {
view.endEditing(true)
}

@IBAction func customImageSwitched(sender: UISwitch) {
fab.removeFromSuperview()
layoutFAB(shouldUseCustomImage: sender.on)
if sender.on == true {
fab.buttonImage = UIImage(named: "custom-add")
} else {
fab.buttonImage = nil
}
}

func layoutFAB(shouldUseCustomImage shouldUseCustomImage: Bool) {
func layoutFAB() {
let item = KCFloatingActionButtonItem()
item.buttonColor = UIColor.blueColor()
item.circleShadowColor = UIColor.redColor()
item.titleShadowColor = UIColor.blueColor()
item.title = "Custom item"
item.handler = { item in

}

fab = KCFloatingActionButton()
fab.addItem(title: "I got a title")
fab.addItem("I got a icon", icon: UIImage(named: "icShare")!)
fab.addItem("I got a handler", icon: UIImage(named: "icMap")!, handler: { item in
fab.addItem("I got a icon", icon: UIImage(named: "icShare"))
fab.addItem("I got a handler", icon: UIImage(named: "icMap")) { item in
let alert = UIAlertController(title: "Hey", message: "I'm hungry...", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Me too", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
self.fab.close()
})
}
fab.addItem(item: item)
fab.fabDelegate = self

if (shouldUseCustomImage) {
fab.buttonImage = UIImage(named: "custom-add")
}

self.view.addSubview(fab)

}
Expand Down

0 comments on commit eeb194d

Please sign in to comment.