Swift 中仅平滑顶角和图像视图
Smooth only top angles and imageview in Swift
我试图只圆化视图和图像视图的顶部角(TopLeft 和 TopRight)。使用下面的代码,我只能将左角四舍五入(尽管也指定了 TopRight)。也许他没有看到故事板上的约束。
我该如何解决?
谢谢。
let rectShape = CAShapeLayer()
rectShape.bounds = self.CopertinaImage1.frame
rectShape.position = self.CopertinaImage1.center
rectShape.path = UIBezierPath(roundedRect: self.CopertinaImage1.bounds, byRoundingCorners: [.TopRight, .TopLeft], cornerRadii: CGSize(width: 10, height: 10)).CGPath
self.CopertinaImage1.layer.backgroundColor = UIColor.greenColor().CGColor
self.CopertinaImage1.layer.mask = rectShape
右边的角总是一样的:
编辑:
我尝试将以下代码放入 viewDidLayoutSubviews
:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let rectShape = CAShapeLayer()
rectShape.bounds = self.CopertinaImage1.frame
rectShape.position = self.CopertinaImage1.center
rectShape.path = UIBezierPath(roundedRect: self.CopertinaImage1.bounds, byRoundingCorners: [.TopRight, .TopLeft], cornerRadii: CGSize(width: 10, height: 10)).CGPath
self.CopertinaImage1.layer.backgroundColor = UIColor.greenColor().CGColor
self.CopertinaImage1.layer.mask = rectShape
}
但是没有用。
我刚刚在操场上试过你的代码。对我来说很好。可能是我理解错了。
我试过你发布的代码。无论出于何种原因,当我输入您的约束并重新创建您的视图层次结构时,自动布局引擎在第二次通过后不会调用 viewDidLayoutSubviews
。我可以强制它但从 viewWillAppear
调用 self.view.layoutIfNeeded()
。例如:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let rectShape = CAShapeLayer()
rectShape.frame = self.imageView.bounds
rectShape.path = UIBezierPath(roundedRect: self.imageView.bounds, byRoundingCorners: [.TopRight, .TopLeft], cornerRadii: CGSize(width: 10, height: 10)).CGPath
self.imageView.layer.backgroundColor = UIColor.greenColor().CGColor
self.imageView.layer.mask = rectShape
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.view.layoutIfNeeded()
}
结果:
两个角都是圆的。
我想说一个更好、更优雅的避免这种自动布局故障的解决方案是只设置图像视图容器的角半径,因为看起来你正在做的事情。例如:
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.clipsToBounds = true
self.imageContainerView.layer.cornerRadius = 10.0
self.imageContainerView.layer.masksToBounds = true
}
您也可以从 viewDidLoad
执行此操作,因为它不依赖于您设置的任何视图框架。
我试图只圆化视图和图像视图的顶部角(TopLeft 和 TopRight)。使用下面的代码,我只能将左角四舍五入(尽管也指定了 TopRight)。也许他没有看到故事板上的约束。
我该如何解决?
谢谢。
let rectShape = CAShapeLayer()
rectShape.bounds = self.CopertinaImage1.frame
rectShape.position = self.CopertinaImage1.center
rectShape.path = UIBezierPath(roundedRect: self.CopertinaImage1.bounds, byRoundingCorners: [.TopRight, .TopLeft], cornerRadii: CGSize(width: 10, height: 10)).CGPath
self.CopertinaImage1.layer.backgroundColor = UIColor.greenColor().CGColor
self.CopertinaImage1.layer.mask = rectShape
右边的角总是一样的:
我尝试将以下代码放入 viewDidLayoutSubviews
:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let rectShape = CAShapeLayer()
rectShape.bounds = self.CopertinaImage1.frame
rectShape.position = self.CopertinaImage1.center
rectShape.path = UIBezierPath(roundedRect: self.CopertinaImage1.bounds, byRoundingCorners: [.TopRight, .TopLeft], cornerRadii: CGSize(width: 10, height: 10)).CGPath
self.CopertinaImage1.layer.backgroundColor = UIColor.greenColor().CGColor
self.CopertinaImage1.layer.mask = rectShape
}
但是没有用。
我刚刚在操场上试过你的代码。对我来说很好。可能是我理解错了。
我试过你发布的代码。无论出于何种原因,当我输入您的约束并重新创建您的视图层次结构时,自动布局引擎在第二次通过后不会调用 viewDidLayoutSubviews
。我可以强制它但从 viewWillAppear
调用 self.view.layoutIfNeeded()
。例如:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let rectShape = CAShapeLayer()
rectShape.frame = self.imageView.bounds
rectShape.path = UIBezierPath(roundedRect: self.imageView.bounds, byRoundingCorners: [.TopRight, .TopLeft], cornerRadii: CGSize(width: 10, height: 10)).CGPath
self.imageView.layer.backgroundColor = UIColor.greenColor().CGColor
self.imageView.layer.mask = rectShape
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.view.layoutIfNeeded()
}
结果:
两个角都是圆的。
我想说一个更好、更优雅的避免这种自动布局故障的解决方案是只设置图像视图容器的角半径,因为看起来你正在做的事情。例如:
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.clipsToBounds = true
self.imageContainerView.layer.cornerRadius = 10.0
self.imageContainerView.layer.masksToBounds = true
}
您也可以从 viewDidLoad
执行此操作,因为它不依赖于您设置的任何视图框架。