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

ios – 如何在Swift 3中使用@IBInspectable分别控制UIView的圆角?,第1张

概述这是我目前的代码: override func viewDidLoad() { super.viewDidLoad() let rect = Draw(frame: CGRect( origin: CGPoint(x: 50, y: 50), size: CGSize(width: 100, height: 100))) self.view 这是我目前的代码:

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属性中的值处理每个角.

这是代码

////  CornerVIEw.swift//  UIVIEwCornerRounded////  Created by ReinIEr Melian on 21/07/2017.//  copyright © 2017 Pruebas. All rights reserved.//import UIKit@IBDesignableclass 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),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),y: 0))        path.close()        return path    }}

结果如下

使用此值

希望这可以帮助

总结

以上是内存溢出为你收集整理的ios – 如何在Swift 3中使用@IBInspectable分别控制UIView的圆角?全部内容,希望文章能够帮你解决ios – 如何在Swift 3中使用@IBInspectable分别控制UIView的圆角?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1059988.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存