【发布时间】:2019-03-03 06:03:40
【问题描述】:
我编写的代码适用于 iPhone X,但不适用于 iPhone SE。 Here's what we have on iPhone SE
有人知道为什么它可以在 iPhone X 上正常工作,而在 iPhone SE 上却不能工作
我在viewDidLayoutSubviews()中调用了这个函数
extension UIView {
enum Direction {
case horizontal
case vertical
}
func addGradient(cornerRadius: CGFloat, colors: [UIColor], lineWidth: CGFloat, direction: Direction) {
self.layer.cornerRadius = cornerRadius
let gradient = CAGradientLayer()
gradient.frame = bounds
gradient.colors = colors.map({ (color) -> CGColor in
color.cgColor
})
switch direction {
case .horizontal:
gradient.startPoint = CGPoint(x: 0, y: 1)
gradient.endPoint = CGPoint(x: 1, y: 1)
case .vertical:
gradient.startPoint = CGPoint(x: 0, y: 0)
gradient.endPoint = CGPoint(x: 0, y: 1)
}
var shadowLayer: CAShapeLayer!
shadowLayer = CAShapeLayer()
shadowLayer.lineWidth = lineWidth
shadowLayer.path = UIBezierPath(roundedRect: self.bounds.insetBy(dx: lineWidth, dy: lineWidth), cornerRadius: cornerRadius).cgPath
shadowLayer.fillColor = nil
shadowLayer.strokeColor = UIColor.black.cgColor
gradient.mask = shadowLayer
self.layer.addSublayer(gradient)
}
}
【问题讨论】:
-
什么时候调用
addGradient函数? -
@AndréSlotta in viewDidLayoutSubviews()
-
你能显示
viewDidLayoutSubviews的代码吗? -
所以我猜
addGradient()被调用了两次。它以正确的方法完成(在布局完成之后),但如果之前有一个图层,您应该删除该图层以避免您的双重问题(或修复另一个使其被调用两次的问题)。 -
@AndréSlotta serversButtonOutlet.addGradient(cornerRadius: 31.0, colors: [UIColor(red: 49/256, green: 211/256, blue: 179/256, alpha: 1.0), UIColor(red: 68/256,绿色:164/256,蓝色:214/256,alpha:1.0)],线宽:2.0,方向:.horizontal)
标签: ios swift user-interface uibutton gradient