ios - 创建具有倾斜边缘的容器

标签 ios swift user-interface

我有一个客户在他的设计中提出以下要求:

创建底部有倾斜边缘的黄色容器的最简单方法是什么?我可以在界面生成器中完成还是必须以编程方式完成?我正在尝试快速执行此操作。

最佳答案

Can I do it within the interface builder or do I have to do it programmatically?

您必须以编程方式执行此操作:定义 UIBezierPath,将其用作 CAShapeLayerpath,然后屏蔽 View 的使用此形状图层的图层。

@IBDesignable
class SlantedView: UIView {

    @IBInspectable var slantHeight: CGFloat = 50 { didSet { updatePath() } }

    private let shapeLayer: CAShapeLayer = {
        let shapeLayer = CAShapeLayer()
        shapeLayer.lineWidth = 0
        shapeLayer.fillColor = UIColor.white.cgColor    // with masks, the color of the shape layer doesn’t matter; it only uses the alpha channel; the color of the view is dictate by its background color
        return shapeLayer
    }()

    override func layoutSubviews() {
        super.layoutSubviews()
        updatePath()
    }

    private func updatePath() {
        let path = UIBezierPath()
        path.move(to: bounds.origin)
        path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))
        path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
        path.addLine(to: CGPoint(x: bounds.minX, y: bounds.maxY - slantHeight))
        path.close()
        shapeLayer.path = path.cgPath
        layer.mask = shapeLayer
    }
}

但通过将其设为 @IBDesignable View ,您可以在 IB 中呈现它,这样您至少可以设计 UI,更清楚地了解最终产品的外观:

enter image description here

仅供引用,如果您确实使它可设计,他们建议您为您的可设计对象创建一个单独的框架目标(这样如果您在处理主项目时去 IB,渲染可设计 View 的能力不会受到影响因为您的主要项目可能不是稳定的、可编译的状态)。

关于ios - 创建具有倾斜边缘的容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47292743/

相关文章:

javascript - Karate UI 中的 ShadowRoot dom 元素访问问题

user-interface - 您什么时候先设计 GUI,然后再设计后端代码,反之亦然?

iOS 应用程序 - 在 GridView 中显示来自 JSON URL 列表的图像。

ios - rootViewController 的用途是什么,你什么时候切换它?

ios - 显示来自非 UI 类的警报

ios - Xcode Storyboard 上的并排约束

ios - 如何防止 TableView 单元格重复? swift

java - 在 Swing 工作线程之外更改模型是否可以?

iOS7 XIB 问题。顶部和底部的空白

ios - 如何在类名中包含索引号