swift

时间:2018-01-16 10:33:09

标签: swift swift3

我正在尝试制作图像。我总是使用maskCircle,但这次它不起作用。我在UICollectionViewCell的awakeFromNib中编写了代码,因为我在cellForItemAt函数中将单元格出列,所以我无法使用viewDidLayoutSubviews。所以这是代码:

@IBOutlet weak var storeLogo: UIImageView!
override func awakeFromNib() {
    super.awakeFromNib()
    storeLogo.maskCircle()
    storeLogo.clipsToBounds = true
}

,imageView看起来像这样: imageView

编辑: 它看起来工作正常,但我又遇到了问题。 这是现在awakeFromNib中的代码:

    super.awakeFromNib()
    storeLogo.clipsToBounds = true
    storeLogo.layer.cornerRadius = storeLogo.frame.width / 2

    let gradient = CAGradientLayer()
    gradient.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 55, height: 55))
    gradient.startPoint = CGPoint(x: 0, y: 1)
    gradient.endPoint = CGPoint(x: 1, y: 0)
    gradient.colors = [Color.storeName?.cgColor, Color.idTint?.cgColor]

    let shape = CAShapeLayer()
    shape.lineWidth = 2
    let radius: CGFloat = 27
    shape.path = UIBezierPath(roundedRect: CGRect(x: -2.5, y: -14, width: 2.0 * radius, height: 2.0 * radius), cornerRadius: radius).cgPath
    shape.position = CGPoint(x: self.frame.midX - radius, y: self.frame.midY - radius)
    shape.strokeColor = UIColor.black.cgColor
    shape.fillColor = UIColor.clear.cgColor
    gradient.mask = shape

    self.storeLogo.layer.addSublayer(gradient)

并且图片不是圆形,如下图所示:imageView

我不知道为什么图像现在不是圆圈。任何想法?!

1 个答案:

答案 0 :(得分:0)

如果您想在UIImageView设置cornerRadius UIImageView storeLogo.layer.cornerRadius = storeLogo.frame.width / 2 storeLogo.clipsToBounds = true 来进行圆形剪辑。

{{1}}