CALayer -- 备忘

CALayer

layer是层,每个view上都会最少有一个layer,view上的可视化内容其实都是层。

 

CALayer展示实例

        let customView = UIView(frame: CGRectMake(0, 0, 100, 100))
        customView.center = view.center
        view.addSubview(customView)
        
        let layer = customView.layer
        layer.backgroundColor = UIColor.brownColor().CGColor
        layer.masksToBounds = true
        layer.cornerRadius = 50.0
        layer.borderWidth = 2
        layer.borderColor = UIColor.blackColor().CGColor
        layer.shadowColor = UIColor.blackColor().CGColor
        layer.shadowOpacity = 0.8
        layer.shadowOffset = CGSizeMake(15, 5)
        layer.contents = UIImage(named: "icon.png")?.CGImage

 上面是一些常用的属性,更多属性请自行参考:

  • backgroundColor:背景颜色
  • masksToBounds:将超过layer展示范围的内容剪掉
  • cornerRadius:设置圆角
  • borderWidth:边框宽度
  • borderColor:边框颜色
  • shadowColor:阴影颜色
  • shadowOpacity:阴影的透明度 0.0 ~ 1.0
  • shadowOffset:阴影的位置 (masksToBounds为true时 不显示阴影)
  • contents:设置图层上的内容

 

CALayer的显式动画和隐式动画

        // 先把layer添加到主图层上
        let customLayer = CALayer()
        customLayer.bounds = CGRectMake(0.0, 0.0, 100, 100)
        customLayer.position = CGPointMake(150, 200)
        customLayer.backgroundColor = UIColor.blackColor().CGColor
        view.layer.addSublayer(customLayer)
        
        self.customLayer = customLayer

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        // 开启事务
        CATransaction.begin()
        // 设置动画时间
        CATransaction.setValue(NSNumber(double: 2.0), forKey: kCATransactionAnimationDuration)
        customLayer.position = CGPointMake(50.0, 50.0)
        customLayer.backgroundColor = UIColor.redColor().CGColor
        customLayer.cornerRadius = 50.0
        // 提交事务
        CATransaction.commit()
    }

上面的例子是执行显示动画,这样一些不具有隐式动画的属性被修改后也可以做出动画效果,比如:cornerRadius。

具有隐式动画的属性在修改值的时候不需要做任何动画处理就会出现动画效果。

一些具有隐式动画的属性(更多属性请看官方文档或进入文件查看,都会有标注的):

  • position
  • backgroundColor
  • bounds

如果修改这些属性不想执行隐式动画可以这样做

        // 开启事务
        CATransaction.begin()
        // 设置是否执行动画
        CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
        customLayer.position = CGPointMake(50.0, 50.0)
        customLayer.backgroundColor = UIColor.redColor().CGColor
        // 提交事务
        CATransaction.commit()

 

CATextLayer

        let textLayer = CATextLayer()
        textLayer.bounds = CGRectMake(0, 0, 300, 100)
        textLayer.position = CGPointMake(10, 200)
        textLayer.anchorPoint = CGPointMake(0.0, 0.0)
        textLayer.backgroundColor = UIColor.yellowColor().CGColor
        textLayer.string = "一些字符串,一些字符串,一些字符串,一些字符串,"
        textLayer.cornerRadius = 10
        textLayer.alignmentMode = kCAAlignmentCenter
        textLayer.wrapped = false
        textLayer.truncationMode = kCATruncationEnd
        textLayer.contentsScale = UIScreen.mainScreen().scale
        let font = UIFont(name: "Helvetica-Bold", size: 13)
        let fontRef = CGFontCreateWithFontName(font!.fontName)
        textLayer.font = fontRef!
        textLayer.fontSize = font!.pointSize
        textLayer.foregroundColor = UIColor.blackColor().CGColor
        view.layer.addSublayer(textLayer)
        
        self.textLayer = textLayer

一些常用属性(更多参照官方文档):

  • string:要显示的字符串,可以是富文本,有隐式动画。
  • alignmentMode:排列模式(居中,靠左,靠右),有隐式动画。
  • wrapped:包裹字符串(是否换行显示)。
  • trancationMode:裁剪字符串模式,当超出范围时指定如何裁剪字符串。
  • contentsScale:内容缩放,需要设置为屏幕的scale,否则可能会出现字体模糊现象。
  • font:字体,有隐式动画。
  • fontSize:字体大小,有隐式动画。
  • foregroundColor:字体颜色,有隐式动画。

 

转载于:https://www.cnblogs.com/Alex-sk/p/5577006.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值