简体   繁体   English

如何适当地创建具有自定义形状的UIView?

[英]How to create UIView with custom shape propely?

I have to create UIView with custom shape, such as triangular, half-rect, etc. I used to crop special image to that form and set as a background of my view. 我必须创建具有自定义形状的UIView ,例如三角形,半矩形等。我曾经将特殊图像裁剪为该形式,并设置为视图的背景。 Although it is a popular solution, I am not sure whether it is the most efficient one in terms of sustainability. 尽管这是一种流行的解决方案,但我不确定它是否在可持续性方面是最有效的解决方案。

On the other hand, I found useful way of solving this problem with CAShapeLayer() 另一方面,我找到了使用CAShapeLayer()解决此问题的有用方法

Could you please provide pros and cons of both approaches? 您能否提供两种方法的利弊?

CAShapeLayer all the way! 一路CAShapeLayer!

With little effort you can achieve the same result with less memory (RAM) and less maintenance time (if you want to make the triangle thicker for example, you'll need a new image however a small change in code and you have it!). 只需花费很少的精力,您就可以用更少的内存(RAM)和更少的维护时间来达到相同的结果(例如,如果您想使三角形变粗,则需要一个新的图像,但是在代码上进行很小的改动就可以了!) 。 Moreover your app size will be relatively smaller and you don't have to worry about the resolution like the images. 此外,您的应用程序大小将相对较小,并且您不必担心图像等分辨率。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM