我需要创建一个左边框与45度倾斜的,我想知道,有什么方法可以编程实现这个目标吗?在这种情况下,CATransform3D对我有帮助吗?因为它不是真正的“3D旋转”吗?
编辑
这里有一张图片解释了我所需要的输出
发布于 2014-10-13 04:48:29
如果您只想要没有内容的形状,那么您可以创建一个CAShapeLayer
并将其添加到视图的层中。(实际上,您也可以使用此方法将内容放入其中,但您需要稍微修改它)。
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
或任何东西。您必须根据所需的值更改坐标。
您还可以使用UIView
子类并重写drawRect
。它需要更多的工作,但UIBezierPath
将基本相同。
CALayer
功能非常强大,被苹果公司广泛使用。例如,页面中的编辑画布几乎完全使用CALayer
编写。
发布于 2020-02-11 02:56:23
Swift5中的代码
class CustomView: UIView {
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
// Get Height and Width
//let layerHeight = layer.frame.height
let layerWidth = layer.frame.width
// Create Path
let bezierPath = UIBezierPath()
// Points
let pointA = CGPoint(x: 0, y: 30)
let pointB = CGPoint(x: 20, y: 0)
let pointC = CGPoint(x: layerWidth, y: 0)
let pointD = CGPoint(x: layerWidth, y:30)
// Draw the path
bezierPath.move(to: pointA)
bezierPath.addLine(to: pointB)
bezierPath.addLine(to: pointC)
bezierPath.addLine(to: pointD)
bezierPath.close()
// Mask to Path
let shapeLayer = CAShapeLayer()
shapeLayer.path = bezierPath.cgPath
layer.mask = shapeLayer
}
}
发布于 2014-10-13 04:31:32
实现这一目标的最短途径可能是采取以下步骤:
UIView
drawRect
为视图提供自定义绘图drawRect
中,使用Core Graphics API
,它允许您在绘图上下文中绘制自定义形状,并提供诸如rotation
或scaling
之类的函数。https://stackoverflow.com/questions/26339943
复制相似问题