创建一个只有一个对角线的UIView [英] Create a UIView with only one diagonal side
问题描述
我需要创建一个UIView,左边框倾斜 45度
我想知道,有没有办法以编程方式实现这个?
在这种情况下 CATransform3D 是否可以帮助我,因为它不是真正的3D旋转?
I need to create a UIView that has the left border inclined with 45 degrees I was wondering,is there a way to acheive this programmatically? Does CATransform3D help me in this case since it’s not really a "3D rotation"?
编辑
这是一张图片,解释了我需要的更多输出
Here's an image explaining more my needed output
推荐答案
如果您只想要形状如果没有内容,则可以创建 CAShapeLayer
并将其添加到视图的图层中。 (事实上你也可以使用这种方法将内容放在那里,但你需要稍微修改它。)
If you just want the shape with no content then you can create a CAShapeLayer
and add it to your view's layer. (In fact you can also put content in there using this method but you'll need to alter it a bit).
CAShapeLayer *layer = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 100)]; // bottom left corner
[path addLineToPoint:CGPointMake(100, 0)]; // top middle
[path addLineToPoint:CGPointMake(300, 0)]; // top right corner
[path addLineToPoint:CGPointMake(300, 100)]; // bottom right corner
[path closePath];
layer.path = path.CGPath;
layer.fillColor = [UIColor blackColor].CGColor;
layer.strokeColor = nil;
[theView.layer addSubLayer:layer];
这不需要使用 drawRect
或任何东西。您必须根据所需的值更改坐标。
This doesn't require using drawRect
or anything. You will have to change the coordinates based on the values you want.
您还可以使用 UIView
子类和覆盖 drawRect
。它需要更多的工作,但 UIBezierPath
将几乎相同。
You can also use a UIView
subclass and override drawRect
. It requires more work but the UIBezierPath
will be pretty much the same.
CALayer
非常强大,Apple使用了很多。例如,Pages中的编辑画布几乎只使用 CALayer
来编写。
CALayer
is very powerful and used a lot by Apple. For instance the edit canvas in Pages is written almost exclusively using CALayer
s.
这篇关于创建一个只有一个对角线的UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!