只显示UIView的angular落

如何只显示UIView的angular落?

let view = UIView() view.layer.borderColor = UIColor.white.cgColor view.layer.borderWidth = 2 let maskframe = UIView(frame: CGRect(x:0, y:0, width:view.frame.width, height:view.frame.height)) view.layer.mask = maskframe.layer.` 

这只是掩盖了正确的边缘,我不明白它是如何工作。

喜欢这个

尝试使用这个class ,在这里我使用CoreGraphics的自定义视图绘图,添加了一些Inspectablevariables来帮助定制

 // // CornerView.swift // CornersViewSO // // Created by Reinier Melian on 5/31/17. // Copyright © 2017 Reinier Melian. All rights reserved. // import UIKit import CoreGraphics @IBDesignable class CornerView: UIView { @IBInspectable var sizeMultiplier : CGFloat = 0.2{ didSet{ self.draw(self.bounds) } } @IBInspectable var lineWidth : CGFloat = 2{ didSet{ self.draw(self.bounds) } } @IBInspectable var lineColor : UIColor = UIColor.black{ didSet{ self.draw(self.bounds) } } override init(frame: CGRect) { super.init(frame: frame) self.backgroundColor = UIColor.clear } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.backgroundColor = UIColor.clear } func drawCorners() { let currentContext = UIGraphicsGetCurrentContext() currentContext?.setLineWidth(lineWidth) currentContext?.setStrokeColor(lineColor.cgColor) //first part of top left corner currentContext?.beginPath() currentContext?.move(to: CGPoint(x: 0, y: 0)) currentContext?.addLine(to: CGPoint(x: self.bounds.size.width*sizeMultiplier, y: 0)) currentContext?.strokePath() //top rigth corner currentContext?.beginPath() currentContext?.move(to: CGPoint(x: self.bounds.size.width - self.bounds.size.width*sizeMultiplier, y: 0)) currentContext?.addLine(to: CGPoint(x: self.bounds.size.width, y: 0)) currentContext?.addLine(to: CGPoint(x: self.bounds.size.width, y: self.bounds.size.height*sizeMultiplier)) currentContext?.strokePath() //bottom rigth corner currentContext?.beginPath() currentContext?.move(to: CGPoint(x: self.bounds.size.width, y: self.bounds.size.height - self.bounds.size.height*sizeMultiplier)) currentContext?.addLine(to: CGPoint(x: self.bounds.size.width, y: self.bounds.size.height)) currentContext?.addLine(to: CGPoint(x: self.bounds.size.width - self.bounds.size.width*sizeMultiplier, y: self.bounds.size.height)) currentContext?.strokePath() //bottom left corner currentContext?.beginPath() currentContext?.move(to: CGPoint(x: self.bounds.size.width*sizeMultiplier, y: self.bounds.size.height)) currentContext?.addLine(to: CGPoint(x: 0, y: self.bounds.size.height)) currentContext?.addLine(to: CGPoint(x: 0, y: self.bounds.size.height - self.bounds.size.height*sizeMultiplier)) currentContext?.strokePath() //second part of top left corner currentContext?.beginPath() currentContext?.move(to: CGPoint(x: 0, y: self.bounds.size.height*sizeMultiplier)) currentContext?.addLine(to: CGPoint(x: 0, y: 0)) currentContext?.strokePath() } // Only override draw() if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func draw(_ rect: CGRect) { // Drawing code super.draw(rect) self.drawCorners() } } 

EDITED

示例使用代码

 import UIKit class ViewController: UIViewController { var cornerViewCode : CornerView? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.cornerViewCode = CornerView(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) self.view.addSubview(self.cornerViewCode!) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

这是它的样子

在这里输入图像说明

希望这可以帮助

看看这个UIView:

 class RectangleView: UIView { override init(frame: CGRect) { super.init(frame: frame) } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override func draw(_ rect: CGRect) { let aPath = UIBezierPath() UIColor.black.set() aPath.move(to: CGPoint(x: rect.minX, y: 0.1*rect.maxY)) aPath.addLine(to: CGPoint(x: rect.minX, y: rect.minY)) aPath.addLine(to: CGPoint(x: 20, y: rect.minY)) aPath.stroke() aPath.move(to: CGPoint(x: rect.maxX - 0.1*rect.maxX, y: rect.minY)) aPath.addLine(to: CGPoint(x: rect.maxX, y: rect.minY)) aPath.addLine(to: CGPoint(x: rect.maxX, y: 0.1*rect.maxY)) aPath.stroke() aPath.move(to: CGPoint(x: rect.maxX, y: rect.maxY - 0.1*rect.maxY)) aPath.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY)) aPath.addLine(to: CGPoint(x: rect.maxX - 0.1*rect.maxX, y: rect.maxY)) aPath.stroke() aPath.move(to: CGPoint(x: rect.minX + 0.1*rect.maxX, y: rect.maxY)) aPath.addLine(to: CGPoint(x: rect.minX, y: rect.maxY)) aPath.addLine(to: CGPoint(x: rect.minX, y: rect.maxY - 0.1*rect.maxY)) aPath.stroke() } }