Created
August 10, 2019 16:15
-
-
Save SocialKitLtd/41e02c4e6939fc3482c2bddce68e49e8 to your computer and use it in GitHub Desktop.
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 | |
protocol CanvasItemProtocol | |
{ | |
var scale : CGFloat { get set } | |
} | |
class CanvasItem : CanvasItemProtocol | |
{ | |
public var scale : CGFloat = 1.0 | |
} | |
class StickerItem: CanvasItem | |
{ | |
public var stickerName : String = "" | |
} | |
class ShapeItem: CanvasItem | |
{ | |
public var color: UIColor = .red | |
} | |
class ViewItem<T: CanvasItemProtocol> : UIView | |
{ | |
let canvasItem: T | |
init(_ item: T) | |
{ | |
canvasItem = item | |
super.init(frame: .zero) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
class CanvasShapeView: ViewItem<ShapeItem> { } | |
class CanvasStickerView: ViewItem<StickerItem> {} | |
let shapeItem = ShapeItem() | |
let stickerItem = StickerItem() | |
let shapeView = ViewItem<ShapeItem>(shapeItem) | |
let stickerView = ViewItem<StickerItem>(stickerItem) | |
let superview = UIView() | |
superview.addSubview(shapeView) | |
superview.addSubview(stickerView) | |
for canvasItemView in superview.subviews.compactMap({$0 as? ViewItem<CanvasItemProtocol>}) { | |
print(canvasItemView.scale) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment