-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
255 additions
and
119 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// ReusableObject.swift | ||
// YMCalendar | ||
// | ||
// Created by Yuma Matsune on 2017/03/07. | ||
// Copyright © 2017年 Yuma Matsune. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// ReusableObject will be reused for many times, such as UICollectionViewCell. | ||
/// This object can be registared and dequeued. | ||
public protocol ReusableObject: class { | ||
|
||
init() | ||
|
||
var reuseIdentifier: String { get set } | ||
|
||
func prepareForReuse() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// ReusableObjectQueue.swift | ||
// YMCalendar | ||
// | ||
// Created by Yuma Matsune on 2017/04/28. | ||
// Copyright © 2017年 Yuma Matsune. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
final internal class ReusableObjectQueue { | ||
typealias T = ReusableObject | ||
|
||
var reusableObjects: [String : T] = [:] | ||
|
||
var objectClasses: [String : T.Type] = [:] | ||
|
||
var totalCreated = 0 | ||
|
||
var count: Int { | ||
return reusableObjects.count | ||
} | ||
|
||
func registerClass(_ objectClass: T.Type?, forObjectWithReuseIdentifier identifier: String) { | ||
if let objClass = objectClass { | ||
objectClasses[identifier] = objClass | ||
} else { | ||
objectClasses.removeValue(forKey: identifier) | ||
reusableObjects.removeValue(forKey: identifier) | ||
} | ||
} | ||
|
||
func enqueueReusableObject(_ object: T) { | ||
reusableObjects[object.reuseIdentifier] = object | ||
} | ||
|
||
func dequeueReusableObjectWithIdentifier(_ identifier: String) -> T? { | ||
if let object = reusableObjects[identifier] { | ||
reusableObjects.removeValue(forKey: identifier) | ||
object.prepareForReuse() | ||
return object | ||
} else { | ||
guard let anyClass = objectClasses[identifier] else { | ||
fatalError("\(identifier) is not registered.") | ||
} | ||
let object = anyClass.init() | ||
totalCreated += 1 | ||
object.reuseIdentifier = identifier | ||
return object | ||
} | ||
} | ||
|
||
func removeAll() { | ||
reusableObjects.removeAll() | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// YMEventStandardView.swift | ||
// YMCalendar | ||
// | ||
// Created by Yuma Matsune on 2017/03/09. | ||
// Copyright © 2017年 Yuma Matsune. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
public class YMEventStandardView: YMEventView { | ||
|
||
private let kSpace: CGFloat = 2 | ||
|
||
public var title: String = "" | ||
|
||
public var textColor: UIColor = .white | ||
|
||
public var font: UIFont = .systemFont(ofSize: 12.0) | ||
|
||
public var attrString = NSMutableAttributedString() | ||
|
||
public var baselineOffset: Float = 0.0 | ||
|
||
override public func layoutSubviews() { | ||
super.layoutSubviews() | ||
setNeedsDisplay() | ||
} | ||
|
||
override public func prepareForReuse() { | ||
super.prepareForReuse() | ||
setNeedsDisplay() | ||
} | ||
|
||
private func redrawStringInRect(_ rect: CGRect) { | ||
let style = NSMutableParagraphStyle() | ||
style.lineBreakMode = .byClipping | ||
|
||
let attributedString = NSMutableAttributedString(string: title, | ||
attributes: [ | ||
NSAttributedString.Key.font : font, | ||
NSAttributedString.Key.paragraphStyle : style, | ||
NSAttributedString.Key.foregroundColor : textColor, | ||
NSAttributedString.Key.baselineOffset : baselineOffset]) | ||
|
||
attrString = attributedString | ||
} | ||
|
||
override public func draw(_ rect: CGRect) { | ||
let drawRect = rect.insetBy(dx: kSpace, dy: 0) | ||
redrawStringInRect(drawRect) | ||
|
||
attrString.draw(with: drawRect, options: [.truncatesLastVisibleLine, .usesLineFragmentOrigin], context: nil) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// YMEventView.swift | ||
// YMCalendar | ||
// | ||
// Created by Yuma Matsune on 2017/03/06. | ||
// Copyright © 2017年 Yuma Matsune. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
open class YMEventView: UIView, ReusableObject { | ||
|
||
public var reuseIdentifier: String = "" | ||
|
||
public var selected: Bool = false | ||
|
||
public var visibleHeight: CGFloat = 0 | ||
|
||
override public init(frame: CGRect) { | ||
super.init(frame: frame) | ||
commonInit() | ||
} | ||
|
||
required public init?(coder aDecoder: NSCoder) { | ||
super.init(coder: aDecoder) | ||
commonInit() | ||
} | ||
|
||
private func commonInit() { | ||
clipsToBounds = true | ||
} | ||
|
||
public func prepareForReuse() { | ||
selected = false | ||
} | ||
} |
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
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
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
Oops, something went wrong.