As a related article notes, CALayer has conflicting types for these methods, so it cannot directly match UIDynamicItem . You will need some kind of adapter layer that converted the CALayer methods to the types and values ββneeded by UIDynamicItem .
Fortunately, UIKit offers just such an adapter layer. It is called UIView . Just put each layer in the view and paste the views.
I have long thought of views as heavy weight, especially from the OS X world. Therefore, I always thought: "If you want it to be fast, you better do it with a layer." But it turns out that the views are actually quite subtle, and in most cases there is not much overhead and level, especially for the numbers you talk about in iOS. See the TearOff Demo for an example that calls UIKItDynamics with many dozens of views without problems.
If you really need layers, then yes, you can still do it. Just create an adapter yourself. You just need to hold CALayer and match UIKitDynamics . He does not need to do anything. Something like this should work:
public class DynamicLayerItem : NSObject, UIDynamicItem { let layer: CALayer init(layer: CALayer) { self.layer = layer} public var center: CGPoint { get { return layer.position } set { layer.position = center } } public var bounds: CGRect { return layer.bounds } public var transform: CGAffineTransform { get { return layer.affineTransform() } set { layer.transform = CATransform3DMakeAffineTransform(newValue) } } }
You just animate this object, and this makes the layer animate equivalently.
source share