selva
selva

Reputation: 809

CAShapeLayer class's method "layer" is not available in Swift

In Objective C, below code works.

CAShapeLayer  *shapeLayer = [CAShapeLayer layer];

I'm trying to convert the same in swift. But, error throws says CAShapeLayer is does not have a member named "layer"

var shapeLayer:CAShapeLayer = CAShapeLayer().layer

Or

CAShapeLayer().layer

Any alternative to get layer?

Thanks

Upvotes: 1

Views: 1233

Answers (2)

Eric Aya
Eric Aya

Reputation: 70098

The equivalent of

CAShapeLayer *shapeLayer = [CAShapeLayer layer];

in Swift is just

var shapeLayer = CAShapeLayer()

Then you can do

shapeLayer.frame = self.bounds
shapeLayer.lineWidth = 3.0
...

Upvotes: 5

LoVo
LoVo

Reputation: 2073

I think CAShapeLayer() already returns an instance of CAShapeLayer so this

  var shapeLayer : CAShapeLayer = CAShapeLayer()
    //customize your layer
    shapeLayer.fillColor = UIColor.redColor().CGColor
    self.layer.insertSublayer(shape, atIndex: 0)

should work.

Upvotes: 8

Related Questions