如何在ios中绘制自定义UIView?

时间:2017-02-07 17:34:09

标签: ios iphone uiview

如何绘制自定义视图,如图所示。 我想在主视图中绘制一个小矩形。

如果你有,请参考任何链接。

图像

enter image description here

谢谢

1 个答案:

答案 0 :(得分:2)

可能就像下面那样简单

import UIKit

class Rectangle:UIView{

    override func draw(_ rect: CGRect) {
        let path = UIBezierPath(rect: rect)
        path.move(to: CGPoint(x:0,y:rect.height))
        path.addLine(to: CGPoint(x:rect.width/2,y:rect.height/1.4))
        path.addLine(to: CGPoint(x:rect.width,y:rect.height))
        UIColor.red.setStroke()
        path.stroke()
    }

}

let rect=Rectangle(frame: CGRect(x: 0, y: 0, width: 100, height: 100))