向UIView添加圆角会禁用touchesBegan

时间:2015-11-07 14:01:14

标签: ios swift uiview

我以这种方式向我的UIView添加了底部圆角:

let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.BottomLeft, .BottomRight], cornerRadii: CGSizeMake(20, 20)).CGPath
self.layer.mask = maskLayer

以前是这样的:enter image description here

在:enter image description here

有两个问题。底部的第一张图片消失,其次是touchesBegantouchesMoved等未被调用。

图片有3个约束:

enter image description here

2 个答案:

答案 0 :(得分:0)

另一种圆角化方法(从未导致这些问题)是将这两种方法应用于您的视图:

myView.clipsToBounds = true
myView.layer.cornerRadius = 5 (play with this to get what you want!)

答案 1 :(得分:0)

将您的代码放在override layoutSubviews中,以便它使用更新的边界:

override func layoutSubviews() {
    super.layoutSubviews()
    let maskLayer = CAShapeLayer()
    maskLayer.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.BottomLeft, .BottomRight], cornerRadii: CGSizeMake(20, 20)).CGPath
    self.layer.mask = maskLayer
}