After failing to get a live image filter on a Metal view to perform better, I was curious if my UI had some kind of impact. I have an extension on UIButton that adds a blur and shadow to them. If I comment out the stuff inside the functions, and run it, the GPU works twice as hard and the battery impact races beyond high in the debug navigator.
extension UIButton
{
func addCircularBlur(radius:CGFloat)
{
let blur = UIVisualEffectView(effect: UIBlurEffect(style: .regular))
blur.frame = self.bounds
blur.layer.cornerRadius = radius * self.bounds.size.width
blur.clipsToBounds = true
blur.isUserInteractionEnabled = false
blur.autoresizingMask = [.flexibleRightMargin, .flexibleLeftMargin, .flexibleBottomMargin, .flexibleTopMargin, .flexibleWidth, .flexibleHeight]
self.insertSubview(blur, at: 0)
if let imageView = self.imageView{
self.bringSubview(toFront: imageView)
}
}
func addShadow()
{
self.imageView?.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1.0).cgColor
self.imageView?.layer.shadowOffset = CGSize(width:0.0, height:0.0)
self.imageView?.layer.shadowOpacity = 0.1
self.imageView?.layer.shadowRadius = 2.0
self.imageView?.layer.masksToBounds = false
self.imageView?.layer.cornerRadius = 4.0
}
}
Here's how I'm calling these functions. This example uses a button that is from Interface Builder, but it can be added to a button programmatically:
@IBOutlet weak var takePhotoBtn: UIButton!
// inside viewDidLoad:
takePhotoBtn.addCircularBlur(radius:0.5)
takePhotoBtn.addShadow()
What about this is making my device perform better when the blur and shadows are ON?