From 6ce96764f34fc6afb175f6a7913d1217ea5bfc1c Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 17 Nov 2019 12:28:21 -0500 Subject: [PATCH] Use visual effect views for sensitive media hide button so the button is visible regardless of the image color --- Tusker/Views/Attachments/AttachmentView.swift | 9 +--- .../AttachmentsContainerView.swift | 54 +++++++++++++------ 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/Tusker/Views/Attachments/AttachmentView.swift b/Tusker/Views/Attachments/AttachmentView.swift index 6771d73e57..e65b2d8d51 100644 --- a/Tusker/Views/Attachments/AttachmentView.swift +++ b/Tusker/Views/Attachments/AttachmentView.swift @@ -105,14 +105,7 @@ class AttachmentView: UIImageView, GIFAnimatable { } @objc func imagePressed() { -// switch attachment.kind { -// case .image: - delegate?.showAttachmentsGallery(startingAt: index) -// case .video: -// delegate?.showVideo(attachment: attachment) -// default: -// fatalError() -// } + delegate?.showAttachmentsGallery(startingAt: index) } } diff --git a/Tusker/Views/Attachments/AttachmentsContainerView.swift b/Tusker/Views/Attachments/AttachmentsContainerView.swift index 6faf7f73fb..f1d8738797 100644 --- a/Tusker/Views/Attachments/AttachmentsContainerView.swift +++ b/Tusker/Views/Attachments/AttachmentsContainerView.swift @@ -19,17 +19,17 @@ class AttachmentsContainerView: UIView { let attachmentViews: NSHashTable = .weakObjects() var blurView: UIVisualEffectView? - var hideButton: UIButton? + var hideButtonView: UIVisualEffectView? var contentHidden: Bool! { didSet { guard let blurView = blurView, - let hideButton = hideButton else { return } + let hideButtonContainerView = hideButtonView else { return } blurView.alpha = contentHidden ? 0 : 1 - hideButton.alpha = contentHidden ? 1 : 0 + hideButtonContainerView.alpha = contentHidden ? 1 : 0 UIView.animate(withDuration: 0.2) { blurView.alpha = self.contentHidden ? 1 : 0 - hideButton.alpha = self.contentHidden ? 0 : 1 + hideButtonContainerView.alpha = self.contentHidden ? 0 : 1 } } } @@ -231,21 +231,43 @@ class AttachmentsContainerView: UIView { } private func createHideButton() { - let hideButton = UIButton() - hideButton.translatesAutoresizingMaskIntoConstraints = false - hideButton.alpha = 0 - hideButton.layer.cornerRadius = 2 - hideButton.layer.masksToBounds = true - hideButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal) - hideButton.addTarget(self, action: #selector(hideButtonTapped), for: .touchUpInside) + let blurEffect = UIBlurEffect(style: .regular) + let hideButtonBlurView = UIVisualEffectView(effect: blurEffect) + hideButtonBlurView.translatesAutoresizingMaskIntoConstraints = false + hideButtonBlurView.alpha = 0 + hideButtonBlurView.isUserInteractionEnabled = true + hideButtonBlurView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hideButtonTapped))) + addSubview(hideButtonBlurView) + self.hideButtonView = hideButtonBlurView - addSubview(hideButton) + let maskLayer = CALayer() + let image = UIImage(systemName: "eye.slash.fill")! + maskLayer.contents = image.cgImage! + maskLayer.frame = CGRect(origin: .zero, size: image.size) + hideButtonBlurView.layer.mask = maskLayer + + let hideButtonVibrancyView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: blurEffect, style: .label)) + hideButtonVibrancyView.translatesAutoresizingMaskIntoConstraints = false + hideButtonBlurView.contentView.addSubview(hideButtonVibrancyView) + let fillView = UIView() + fillView.translatesAutoresizingMaskIntoConstraints = false + fillView.backgroundColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 0.5) + hideButtonVibrancyView.contentView.addSubview(fillView) + NSLayoutConstraint.activate([ - hideButton.topAnchor.constraint(equalTo: topAnchor, constant: 8), - hideButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8) + hideButtonBlurView.topAnchor.constraint(equalTo: topAnchor, constant: 8), + hideButtonBlurView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8), + hideButtonBlurView.widthAnchor.constraint(equalToConstant: image.size.width), + hideButtonBlurView.heightAnchor.constraint(equalToConstant: image.size.height), + hideButtonVibrancyView.leadingAnchor.constraint(equalTo: hideButtonBlurView.contentView.leadingAnchor), + hideButtonVibrancyView.trailingAnchor.constraint(equalTo: hideButtonBlurView.contentView.trailingAnchor), + hideButtonVibrancyView.topAnchor.constraint(equalTo: hideButtonBlurView.contentView.topAnchor), + hideButtonVibrancyView.bottomAnchor.constraint(equalTo: hideButtonBlurView.contentView.bottomAnchor), + fillView.leadingAnchor.constraint(equalTo: hideButtonBlurView.contentView.leadingAnchor), + fillView.trailingAnchor.constraint(equalTo: hideButtonBlurView.contentView.trailingAnchor), + fillView.topAnchor.constraint(equalTo: hideButtonBlurView.contentView.topAnchor), + fillView.bottomAnchor.constraint(equalTo: hideButtonBlurView.contentView.bottomAnchor), ]) - - self.hideButton = hideButton } private func fillView(_ view: UIView, in parentView: UIView? = nil) {