如何在swift中按索引选择CALayer?

如何在swift中按索引选择CALayer?,swift,calayer,Swift,Calayer,我在swift上添加了一些CALayers。如果我做错了什么,请告诉我怎样才能做得更好 for index in 1...5 { nowCard = CALayer() nowCard.backgroundColor = colorMain.CGColor nowCard.frame = CGRectMake(fromX, fromY, cardsWidth, cardsHeight) self.layer.insertSublayer(nowCard, atIn

我在swift上添加了一些
CALayers
。如果我做错了什么,请告诉我怎样才能做得更好

for index in 1...5 {
    nowCard = CALayer()
    nowCard.backgroundColor = colorMain.CGColor
    nowCard.frame = CGRectMake(fromX, fromY, cardsWidth, cardsHeight)
    self.layer.insertSublayer(nowCard, atIndex: UInt32(index))
}
在下一步中,我必须选择其中一个。在obj-c中,我可以用
objectAtIndex
来实现这一点,但现在不行了。如何按索引选择一个图层?

好的,我做到了

var layerNow: CALayer
layerNow = self.layer.sublayers[atIndex] as CALayer

为您提供了一个小示例,其中包含一些小的修复:

    let nowCard = CALayer()
    let myView = UIView(frame:CGRectMake(0, 0, 320, 200))

    for index in 1...5 {
        nowCard.backgroundColor = UIColor.whiteColor().CGColor
        nowCard.frame = CGRectMake(0, 0, 320, 200)
        myView.layer.insertSublayer(nowCard, atIndex: 1)
    }