Created
August 22, 2018 21:42
-
-
Save kershner/bdd335a765b42616ba9b38f1bc191561 to your computer and use it in GitHub Desktop.
SWLayer.swift
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
class SWLayer: CALayer { | |
@NSManaged var currentX: CGFloat | |
override init() { | |
super.init() | |
} | |
override init(layer: Any) { | |
super.init(layer: layer) | |
if let layer = layer as? SWLayer { | |
currentX = layer.currentX | |
} | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
private class func isCustomAnimKey(key: String) -> Bool { | |
return key == "currentX" | |
} | |
override class func needsDisplay(forKey key: String) -> Bool { | |
if self.isCustomAnimKey(key: key) { | |
return true | |
} | |
return super.needsDisplay(forKey: key) | |
} | |
} |
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
class SWView : UIView { | |
var currentX: CGFloat { | |
set { | |
if let layer = layer as? SWLayer { | |
layer.currentX = newValue | |
} | |
} | |
get { | |
if let layer = layer as? SWLayer { | |
return layer.currentX | |
} | |
return 0.0 | |
} | |
} | |
override class var layerClass : AnyClass { | |
return SWLayer.self | |
} | |
override func display(_ layer: CALayer) { | |
print("DISPLAYING") | |
print(self.currentX) | |
} | |
} |
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
class Test : UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let viewWithCustomLayer = SWView() | |
viewWithCustomLayer.frame = CGRect(x: 0, y: 0, width: 150, height: 150) | |
viewWithCustomLayer.currentX = 0 | |
UIView.animate(withDuration: 2, animations: { | |
viewWithCustomLayer.currentX = 100.0 | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment