先拓展一下上篇文章说的点与像素:

iOS使用了以下坐标系统:

1、

iOS CoreAnimation(二)contentsRect,contentsCenter

2、 

iOS CoreAnimation(二)contentsRect,contentsCenter

3、

iOS CoreAnimation(二)contentsRect,contentsCenter


一、CALayer的 contentsRect 属性    允许我们在图层边框里显示寄宿图的一个子域

它使用单位坐标 0~1 ,是一个相对值,默认 { 0,0,1,1 } ,表示整个寄宿图可见,如果指定为 { 0,0,0.5,0.5 },图片就被裁减

iOS CoreAnimation(二)contentsRect,contentsCenter

用法呢?用途呢?书上说写游戏的会用到这个技术,我在这里弄了个demo,虽然我没发现有卵用==

我只有一张图片,是一张。

iOS CoreAnimation(二)contentsRect,contentsCenter


app效果图:

iOS CoreAnimation(二)contentsRect,contentsCenter

书上说写游戏用到,而且有个第三方库专门弄这个==   我为了实现功能的代码(和书上OC的差别很大啊艹):


import UIKit


class ViewController: UIViewController {

    

    var view1:UIView!

    var view2:UIView!

    var view3:UIView!

    var view4:UIView!

    

    ///myView 的大小

    let size:CGFloat = 100

    

    ///屏幕的宽度

    let width = UIScreen.main.bounds.width

    ///屏幕的高度

    let height = UIScreen.main.bounds.height

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        //frame我随便敲的

        view1 = UIView(frame: CGRect(x: 20, y: 50, width: size, height: size))

        view2 = UIView(frame: CGRect(x: 150, y: 250, width: size, height: size))

        view3 = UIView(frame: CGRect(x: 40, y: 380, width: size, height: size))

        view4 = UIView(frame: CGRect(x: 180, y: 520, width: size, height: size))

        

        let img = UIImage(named: "bigImg.png")!

        

//方法在下面,下面!

        view1.addImage(img: img, withContent: CGRect(x: 0, y: 0, width: 0.5, height: 0.5))

        view2.addImage(img: img, withContent: CGRect(x: 0.5, y: 0.5, width: 0.5, height: 0.5))

        view3.addImage(img: img, withContent: CGRect(x: 0, y: 0.5, width: 0.5, height: 0.5))

        view4.addImage(img: img, withContent: CGRect(x: 0.5, y: 0, width: 0.5, height: 0.5))

        

        view.addSubview(view1)

        view.addSubview(view2)

        view.addSubview(view3)

        view.addSubview(view4)

        

    }

}

extension UIView {

    func addImage(img:UIImage,withContent rect:CGRect){

        layer.contents = img.cgImage

        layer.contentsGravity = kCAGravityResizeAspect

        layer.contentsRect = rect

    }

}


二、 contentsCenter 属性 :我觉得和java的 GridBagLayout 差不多。。。

它定义一个固定边框,在图层上的可拉伸区域。可拉伸区域 强调一下  默认 { 0,0,1,1 } 如果是{ 0.25,0.25,0.5,0.5 }

iOS CoreAnimation(二)contentsRect,contentsCenter

意思是 蓝色水平拉伸,红垂直,黄不变,绿都拉。demo:

原图一张:

iOS CoreAnimation(二)contentsRect,contentsCenter

代码:

import UIKit


class ViewController: UIViewController {

    

    var view1:UIView!

    var view2:UIView!


    ///myView 的大小

    let size:CGFloat = 100

    

    ///屏幕的宽度

    let width = UIScreen.main.bounds.width

    ///屏幕的高度

    let height = UIScreen.main.bounds.height

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        //frame我随便敲的

        view1 = UIView(frame: CGRect(x: 40, y: 50, width: size, height: size*2))

        view2 = UIView(frame: CGRect(x: 150, y: 450, width: 2.4*size, height: 1.2*size))

   

        let img = UIImage(named: "big.png")!

        

        let rect = CGRect(x: 0.25, y: 0.25, width: 0.5, height: 0.5)

        

        view1.addStretchableImage(img: img, withCenter: rect)

        view2.addStretchableImage(img: img, withCenter: rect)

      

        view.addSubview(view1)

        view.addSubview(view2)

    }

}

extension UIView {

    func addStretchableImage(img:UIImage,withCenter rect:CGRect){

        layer.contents = img.cgImage

        layer.contentsCenter = rect

    }

}

效果:看见被拉伸成啥样了吧
iOS CoreAnimation(二)contentsRect,contentsCenter

下篇文章写CoreGraphics绘制BGImage


相关文章:

  • 2021-12-20
  • 2021-05-07
  • 2021-06-23
  • 2022-12-23
  • 2022-03-06
  • 2021-08-07
  • 2021-09-22
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-14
  • 2021-08-07
  • 2021-09-03
  • 2022-12-23
相关资源
相似解决方案