From 79611ba86424410d056f859deab2b0267481b73e Mon Sep 17 00:00:00 2001 From: longitachi Date: Fri, 22 Mar 2024 14:52:41 +0800 Subject: [PATCH] Adapt the text sticker input interface for iPad landscape mode. --- .../General/ZLClipImageViewController.swift | 4 +- .../General/ZLEditImageViewController.swift | 4 +- Sources/General/ZLImageEditorDefine.swift | 8 ++++ .../General/ZLInputTextViewController.swift | 46 +++++++++++++++---- 4 files changed, 49 insertions(+), 13 deletions(-) diff --git a/Sources/General/ZLClipImageViewController.swift b/Sources/General/ZLClipImageViewController.swift index 78b3e26..98a841d 100644 --- a/Sources/General/ZLClipImageViewController.swift +++ b/Sources/General/ZLClipImageViewController.swift @@ -224,9 +224,7 @@ class ZLClipImageViewController: UIViewController { override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() - guard shouldLayout else { - return - } + guard shouldLayout else { return } shouldLayout = false scrollView.frame = view.bounds diff --git a/Sources/General/ZLEditImageViewController.swift b/Sources/General/ZLEditImageViewController.swift index 316cdb6..6eff081 100644 --- a/Sources/General/ZLEditImageViewController.swift +++ b/Sources/General/ZLEditImageViewController.swift @@ -364,7 +364,9 @@ open class ZLEditImageViewController: UIViewController { open override var prefersHomeIndicatorAutoHidden: Bool { true } - override open var supportedInterfaceOrientations: UIInterfaceOrientationMask { .portrait } + override open var supportedInterfaceOrientations: UIInterfaceOrientationMask { + deviceIsiPhone() ? .portrait : .all + } deinit { cleanToolViewStateTimer() diff --git a/Sources/General/ZLImageEditorDefine.swift b/Sources/General/ZLImageEditorDefine.swift index ba48e65..0e1adab 100644 --- a/Sources/General/ZLImageEditorDefine.swift +++ b/Sources/General/ZLImageEditorDefine.swift @@ -34,6 +34,14 @@ struct ZLImageEditorLayout { static let bottomToolBtnCornerRadius: CGFloat = 5 } +func deviceIsiPhone() -> Bool { + return UIDevice.current.userInterfaceIdiom == .phone +} + +func deviceIsiPad() -> Bool { + return UIDevice.current.userInterfaceIdiom == .pad +} + func deviceSafeAreaInsets() -> UIEdgeInsets { var insets: UIEdgeInsets = .zero diff --git a/Sources/General/ZLInputTextViewController.swift b/Sources/General/ZLInputTextViewController.swift index 6fc994c..792cb8f 100644 --- a/Sources/General/ZLInputTextViewController.swift +++ b/Sources/General/ZLInputTextViewController.swift @@ -43,6 +43,19 @@ class ZLInputTextViewController: UIViewController { private var textStyle: ZLInputTextStyle + private lazy var bgImageView: UIImageView = { + let view = UIImageView(image: image?.zl.blurImage(level: 4)) + view.contentMode = .scaleAspectFit + return view + }() + + private lazy var coverView: UIView = { + let view = UIView() + view.backgroundColor = .black + view.alpha = 0.4 + return view + }() + private lazy var cancelBtn: UIButton = { let btn = UIButton(type: .custom) btn.setTitle(localLanguageTextValue(.cancel), for: .normal) @@ -64,8 +77,7 @@ class ZLInputTextViewController: UIViewController { }() private lazy var textView: UITextView = { - let y = max(deviceSafeAreaInsets().top, 20) + 20 + ZLImageEditorLayout.bottomToolBtnH + 12 - let textView = UITextView(frame: CGRect(x: 10, y: y, width: view.zl.width - 20, height: 200)) + let textView = UITextView() textView.keyboardAppearance = .dark textView.returnKeyType = .done textView.delegate = self @@ -114,6 +126,8 @@ class ZLInputTextViewController: UIViewController { return collectionView }() + private var shouldLayout = true + private lazy var textLayer = CAShapeLayer() private let textLayerRadius: CGFloat = 10 @@ -172,6 +186,20 @@ class ZLInputTextViewController: UIViewController { override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() + guard shouldLayout else { return } + + shouldLayout = false + bgImageView.frame = view.bounds + + // iPad图片由竖屏切换到横屏时候填充方式会有点异常,这里重置下 + if deviceIsiPad() { + if UIApplication.shared.statusBarOrientation.isLandscape { + bgImageView.contentMode = .scaleAspectFill + } else { + bgImageView.contentMode = .scaleAspectFit + } + } + let btnY = max(deviceSafeAreaInsets().top, 20) + 20 let cancelBtnW = localLanguageTextValue(.cancel).zl.boundingRect(font: ZLImageEditorLayout.bottomToolTitleFont, limitSize: CGSize(width: .greatestFiniteMagnitude, height: ZLImageEditorLayout.bottomToolBtnH)).width + 20 cancelBtn.frame = CGRect(x: 15, y: btnY, width: cancelBtnW, height: ZLImageEditorLayout.bottomToolBtnH) @@ -179,6 +207,8 @@ class ZLInputTextViewController: UIViewController { let doneBtnW = localLanguageTextValue(.done).zl.boundingRect(font: ZLImageEditorLayout.bottomToolTitleFont, limitSize: CGSize(width: .greatestFiniteMagnitude, height: ZLImageEditorLayout.bottomToolBtnH)).width + 20 doneBtn.frame = CGRect(x: view.zl.width - 20 - doneBtnW, y: btnY, width: doneBtnW, height: ZLImageEditorLayout.bottomToolBtnH) + textView.frame = CGRect(x: 10, y: doneBtn.zl.bottom + 30, width: view.zl.width - 20, height: 200) + textStyleBtn.frame = CGRect( x: 12, y: 0, @@ -197,17 +227,15 @@ class ZLInputTextViewController: UIViewController { } } + override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { + super.viewWillTransition(to: size, with: coordinator) + shouldLayout = true + } + func setupUI() { view.backgroundColor = .black - let bgImageView = UIImageView(image: image?.zl.blurImage(level: 4)) - bgImageView.frame = view.bounds - bgImageView.contentMode = .scaleAspectFit view.addSubview(bgImageView) - - let coverView = UIView(frame: bgImageView.bounds) - coverView.backgroundColor = .black - coverView.alpha = 0.4 bgImageView.addSubview(coverView) view.addSubview(cancelBtn)