亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何為UIView的左上角和右上角設置角半徑?

如何為UIView的左上角和右上角設置角半徑?

狐的傳說 2019-06-29 15:03:55
如何為UIView的左上角和右上角設置角半徑?有沒有辦法cornerRadius的左上角和右上角UIView?我試過以下幾種方法,但最后都看不到風景了。UIView *view = [[UIView alloc] initWithFrame:frame];CALayer *layer = [CALayer layer];UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRoundedRect:frame byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(3.0, 3.0)];layer.shadowPath = shadowPath.CGPath;view.layer.mask = layer;
查看完整描述

3 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

SWIFT 4

請注意這樣一個事實,即如果您的UIView子類中附加了布局約束,則必須按如下方式刷新它:

override func layoutSubviews() {
    super.layoutSubviews()
    roundCorners(corners: [.topLeft, .topRight], radius: 3.0)}

如果你不這么做,它就不會出現。


在圓角處,使用分機:

extension UIView {
   func roundCorners(corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        layer.mask = mask    }}


查看完整回答
反對 回復 2019-06-29
?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

我不知道為什么您的解決方案不起作用,但下面的代碼對我有用。創建一個Bezier掩碼并將其應用到您的視圖中。在下面的代碼中,我正在舍入_backgroundView半徑為3像素。self是一種習俗UITableViewCell:

UIBezierPath?*maskPath?=?[UIBezierPath
????bezierPathWithRoundedRect:self.backgroundImageView.bounds
????byRoundingCorners:(UIRectCornerBottomLeft?|?UIRectCornerBottomRight)
????cornerRadii:CGSizeMake(20,?20)];CAShapeLayer?*maskLayer?=?[CAShapeLayer?layer];maskLayer.frame?=?self.bounds;maskLayer.path?=?maskPath.CGPath;self.backgroundImageView.layer.mask?=?maskLayer;

有一些改進的SWIFT版本:

let?path?=?UIBezierPath(roundedRect:viewToRound.bounds,?byRoundingCorners:[.TopRight,?.BottomLeft],?cornerRadii:?CGSizeMake(20,?20))let?maskLayer?=?CAShapeLayer()maskLayer.path?=?path.CGPathviewToRound.layer.mask?=?maskLayer

SWIFT 3.0版本:

let?path?=?UIBezierPath(roundedRect:viewToRound.bounds,
????????????????????????byRoundingCorners:[.topRight,?.bottomLeft],
????????????????????????cornerRadii:?CGSize(width:?20,?height:??20))let?maskLayer?=?CAShapeLayer()maskLayer.path?=?path.cgPath
viewToRound.layer.mask?=?maskLayer


查看完整回答
反對 回復 2019-06-29
?
四季花海

TA貢獻1811條經驗 獲得超5個贊

這里有一個斯威夫特@JohnnyRockex答案的版本

extension UIView {

    func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
         let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
         let mask = CAShapeLayer()
         mask.path = path.cgPath
         self.layer.mask = mask    }}

view.roundCorners([.topLeft, .bottomRight], radius: 10)

如果你用自動布局,您需要將您的UIView打電話roundCorners在視野中layoutSubviews為了達到最佳效果。

class View: UIView {
    override func layoutSubviews() {
        super.layoutSubviews()

        self.roundCorners([.topLeft, .bottomLeft], radius: 10)
    }}


查看完整回答
反對 回復 2019-06-29
  • 3 回答
  • 0 關注
  • 933 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號