zoukankan      html  css  js  c++  java
  • 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:字体颜色,有隐式动画。
  • 相关阅读:
    工厂方法模式
    简单工厂模式
    页面滚动动画
    JAVA的深浅拷备
    雷哥架构师课程大钢
    字节与字符串转换
    商城图片懒加载
    MYSQL数据库类型与JAVA类型对应表
    微信支付二码生成办法
    项目中dubbo的标准配置
  • 原文地址:https://www.cnblogs.com/Alex-sk/p/5577006.html
Copyright © 2011-2022 走看看