CALayer在iPhone / iPad之间有不同的效果

时间:2018-04-19 13:35:56

标签: ios objective-c iphone ipad calayer

这让我有点困惑。我有一个通用应用程序,它有两个不同的iPhone和iPad故事板。我使用一小块@IBOutlet weak var x1: UITextField! @IBOutlet weak var x2: UITextField! @IBOutlet weak var y1: UITextField! @IBOutlet weak var y2: UITextField! @IBOutlet weak var z: UITextField! @IBOutlet weak var show: UITextView! @IBAction func result(_ sender: UIButton) { let X1 = Int(x1.text!)! let X2 = Int(x2.text!)! let Y1 = Int(y1.text!)! let Y2 = Int(y2.text!)! let Z = Int(z.text!)! if X1...X2 ~= Z || Y1...Y2 ~= Z { for X in stride(from: X1, to: Z, by: 1) { for Y in stride(from: Y1, to: Z, by: 1) { print(" \(X) × \(Y) = \(X * Y) ,", terminator:"") }} for X in stride(from: Z+1, through: X2, by: 1) { for Y in stride(from: Z+1, through: Y2, by: 1) { print(" \(X) × \(Y) = \(X * Y) ,", terminator:"") }} }else{ for X in stride(from: X1, through: X2, by: 1) { for Y in stride(from: Y1, through: Y2, by: 1) { print(" \(X) × \(Y) = \(X * Y) ,", terminator:"") }} } show.text = print("") 代码来围绕视图的边缘并在其上投射阴影。在iPhone上完美运行。

CALayer

在iPad上,它给我一个钻石形状。我必须将角半径从一半改为四分之一然后才能正常工作。但当然,iPhone视图从圆圈变为圆形矩形。

我错过了什么?

iPhone view

enter image description here

1 个答案:

答案 0 :(得分:1)

在视图布局完成后,您应该调用configShadowLayer:cornerRadius:shadowOffsetX:shadowOffsetY:shadowRadius:opacity:。当你在布局过程之前调用它时,视图的框架可能有错误的尺寸,并且你会得到一个太大的角半径。

BTW:您应该根据视图的边界计算半径,因为边界代表视图内部的坐标系。