如何在Swift 3中使用@IBInspectable分别控制UIView的圆角?

编程入门 行业动态 更新时间:2024-10-26 12:29:23
本文介绍了如何在Swift 3中使用@IBInspectable分别控制UIView的圆角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我当前的代码:

override func viewDidLoad() { super.viewDidLoad() let rect = Draw(frame: CGRect( origin: CGPoint(x: 50, y: 50), size: CGSize(width: 100, height: 100))) self.view.addSubview(rect) }

推荐答案

使用此自定义类,基本上,您需要使用直线和四边形曲线创建贝塞尔曲线路径,并使用@IBInspectable属性中的值处理每个角.

Use this custom class, basically you need create a bezier path, using lines and quad curves, handling every corner with values in @IBInspectable properties.

// // CornerView.swift // UIViewCornerRounded // // Created by Reinier Melian on 21/07/2017. // Copyright © 2017 Pruebas. All rights reserved. // import UIKit @IBDesignable class CornerView: UIView { @IBInspectable var leftTopRadius : CGFloat = 0{ didSet{ self.applyMask() } } @IBInspectable var rightTopRadius : CGFloat = 0{ didSet{ self.applyMask() } } @IBInspectable var rightBottomRadius : CGFloat = 0{ didSet{ self.applyMask() } } @IBInspectable var leftBottomRadius : CGFloat = 0{ didSet{ self.applyMask() } } override func layoutSubviews() { super.layoutSubviews() self.applyMask() } // Only override draw() if you perform custom drawing. // An empty implementation adversely affects performance during animation. /*override func draw(_ rect: CGRect) { super.draw(rect) }*/ func applyMask() { let shapeLayer = CAShapeLayer(layer: self.layer) shapeLayer.path = self.pathForCornersRounded(rect:self.bounds).cgPath shapeLayer.frame = self.bounds shapeLayer.masksToBounds = true self.layer.mask = shapeLayer } func pathForCornersRounded(rect:CGRect) ->UIBezierPath { let path = UIBezierPath() path.move(to: CGPoint(x: 0 + leftTopRadius , y: 0)) path.addLine(to: CGPoint(x: rect.size.width - rightTopRadius , y: 0)) path.addQuadCurve(to: CGPoint(x: rect.size.width , y: rightTopRadius), controlPoint: CGPoint(x: rect.size.width, y: 0)) path.addLine(to: CGPoint(x: rect.size.width , y: rect.size.height - rightBottomRadius)) path.addQuadCurve(to: CGPoint(x: rect.size.width - rightBottomRadius , y: rect.size.height), controlPoint: CGPoint(x: rect.size.width, y: rect.size.height)) path.addLine(to: CGPoint(x: leftBottomRadius , y: rect.size.height)) path.addQuadCurve(to: CGPoint(x: 0 , y: rect.size.height - leftBottomRadius), controlPoint: CGPoint(x: 0, y: rect.size.height)) path.addLine(to: CGPoint(x: 0 , y: leftTopRadius)) path.addQuadCurve(to: CGPoint(x: 0 + leftTopRadius , y: 0), controlPoint: CGPoint(x: 0, y: 0)) path.close() return path } }

这是结果

使用此值

希望这会有所帮助

更多推荐

如何在Swift 3中使用@IBInspectable分别控制UIView的圆角?

本文发布于:2023-11-26 21:58:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:圆角   如何在   IBInspectable   Swift   UIView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!