From 352777765e4413376f707b5d3f9c1c55ba93e3c6 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 26 Sep 2018 15:50:29 -0400 Subject: [PATCH] Add inner shadows to profile pics. --- SignalMessaging/Views/AvatarImageView.swift | 46 ++++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/SignalMessaging/Views/AvatarImageView.swift b/SignalMessaging/Views/AvatarImageView.swift index b2481f7ab..9c71c95e1 100644 --- a/SignalMessaging/Views/AvatarImageView.swift +++ b/SignalMessaging/Views/AvatarImageView.swift @@ -7,6 +7,8 @@ import UIKit @objc public class AvatarImageView: UIImageView { + private let shadowLayer = CAShapeLayer() + public init() { super.init(frame: .zero) self.configureView() @@ -32,14 +34,54 @@ public class AvatarImageView: UIImageView { self.layer.minificationFilter = kCAFilterTrilinear self.layer.magnificationFilter = kCAFilterTrilinear - self.layer.borderWidth = 0.5 self.layer.masksToBounds = true + + self.layer.addSublayer(self.shadowLayer) + self.contentMode = .scaleToFill } override public func layoutSubviews() { - self.layer.borderColor = UIColor(white: 0, alpha: 0.15).cgColor + updateLayers() + } + + @objc public override var bounds: CGRect { + didSet { + if oldValue != bounds { + updateLayers() + } + } + } + + @objc public override var frame: CGRect { + didSet { + if oldValue != frame { + updateLayers() + } + } + } + + private func updateLayers() { self.layer.cornerRadius = self.frame.size.width / 2 + + // Inner shadow. + // This should usually not be visible; it is used to distinguish + // profile pics from the background if they are similar. + self.shadowLayer.frame = self.bounds + self.shadowLayer.masksToBounds = true + let shadowBounds = self.bounds + let shadowPath = UIBezierPath(ovalIn: shadowBounds) + // This can be any value large enough to cast a sufficiently large shadow. + let shadowInset: CGFloat = -3 + shadowPath.append(UIBezierPath(rect: shadowBounds.insetBy(dx: shadowInset, dy: shadowInset))) + // This can be any color since the fill should be clipped. + self.shadowLayer.fillColor = UIColor.black.cgColor + self.shadowLayer.path = shadowPath.cgPath + self.shadowLayer.fillRule = kCAFillRuleEvenOdd + self.shadowLayer.shadowColor = (Theme.isDarkThemeEnabled ? UIColor.white : UIColor.black).cgColor + self.shadowLayer.shadowRadius = 0.5 + self.shadowLayer.shadowOpacity = 0.15 + self.shadowLayer.shadowOffset = .zero } }