Skip to content

Instantly share code, notes, and snippets.

@shoheiyokoyama
Last active July 31, 2016 06:55
Show Gist options
  • Select an option

  • Save shoheiyokoyama/fe60aa99a75e650baf36c3183810cd02 to your computer and use it in GitHub Desktop.

Select an option

Save shoheiyokoyama/fe60aa99a75e650baf36c3183810cd02 to your computer and use it in GitHub Desktop.
ライブラリのリファクタリング実践 ref: http://qiita.com/shoheiyokoyama/items/3405b85205dad77f1b01
public class SYButton: UIButton {
...
public final class SYButton: UIButton {
...
struct AnimationDefaultColor {
static let border = UIColor(red: 54/255, green: 215/255, blue: 183/255, alpha: 1)
static let background = UIColor(red: 248/255, green: 148/255, blue: 6/255, alpha: 1)
static let text = UIColor(red: 214/255, green: 69/255, blue: 65/255, alpha: 1)
static let ripple = UIColor(red: 171/255, green: 183/255, blue: 183/255, alpha: 1)
}
private var animationBorderColor = AnimationDefaultColor.border
//必要に応じてアクセスコントロールをprivateにする
private struct AnimationConstants {
static let borderWidth: CGFloat = 1
static let defaultDuration: CFTimeInterval = 1.5
static let rippleDiameterRatio: CGFloat = 0.7
...
}
private var animationDuration: CFTimeInterval = AnimationConstants.defaultDuration
let borderColorAnimtion = CABasicAnimation(keyPath: "borderColor")
let backgroundColorAnimtion = CABasicAnimation(keyPath: "backgroundColor")
extension CABasicAnimation {
convenience init(type: AnimationKeyType) {
self.init(keyPath: type.rawValue)
}
}
enum AnimationType: String {
case borderColor
case backgroundColor
..
//使用時
let borderColorAnimtion = CABasicAnimation(type: .borderColor)
let backgroundColorAnimtion = CABasicAnimation(type: .backgroundColor)
private func setTextLayer() {
guard let font = titleLabel?.font, text = currentTitle else {
return
}
var attributes = [String: AnyObject]()
attributes[NSFontAttributeName] = font
let size = text.sizeWithAttributes(attributes)
let x = ( self.frame.width - size.width ) / 2
let y = ( self.frame.height - size.height ) / 2
let frame = CGRect(origin: CGPoint(x: x, y: y), size: CGSize(width: size.width, height: size.height + layer.borderWidth))
textLayer.font = font
textLayer.string = text
textLayer.fontSize = font.pointSize
textLayer.foregroundColor = textColor.CGColor
textLayer.contentsScale = UIScreen.mainScreen().scale
textLayer.frame = frame
textLayer.alignmentMode = kCAAlignmentCenter
}
protocol TextConvertible {
var textLayer: CATextLayer { get set }
func configureTextLayer(text: String?, font: UIFont?, textColor: UIColor)
}
extension TextConvertible where Self: UIView {
func configureTextLayer(text: String?, font: UIFont?, textColor: UIColor) {
guard let text = text, font = font else { return }
var attributes = [String: AnyObject]()
attributes[NSFontAttributeName] = font
let size = text.sizeWithAttributes(attributes)
let x = ( self.frame.width - size.width ) / 2
let y = ( self.frame.height - size.height ) / 2
let frame = CGRect(x: x, y: y, width: size.width, height: size.height + layer.borderWidth)
textLayer.font = font
textLayer.string = text
textLayer.fontSize = font.pointSize
textLayer.foregroundColor = textColor.CGColor
textLayer.contentsScale = UIScreen.mainScreen().scale
textLayer.frame = frame
textLayer.alignmentMode = kCAAlignmentCenter
}
}
//使用時
class SYButton: UIButton, TextConvertible {
...
func resetTextLayer() {
configureTextLayer(currentTitle, font: titleLabel?.font, textColor: textColor)
...
class SYLayer {
...
// CALayerを管理する処理
func setLayer() {
...
func resizeLayer() {
...
// アニメーションの処理
func startAnimation() {
...
func stopAnimation() {
...
// CALayerを管理するクラス Animator protocolに準拠
class SYLayer: Animator {
...
func setLayer() {
...
func resizeLayer() {
...
// アニメーションを管理するprotocol
protocol Animatable {
..
extension Animatable {
func startAnimation() {
..
func stopAnimation() {
...
//使用時
class SYLabel {
..
let layer = SYLayer(self.layer)
layer.startAnimation()
switch animationType {
case .border:
syLayer.animationType = .border
case .borderWithShadow:
syLayer.animationType = .borderWithShadow
case .background:
syLayer.animationType = .background
case .ripple:
syLayer.animationType = .ripple
case .text:
syLayer.animationType = .text
}
syLayer.animationType = {
switch animationType {
case .border:
return .border
case .borderWithShadow:
return .borderWithShadow
case .background:
return .background
case .ripple:
return .ripple
case .text:
return .text
}
}()
// MARK: - Private Methods -
private extension SYButton {
private func setup() {
...
// MARK: - UITableViewDataSource -
extension UIViewController: UITableViewDataSource {
...
// MARK: - UITableViewDelegate -
extension UIViewController: UITableViewDelegate {
let rect = CGRectMake(0, 0, 100, 100)
let point = CGPointMake(0, 0)
let size = CGSizeMake(100, 100)
let height = CGRectGetHeight(frame)
let width = CGRectGetWidth(frame)
let rect = CGRect(x: 0, y: 0, width: 100, height: 100)
let size = CGSize(width: 100, height: 100)
let point = CGPoint(x: 0, y: 0)
let height = frame.height
let width = frame.width
let frame = CGRect(x: 0, y: 0, width: 100, height: 100)
let maxX = frame.maxX
let maxY = frame.maxY
let rect = CGRect.zero
let size = CGSize.zero
let point = CGPoint.zero
private var animationBorderColor = UIColor(red: 54/255, green: 215/255, blue: 183/255, alpha: 1)
private var animationDuration: CFTimeInterval = 1.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment