Last active
July 31, 2016 06:55
-
-
Save shoheiyokoyama/fe60aa99a75e650baf36c3183810cd02 to your computer and use it in GitHub Desktop.
ライブラリのリファクタリング実践 ref: http://qiita.com/shoheiyokoyama/items/3405b85205dad77f1b01
This file contains hidden or 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
| public class SYButton: UIButton { | |
| ... |
This file contains hidden or 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
| public final class SYButton: UIButton { | |
| ... |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| let borderColorAnimtion = CABasicAnimation(keyPath: "borderColor") | |
| let backgroundColorAnimtion = CABasicAnimation(keyPath: "backgroundColor") |
This file contains hidden or 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
| 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) | |
This file contains hidden or 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
| 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 | |
| } |
This file contains hidden or 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
| 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) | |
| ... |
This file contains hidden or 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
| class SYLayer { | |
| ... | |
| // CALayerを管理する処理 | |
| func setLayer() { | |
| ... | |
| func resizeLayer() { | |
| ... | |
| // アニメーションの処理 | |
| func startAnimation() { | |
| ... | |
| func stopAnimation() { | |
| ... |
This file contains hidden or 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
| // 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() |
This file contains hidden or 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
| 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 | |
| } | |
This file contains hidden or 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
| syLayer.animationType = { | |
| switch animationType { | |
| case .border: | |
| return .border | |
| case .borderWithShadow: | |
| return .borderWithShadow | |
| case .background: | |
| return .background | |
| case .ripple: | |
| return .ripple | |
| case .text: | |
| return .text | |
| } | |
| }() |
This file contains hidden or 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
| // MARK: - Private Methods - | |
| private extension SYButton { | |
| private func setup() { | |
| ... | |
This file contains hidden or 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
| // MARK: - UITableViewDataSource - | |
| extension UIViewController: UITableViewDataSource { | |
| ... | |
| // MARK: - UITableViewDelegate - | |
| extension UIViewController: UITableViewDelegate { | |
This file contains hidden or 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
| 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) |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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