CATiledLayer的使用
文章图片
效果图.png 【CATiledLayer的使用】使用CATiledLayer解决加载大图的问题。如:地图
class ViewController: UIViewController, CALayerDelegate, UIScrollViewDelegate {var contentView:UIView?@IBOutlet weak var scrollView: UIScrollView!override func viewDidLoad() {
super.viewDidLoad()
let rect = CGRect(x: 0, y: 0, width: 1280, height: 960)let contentView = UIView(frame: rect)
//瓦片
let tiledLayer = CATiledLayer()
tiledLayer.frame = rect
tiledLayer.delegate = self
contentView.layer.addSublayer(tiledLayer)
scrollView.addSubview(contentView)
scrollView.contentSize = contentView.frame.size
//draw layer
tiledLayer.setNeedsDisplay()
self.contentView = contentView
}func draw(_ layer: CALayer, in ctx: CGContext) {
let layer = layer as! CATiledLayer//配置瓦片位置
let bounds = ctx.boundingBoxOfClipPath
let x = bounds.origin.x / layer.tileSize.width
let y = bounds.origin.y / layer.tileSize.height//加载瓦片图片
let imageName = "LiuYan_0\(Int(x))_0\(Int(y))"//大图切成的小图
let tileImage = UIImage(named: imageName)//draw tile
UIGraphicsPushContext(ctx)
tileImage?.draw(in: bounds)
UIGraphicsPopContext()
}func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self.contentView
}
}
Github地址: https://github.com/LJjack/LearnCATiledLayer
推荐阅读
- 热闹中的孤独
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 一个人的旅行,三亚
- 布丽吉特,人生绝对的赢家
- 慢慢的美丽
- 尽力
- 一个小故事,我的思考。
- 家乡的那条小河
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量