Created
December 9, 2014 13:45
-
-
Save morizotter/186ef2dce3d4077a3949 to your computer and use it in GitHub Desktop.
XcodeのLive Renderingを使って簡単に使い勝手の良いアイコンボタンをつくろう ref: http://qiita.com/morizotter/items/2347363f99bd48f925da
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
@IBDesignable | |
class IconButton: UIControl { | |
@IBInspectable var iconImage: UIImage? | |
override func drawRect(rect: CGRect) { | |
if let iconImage = self.iconImage { | |
iconImage.drawInRect(self.bounds) | |
let maskPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: CGRectGetWidth(self.bounds) / 2) | |
let maskLayer = CAShapeLayer() | |
maskLayer.path = maskPath.CGPath | |
self.layer.mask = maskLayer; | |
} | |
} | |
} |
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
@IBDesignable | |
class IconButton: UIControl { | |
@IBInspectable var iconImage: UIImage? | |
@IBInspectable var borderColor: UIColor = UIColor.clearColor() | |
@IBInspectable var lineWidth: CGFloat = 0.0 | |
var borderLayer: CAShapeLayer? | |
override func drawRect(rect: CGRect) { | |
if let iconImage = self.iconImage { | |
iconImage.drawInRect(self.bounds) | |
} | |
let path = UIBezierPath(roundedRect: self.bounds, cornerRadius: CGRectGetWidth(self.bounds) / 2) | |
let maskLayer = CAShapeLayer() | |
maskLayer.path = path.CGPath | |
self.layer.mask = maskLayer; | |
if self.borderLayer == nil { | |
self.borderLayer = CAShapeLayer() | |
self.borderLayer!.path = path.CGPath | |
self.borderLayer!.fillColor = UIColor.clearColor().CGColor | |
} | |
self.borderLayer!.strokeColor = self.borderColor.CGColor | |
self.borderLayer!.lineWidth = self.lineWidth | |
self.borderLayer!.lineCap = kCALineCapRound; | |
self.layer.addSublayer(self.borderLayer!) | |
} | |
} |
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
import UIKit | |
@IBDesignable | |
class IconButton: UIControl { | |
@IBInspectable var normalImage: UIImage? | |
@IBInspectable var highlightedImage: UIImage? | |
@IBInspectable var normalBorderColor: UIColor = UIColor.clearColor() | |
@IBInspectable var highlightedBorderColor: UIColor = UIColor.clearColor() | |
@IBInspectable var lineWidth: CGFloat = 0.0 | |
var borderLayer: CAShapeLayer? | |
override var highlighted: Bool { | |
didSet { | |
self.setNeedsDisplay() | |
} | |
} | |
override func drawRect(rect: CGRect) { | |
var iconImage: UIImage? | |
var borderColor = UIColor.clearColor() | |
if self.highlighted { | |
if let highlightedImage = self.highlightedImage { | |
iconImage = highlightedImage | |
borderColor = self.highlightedBorderColor | |
} | |
} else { | |
if let normalImage = self.normalImage { | |
iconImage = normalImage | |
borderColor = self.normalBorderColor | |
} | |
} | |
iconImage?.drawInRect(self.bounds) | |
let path = UIBezierPath(roundedRect: self.bounds, cornerRadius: CGRectGetWidth(self.bounds) / 2) | |
let maskLayer = CAShapeLayer() | |
maskLayer.path = path.CGPath | |
self.layer.mask = maskLayer; | |
if self.borderLayer == nil { | |
self.borderLayer = CAShapeLayer() | |
self.borderLayer!.path = path.CGPath | |
self.borderLayer!.fillColor = UIColor.clearColor().CGColor | |
} | |
self.borderLayer!.strokeColor = borderColor.CGColor | |
self.borderLayer!.lineWidth = self.lineWidth | |
self.borderLayer!.lineCap = kCALineCapRound; | |
self.layer.addSublayer(self.borderLayer!) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment