First of all: This is a series of articles, refer to other articles under this topic to help you understand this article.
The last piece introduced the Cashpelayer, and made two examples, if the creativity is stronger, those things can create the animation effect that you previously dared not imagine.
Today we're going to talk about careplication.
Careplication is a duplicate layer, you can copy the layers as required, first look at the Calayer based on the addition of those APIs
API |
Description |
InstanceCount |
Number of repeating instance layer (hereinafter referred to as instance) |
Instancedelay |
The time that the instance lasted |
Instancetransform |
Instance animations |
Instancecolor |
Instance Color |
Instanceredoffset |
Red Offset |
Instancegreenoffset |
Green Offset |
Instanceblueoffset |
Blue Offset |
Instancealphaoffset |
Transparency Offset |
·
Today I take a chrysanthemum as an example, the effect is such a demo download
This can be used as the tableview of the drop-down refresh effect, of course, I am lazy, is a slider instead of the TableView drop offset.
His implementation is simple, so let's create a careplicatorlayer.
var replicatorLayer = CAReplicatorLayer()replicatorLayer.frame = someView.boundssomeView.layer.addSublayer(replicatorLayer)
- Explain
Same someview for the view created in storyboard
Given size, and add it to the layer on Someview
In fact, this chrysanthemum is composed of a lot of duplicate rectangular layer. Then you need a rectangle layer.
10.0let midX = CGRectGetMidX(someView.bounds2instanceLayer.frame = CGRect(xy0.02.5)instanceLayer.backgroundColor = UIColor.whiteColor().CGColorreplicatorLayer.addSublayer(instanceLayer)
- Explain
The small rectangular layer created here is called Instancelayer
Add it to Replicatorlayer
This is just to put a replicatorlayer on the view, and then add a rectangle layer, then you can start to set the properties of the Replicatorlayer
Set the color first
replicatorLayer.instanceColor = UIColor.grayColor().CGColor// replicatorLayer.instanceRedOffset0.0// replicatorLayer.instanceGreenOffset0.0// replicatorLayer.instanceBlueOffset0.0// replicatorLayer.instanceAlphaOffset0.0
- Explain
Color offset try to tune yourself to see what's changed.
Create a slider on storyboard, a way to generate a powerful person by dragging lines
@IBAction func Slider (sender:uislider) {Let count:int = Int (sender. Value* -) Replicatorlayer. InstanceCount= Count If sender. Value==1{Replicatorlayer. Instancedelay= Cftimeinterval (1.0/Double (count)) addanimation ()}else{if faderanimation! = Nil {Instancelaye R. Opacity=1.0Instancelayer. Removeanimationforkey("Fader")}} Replicatorlayer. Instancetransform= Catransform3dmakerotation (CGFloat (Float (M_PI *2.0) * Sender. Value/Float (count)),0.0,0.0,1.0) }
- Explain
Setting Instanccount varies according to the value of the slider, int type
Lasts for 1 seconds, the Cftimeinterval type, which represents the time required to generate count layer
When the slider value is not 1.0, the animation is removed, and the opacity is set to 1
Instancetransform, animating, the angle at which the 3D animation rotates around the z-axis
Add animations
func addAnimation() { "opacity") faderAnimation.fromValue1.0 faderAnimation.toValue0.0 faderAnimation.duration1 faderAnimation.repeatCount = Float(Int.max) instanceLayer.opacity0.0 instanceLayer.addAnimation"fader") }
Animation I do not explain, want to understand the animation please see the article under this topic
IOS Animation Animation-4-4:calayer Subclass: Careplicatorlayer