how to redraw only one specific layer ? (IOS,SWIFT

2019-08-09 15:35发布

问题:

I have my UIView. I draw something in my UIView. I added a CALayer to my UIView. Now I would like my controller to only redraw my added CALayer.

How do I do that ?

In my actual Code it will redraw the main view. How can I redraw my subview (myshapelayer) ?

     override func drawRect(rect: CGRect) {

            var myshapelayer:CAShapeLayer=CAShapeLayer();
            var testbezier:UIBezierPath=UIBezierPath();
            testbezier.moveToPoint(CGPoint(x: 60, y: 80));
            ...
    ...

            myshapelayer.strokeColor=UIColor.blueColor().CGColor;
            myshapelayer.path=testbezier.CGPath;
            newlayer.addSublayer(myshapelayer)

<draw on my main view>

Controller:

myview.setNeedsDisplay();

回答1:

There are two ways.

First, you make delegate protocol and pass your CALayer to change in ViewController

Second, you can get CALayer by using self.view.subviews if CALayer's index is 1, you can get CALayer object by self.view.subviews[1] as? CALayer

Hope that it may help you.



回答2:

you can call

self.view.displayLayer(myshapelayer)

from ViewController