Skip to content

Commit

Permalink
Add ZLEnlargeButton class instead of extending UIControl to enlarge b…
Browse files Browse the repository at this point in the history
…utton click area
  • Loading branch information
longitachi committed Apr 24, 2022
1 parent b8f4e2d commit fca9302
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 65 deletions.
12 changes: 6 additions & 6 deletions Sources/Camera/ZLCustomCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ open class ZLCustomCamera: UIViewController, CAAnimationDelegate {

public lazy var animateLayer = CAShapeLayer()

public lazy var retakeBtn = UIButton(type: .custom)
public lazy var retakeBtn = ZLEnlargeButton(type: .custom)

public lazy var doneBtn = UIButton(type: .custom)

public lazy var dismissBtn = UIButton(type: .custom)
public lazy var dismissBtn = ZLEnlargeButton(type: .custom)

public lazy var switchCameraBtn = UIButton(type: .custom)
public lazy var switchCameraBtn = ZLEnlargeButton(type: .custom)

public lazy var focusCursorView = UIImageView(image: getImage("zl_focus"))

Expand Down Expand Up @@ -300,7 +300,7 @@ open class ZLCustomCamera: UIViewController, CAAnimationDelegate {
dismissBtn.setImage(getImage("zl_arrow_down"), for: .normal)
dismissBtn.addTarget(self, action: #selector(dismissBtnClick), for: .touchUpInside)
dismissBtn.adjustsImageWhenHighlighted = false
dismissBtn.zl_enlargeValidTouchArea(inset: 30)
dismissBtn.enlargeInset = 30
bottomView.addSubview(self.dismissBtn)

largeCircleView.layer.masksToBounds = true
Expand Down Expand Up @@ -352,14 +352,14 @@ open class ZLCustomCamera: UIViewController, CAAnimationDelegate {
retakeBtn.addTarget(self, action: #selector(retakeBtnClick), for: .touchUpInside)
retakeBtn.isHidden = true
retakeBtn.adjustsImageWhenHighlighted = false
retakeBtn.zl_enlargeValidTouchArea(inset: 30)
retakeBtn.enlargeInset = 30
view.addSubview(retakeBtn)

let cameraCount = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: .video, position: .unspecified).devices.count
switchCameraBtn.setImage(getImage("zl_toggle_camera"), for: .normal)
switchCameraBtn.addTarget(self, action: #selector(switchCameraBtnClick), for: .touchUpInside)
switchCameraBtn.adjustsImageWhenHighlighted = false
switchCameraBtn.zl_enlargeValidTouchArea(inset: 30)
switchCameraBtn.enlargeInset = 30
switchCameraBtn.isHidden = cameraCount <= 1
view.addSubview(switchCameraBtn)

Expand Down
16 changes: 8 additions & 8 deletions Sources/Edit/ZLClipImageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ class ZLClipImageViewController: UIViewController {

lazy var bottomToolLineView = UIView()

lazy var cancelBtn = UIButton(type: .custom)
lazy var cancelBtn = ZLEnlargeButton(type: .custom)

lazy var revertBtn = UIButton(type: .custom)
lazy var revertBtn = ZLEnlargeButton(type: .custom)

lazy var doneBtn = UIButton(type: .custom)
lazy var doneBtn = ZLEnlargeButton(type: .custom)

lazy var rotateBtn = UIButton(type: .custom)
lazy var rotateBtn = ZLEnlargeButton(type: .custom)

lazy var clipRatioColView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
Expand Down Expand Up @@ -318,26 +318,26 @@ class ZLClipImageViewController: UIViewController {

cancelBtn.setImage(getImage("zl_close"), for: .normal)
cancelBtn.adjustsImageWhenHighlighted = false
cancelBtn.zl_enlargeValidTouchArea(inset: 20)
cancelBtn.enlargeInset = 20
cancelBtn.addTarget(self, action: #selector(cancelBtnClick), for: .touchUpInside)
bottomToolView.addSubview(cancelBtn)

revertBtn.setTitleColor(.white, for: .normal)
revertBtn.setTitle(localLanguageTextValue(.revert), for: .normal)
revertBtn.zl_enlargeValidTouchArea(inset: 20)
revertBtn.enlargeInset = 20
revertBtn.titleLabel?.font = ZLLayout.bottomToolTitleFont
revertBtn.addTarget(self, action: #selector(revertBtnClick), for: .touchUpInside)
bottomToolView.addSubview(revertBtn)

doneBtn.setImage(getImage("zl_right"), for: .normal)
doneBtn.adjustsImageWhenHighlighted = false
doneBtn.zl_enlargeValidTouchArea(inset: 20)
doneBtn.enlargeInset = 20
doneBtn.addTarget(self, action: #selector(doneBtnClick), for: .touchUpInside)
bottomToolView.addSubview(doneBtn)

rotateBtn.setImage(getImage("zl_rotateimage"), for: .normal)
rotateBtn.adjustsImageWhenHighlighted = false
rotateBtn.zl_enlargeValidTouchArea(inset: 20)
rotateBtn.enlargeInset = 20
rotateBtn.addTarget(self, action: #selector(rotateBtnClick), for: .touchUpInside)
view.addSubview(rotateBtn)

Expand Down
4 changes: 2 additions & 2 deletions Sources/Edit/ZLEditImageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ open class ZLEditImageViewController: UIViewController {

@objc public var ashbinNormalBgColor = zlRGB(40, 40, 40).withAlphaComponent(0.8)

@objc public lazy var cancelBtn = UIButton(type: .custom)
@objc public lazy var cancelBtn = ZLEnlargeButton(type: .custom)

@objc public lazy var mainScrollView = UIScrollView()

Expand Down Expand Up @@ -470,7 +470,7 @@ open class ZLEditImageViewController: UIViewController {
self.cancelBtn.setImage(getImage("zl_retake"), for: .normal)
self.cancelBtn.addTarget(self, action: #selector(cancelBtnClick), for: .touchUpInside)
self.cancelBtn.adjustsImageWhenHighlighted = false
self.cancelBtn.zl_enlargeValidTouchArea(inset: 30)
self.cancelBtn.enlargeInset = 30
self.topShadowView.addSubview(self.cancelBtn)

self.view.addSubview(self.bottomShadowView)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//
// UIControl+ZLPhotoBrowser.swift
// ZLEnlargeButton.swift
// ZLPhotoBrowser
//
// Created by long on 2020/8/17.
// Created by long on 2022/4/24.
//
// Copyright (c) 2020 Long Zhang <[email protected]>
//
Expand All @@ -26,55 +26,43 @@

import UIKit

private var edgeKey = "edgeKey"
public class ZLEnlargeButton: UIButton {

extension UIControl {
/// 扩大点击区域
public var enlargeInsets: UIEdgeInsets = .zero

private var zl_insets: UIEdgeInsets? {
get {
if let temp = objc_getAssociatedObject(self, &edgeKey) as? UIEdgeInsets {
return temp
}
return nil
}
set {
objc_setAssociatedObject(self, &edgeKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
/// 上下左右均扩大该值的点击范围
public var enlargeInset: CGFloat = 0 {
didSet {
let inset = max(0, enlargeInset)
enlargeInsets = UIEdgeInsets(top: inset, left: inset, bottom: inset, right: inset)
}
}


open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
guard !self.isHidden && self.alpha != 0 else {
public override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
guard !isHidden && alpha != 0 else {
return false
}

let rect = self.enlargeRect()

if rect.equalTo(self.bounds) {
let rect = enlargeRect()
if rect.equalTo(bounds) {
return super.point(inside: point, with: event)
}
return rect.contains(point) ? true : false
}

private func enlargeRect() -> CGRect {
guard let edge = self.zl_insets else {
return self.bounds
guard enlargeInsets != .zero else {
return bounds
}

let rect = CGRect(x: self.bounds.minX - edge.left, y: self.bounds.minY - edge.top, width: self.bounds.width + edge.left + edge.right, height: self.bounds.height + edge.top + edge.bottom)

let rect = CGRect(
x: bounds.minX - enlargeInsets.left,
y: bounds.minY - enlargeInsets.top,
width: bounds.width + enlargeInsets.left + enlargeInsets.right,
height: bounds.height + enlargeInsets.top + enlargeInsets.bottom
)
return rect
}

func zl_enlargeValidTouchArea(insets: UIEdgeInsets) {
self.zl_insets = insets
}

func zl_enlargeValidTouchArea(inset: CGFloat) {
guard inset != 0 else {
return
}
self.zl_insets = UIEdgeInsets(top: inset, left: inset, bottom: inset, right: inset)
}


}
6 changes: 3 additions & 3 deletions Sources/General/ZLImagePreviewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class ZLImagePreviewController: UIViewController {

var indexLabel: UILabel!

var selectBtn: UIButton!
var selectBtn: ZLEnlargeButton!

var bottomView: UIView!

Expand Down Expand Up @@ -218,10 +218,10 @@ public class ZLImagePreviewController: UIViewController {
self.indexLabel.textAlignment = .center
self.navView.addSubview(self.indexLabel)

self.selectBtn = UIButton(type: .custom)
self.selectBtn = ZLEnlargeButton(type: .custom)
self.selectBtn.setImage(getImage("zl_btn_circle"), for: .normal)
self.selectBtn.setImage(getImage("zl_btn_selected"), for: .selected)
self.selectBtn.zl_enlargeValidTouchArea(inset: 10)
self.selectBtn.enlargeInset = 10
self.selectBtn.addTarget(self, action: #selector(selectBtnClick), for: .touchUpInside)
self.navView.addSubview(self.selectBtn)

Expand Down
6 changes: 3 additions & 3 deletions Sources/General/ZLPhotoPreviewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ZLPhotoPreviewController: UIViewController {

var backBtn: UIButton!

var selectBtn: UIButton!
var selectBtn: ZLEnlargeButton!

var indexLabel: UILabel!

Expand Down Expand Up @@ -233,10 +233,10 @@ class ZLPhotoPreviewController: UIViewController {
self.backBtn.addTarget(self, action: #selector(backBtnClick), for: .touchUpInside)
self.navView.addSubview(self.backBtn)

self.selectBtn = UIButton(type: .custom)
self.selectBtn = ZLEnlargeButton(type: .custom)
self.selectBtn.setImage(getImage("zl_btn_circle"), for: .normal)
self.selectBtn.setImage(getImage("zl_btn_selected"), for: .selected)
self.selectBtn.zl_enlargeValidTouchArea(inset: 10)
self.selectBtn.enlargeInset = 10
self.selectBtn.addTarget(self, action: #selector(selectBtnClick), for: .touchUpInside)
self.navView.addSubview(self.selectBtn)

Expand Down
2 changes: 1 addition & 1 deletion Sources/General/ZLPhotoUIConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class ZLPhotoUIConfiguration: NSObject {
@objc public var statusBarStyle: UIStatusBarStyle = .lightContent

/// text: Cancel. image: 'x'. Default to image.
@objc public var navCancelButtonStyle: ZLPhotoUIConfiguration.CancelButtonStyle = .text
@objc public var navCancelButtonStyle: ZLPhotoUIConfiguration.CancelButtonStyle = .image

/// Whether to show the status bar when previewing photos. Defaults to false.
@objc public var showStatusBarInPreviewInterface = false
Expand Down
6 changes: 3 additions & 3 deletions Sources/General/ZLThumbnailPhotoCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ZLThumbnailPhotoCell: UICollectionViewCell {

var imageView: UIImageView!

var btnSelect: UIButton!
var btnSelect: ZLEnlargeButton!

var bottomShadowView: UIImageView!

Expand Down Expand Up @@ -95,11 +95,11 @@ class ZLThumbnailPhotoCell: UICollectionViewCell {
self.coverView.isHidden = true
self.contentView.addSubview(self.coverView)

self.btnSelect = UIButton(type: .custom)
self.btnSelect = ZLEnlargeButton(type: .custom)
self.btnSelect.setBackgroundImage(getImage("zl_btn_unselected"), for: .normal)
self.btnSelect.setBackgroundImage(getImage("zl_btn_selected"), for: .selected)
self.btnSelect.addTarget(self, action: #selector(btnSelectClick), for: .touchUpInside)
self.btnSelect.zl_enlargeValidTouchArea(insets: UIEdgeInsets(top: 5, left: 20, bottom: 20, right: 5))
self.btnSelect.enlargeInsets = UIEdgeInsets(top: 5, left: 20, bottom: 20, right: 5)
self.contentView.addSubview(self.btnSelect)

self.indexLabel = UILabel()
Expand Down
8 changes: 4 additions & 4 deletions ZLPhotoBrowser.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
E4C911DE24E2AA950061DA40 /* ZLAlbumListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4C911DD24E2AA950061DA40 /* ZLAlbumListModel.swift */; platformFilter = ios; };
E4C911E024E2ACE20061DA40 /* ZLPhotoManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4C911DF24E2ACE20061DA40 /* ZLPhotoManager.swift */; platformFilter = ios; };
E4CF57EC24EA63BA00BEBFC6 /* ZLProgressHUD.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4CF57EB24EA63BA00BEBFC6 /* ZLProgressHUD.swift */; };
E4CF57EE24EA73EF00BEBFC6 /* UIControl+ZLPhotoBrowser.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4CF57ED24EA73EF00BEBFC6 /* UIControl+ZLPhotoBrowser.swift */; };
E4CF57F024EA923C00BEBFC6 /* ZLLanguageDefine.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4CF57EF24EA923C00BEBFC6 /* ZLLanguageDefine.swift */; };
E4D046F02500A341000BAEC2 /* ZLPhotoPreviewAnimatedTransition.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4D046EF2500A341000BAEC2 /* ZLPhotoPreviewAnimatedTransition.swift */; };
E4D046F22500A361000BAEC2 /* ZLPhotoPreviewPopInteractiveTransition.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4D046F12500A361000BAEC2 /* ZLPhotoPreviewPopInteractiveTransition.swift */; };
Expand All @@ -59,6 +58,7 @@
FD047629276C7C1C00A93FA3 /* ZLAdjustSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD047628276C7C1C00A93FA3 /* ZLAdjustSlider.swift */; };
FD1C586B277AB8790037A746 /* UIViewController+ZLPhotoBrowser.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD1C586A277AB8790037A746 /* UIViewController+ZLPhotoBrowser.swift */; };
FD21E4E1272FFE8E00D1D168 /* ZLPhotoConfiguration+Chaining.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD21E4E0272FFE8E00D1D168 /* ZLPhotoConfiguration+Chaining.swift */; };
FD5230B5281562DD0034B782 /* ZLEnlargeButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD5230B4281562DD0034B782 /* ZLEnlargeButton.swift */; };
FDB34114280D0D70008F20B3 /* ZLPhotoUIConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDB34113280D0D70008F20B3 /* ZLPhotoUIConfiguration.swift */; };
FDB34268280E6566008F20B3 /* ZLPhotoUIConfiguration+Chaining.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDB34267280E6566008F20B3 /* ZLPhotoUIConfiguration+Chaining.swift */; };
FDC6B5FD273B6E1C00973E43 /* ZLCameraConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDC6B5FC273B6E1C00973E43 /* ZLCameraConfiguration.swift */; };
Expand Down Expand Up @@ -108,7 +108,6 @@
E4C911DD24E2AA950061DA40 /* ZLAlbumListModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZLAlbumListModel.swift; sourceTree = "<group>"; };
E4C911DF24E2ACE20061DA40 /* ZLPhotoManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZLPhotoManager.swift; sourceTree = "<group>"; };
E4CF57EB24EA63BA00BEBFC6 /* ZLProgressHUD.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZLProgressHUD.swift; sourceTree = "<group>"; };
E4CF57ED24EA73EF00BEBFC6 /* UIControl+ZLPhotoBrowser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIControl+ZLPhotoBrowser.swift"; sourceTree = "<group>"; };
E4CF57EF24EA923C00BEBFC6 /* ZLLanguageDefine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZLLanguageDefine.swift; sourceTree = "<group>"; };
E4D046EF2500A341000BAEC2 /* ZLPhotoPreviewAnimatedTransition.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZLPhotoPreviewAnimatedTransition.swift; sourceTree = "<group>"; };
E4D046F12500A361000BAEC2 /* ZLPhotoPreviewPopInteractiveTransition.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZLPhotoPreviewPopInteractiveTransition.swift; sourceTree = "<group>"; };
Expand All @@ -119,6 +118,7 @@
FD047628276C7C1C00A93FA3 /* ZLAdjustSlider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZLAdjustSlider.swift; sourceTree = "<group>"; };
FD1C586A277AB8790037A746 /* UIViewController+ZLPhotoBrowser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+ZLPhotoBrowser.swift"; sourceTree = "<group>"; };
FD21E4E0272FFE8E00D1D168 /* ZLPhotoConfiguration+Chaining.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ZLPhotoConfiguration+Chaining.swift"; sourceTree = "<group>"; };
FD5230B4281562DD0034B782 /* ZLEnlargeButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZLEnlargeButton.swift; sourceTree = "<group>"; };
FDB34113280D0D70008F20B3 /* ZLPhotoUIConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZLPhotoUIConfiguration.swift; sourceTree = "<group>"; };
FDB34267280E6566008F20B3 /* ZLPhotoUIConfiguration+Chaining.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ZLPhotoUIConfiguration+Chaining.swift"; sourceTree = "<group>"; };
FDC6B5FC273B6E1C00973E43 /* ZLCameraConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZLCameraConfiguration.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -189,7 +189,6 @@
children = (
E49BD1FA24E3D515005D7DFB /* Bundle+ZLPhotoBrowser.swift */,
E492ABEA24E53575005E1BD5 /* Cell+ZLPhotoBrowser.swift */,
E4CF57ED24EA73EF00BEBFC6 /* UIControl+ZLPhotoBrowser.swift */,
E462466124EBF06F00EF6C57 /* String+ZLPhotoBrowser.swift */,
E462466324EBF36F00EF6C57 /* UIColor+ZLPhotoBrowser.swift */,
E43EFA0B24F13747007067EC /* UIImage+ZLPhotoBrowser.swift */,
Expand Down Expand Up @@ -234,6 +233,7 @@
E4CF57EB24EA63BA00BEBFC6 /* ZLProgressHUD.swift */,
E40ECF4724FFB8BB00A4D923 /* ZLPhotoBrowser.swift */,
2CC461BE2551214600BF96E8 /* ZLAddPhotoCell.swift */,
FD5230B4281562DD0034B782 /* ZLEnlargeButton.swift */,
);
path = General;
sourceTree = "<group>";
Expand Down Expand Up @@ -344,6 +344,7 @@
files = (
E4A9CE012505DB91003201C1 /* ZLEmbedAlbumListView.swift in Sources */,
E48E52D52507297500619AED /* ZLClipImageDismissAnimatedTransition.swift in Sources */,
FD5230B5281562DD0034B782 /* ZLEnlargeButton.swift in Sources */,
E48CE1C525F89EC900D31452 /* ZLWeakProxy.swift in Sources */,
E4C911D224E2884E0061DA40 /* ZLPhotoPreviewSheet.swift in Sources */,
E46EA7D824F79F1C00033853 /* ZLClipImageViewController.swift in Sources */,
Expand Down Expand Up @@ -372,7 +373,6 @@
E4C911D424E288780061DA40 /* ZLPhotoConfiguration.swift in Sources */,
E410177925305C73004C4952 /* ZLFilter.swift in Sources */,
E492ABEB24E53575005E1BD5 /* Cell+ZLPhotoBrowser.swift in Sources */,
E4CF57EE24EA73EF00BEBFC6 /* UIControl+ZLPhotoBrowser.swift in Sources */,
E49BD1FB24E3D515005D7DFB /* Bundle+ZLPhotoBrowser.swift in Sources */,
E4D046F02500A341000BAEC2 /* ZLPhotoPreviewAnimatedTransition.swift in Sources */,
FDB34268280E6566008F20B3 /* ZLPhotoUIConfiguration+Chaining.swift in Sources */,
Expand Down

0 comments on commit fca9302

Please sign in to comment.