Skip to content

Commit

Permalink
fix: fix reset issues
Browse files Browse the repository at this point in the history
fix reset rotation dial issues
fix reset crop mask view issues
fix rotation device issues
  • Loading branch information
Yingtao Guo committed Jan 24, 2023
1 parent 2647f28 commit b10d031
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 31 deletions.
4 changes: 0 additions & 4 deletions Sources/Mantis/CropView/CropMaskViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ extension CropMaskViewManager: CropMaskViewManagerProtocol {
maskViews.forEach { $0.removeFromSuperview() }
}

func bringMaskViewsToFront() {
maskViews.forEach { $0.bringSelfToFront() }
}

func showDimmingBackground(animated: Bool) {
if animated {
UIView.animate(withDuration: 0.1) {
Expand Down
40 changes: 18 additions & 22 deletions Sources/Mantis/CropView/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ class CropView: UIView {
let cropWorkbenchView: CropWorkbenchViewProtocol
let cropMaskViewManager: CropMaskViewManagerProtocol

var rotationDial: RotationDialProtocol?
var rotationDial: RotationDialProtocol? {
didSet {
guard let rotationDial = rotationDial else {
return
}
addSubview(rotationDial)
}
}

var manualZoomed = false
var forceFixedRatio = false
Expand Down Expand Up @@ -107,7 +114,7 @@ class CropView: UIView {
self?.handleCropBoxFrameChange(cropBoxFrame)
}

initalRender()
viewModel.setInitialStatus()
}

required init?(coder aDecoder: NSCoder) {
Expand All @@ -125,7 +132,7 @@ class CropView: UIView {
cropMaskViewManager.adaptMaskTo(match: cropBoxFrame, cropRatio: cropRatio)
}

private func initalRender() {
private func initialRender() {
setupCropWorkbenchView()
setupCropAuxiliaryIndicatorView()
checkImageStatusChanged()
Expand All @@ -136,7 +143,7 @@ class CropView: UIView {

switch viewStatus {
case .initial:
initalRender()
initialRender()
case .rotating(let angle):
viewModel.degrees = angle.degrees
rotateCropWorkbenchView()
Expand Down Expand Up @@ -195,13 +202,13 @@ class CropView: UIView {
}

func resetComponents() {
cropMaskViewManager.removeMaskViews()
cropMaskViewManager.setup(in: self, cropRatio: CGFloat(getImageHorizontalToVerticalRatio()))

viewModel.resetCropFrame(by: getInitialCropBoxRect())
cropWorkbenchView.resetImageContent(by: viewModel.cropBoxFrame)
cropAuxiliaryIndicatorView.bringSelfToFront()

setupAngleDashboard()
setupRotationDial()

if aspectRatioLockEnabled {
setFixedRatioCropBox()
Expand Down Expand Up @@ -231,22 +238,20 @@ class CropView: UIView {
addSubview(cropAuxiliaryIndicatorView)
}

private func setupAngleDashboard() {
private func setupRotationDial() {
guard cropViewConfig.showRotationDial, let rotationDial = rotationDial else {
return
}

rotationDial.removeFromSuperview()
rotationDial.reset()

let boardLength = min(bounds.width, bounds.height) * 0.6
let dialFrame = CGRect(x: 0,
y: 0,
width: boardLength,
height: angleDashboardHeight)
rotationDial.setup(with: dialFrame)

rotationDial.isUserInteractionEnabled = true
addSubview(rotationDial)

rotationDial.setRotationCenter(by: cropAuxiliaryIndicatorView.center, of: self)

Expand All @@ -259,6 +264,8 @@ class CropView: UIView {
}

rotationDial.rotateDialPlate(by: CGAngle(radians: viewModel.radians))
rotationDial.bringSelfToFront()

adaptAngleDashboardToCropBox()
}

Expand Down Expand Up @@ -750,7 +757,6 @@ extension CropView: CropViewProtocol {
cropWorkbenchView.transform = CGAffineTransform(scaleX: 1, y: 1)
cropWorkbenchView.reset(by: viewModel.cropBoxFrame)

setupAngleDashboard()
rotateCropWorkbenchView()

if viewModel.cropRightBottomOnImage != .zero {
Expand All @@ -770,7 +776,6 @@ extension CropView: CropViewProtocol {
let contentRect = getContentBounds()

adjustUIForNewCrop(contentRect: contentRect) { [weak self] in
self?.adaptAngleDashboardToCropBox()
self?.viewModel.setBetweenOperationStatus()
}
}
Expand Down Expand Up @@ -978,16 +983,7 @@ extension CropView: CropViewProtocol {
}

func reset() {
cropWorkbenchView.removeFromSuperview()
cropAuxiliaryIndicatorView.removeFromSuperview()
rotationDial?.removeFromSuperview()

if forceFixedRatio {
aspectRatioLockEnabled = true
} else {
aspectRatioLockEnabled = false
}

aspectRatioLockEnabled = forceFixedRatio
viewModel.reset(forceFixedRatio: forceFixedRatio)

resetComponents()
Expand Down
9 changes: 6 additions & 3 deletions Sources/Mantis/MaskBackground/CropMaskProtocal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import UIKit
private let minOverLayerUnit: CGFloat = 30
private let initialFrameLength: CGFloat = 1000

protocol CropMaskProtocol where Self: UIView {
protocol CropMaskProtocol: UIView {
var cropShapeType: CropShapeType { get set }
var innerLayer: CALayer? { get set }

Expand Down Expand Up @@ -39,7 +39,7 @@ extension CropMaskProtocol {
}

func adaptMaskTo(match cropRect: CGRect, cropRatio: CGFloat) {
let scaleX: CGFloat
var scaleX: CGFloat

switch cropShapeType {
case .roundedRect:
Expand All @@ -50,7 +50,10 @@ extension CropMaskProtocol {
scaleX = cropRect.width / minOverLayerUnit
}

let scaleY = cropRect.height / minOverLayerUnit
var scaleY = cropRect.height / minOverLayerUnit

scaleX = max(scaleX, 0.0001)
scaleY = max(scaleY, 0.0001)

transform = CGAffineTransform(scaleX: scaleX, y: scaleY)

Expand Down
1 change: 0 additions & 1 deletion Sources/Mantis/Protocols/CropMaskViewManagerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import UIKit
protocol CropMaskViewManagerProtocol {
func setup(in view: UIView, cropRatio: CGFloat)
func removeMaskViews()
func bringMaskViewsToFront()
func showDimmingBackground(animated: Bool)
func showVisualEffectBackground(animated: Bool)
func adaptMaskTo(match cropRect: CGRect, cropRatio: CGFloat)
Expand Down
1 change: 1 addition & 0 deletions Sources/Mantis/Protocols/RotationDialProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ protocol RotationDialProtocol: UIView {
func resetAngle(animated: Bool)
func getRotationAngle() -> CGAngle
func setRotationCenter(by point: CGPoint, of view: UIView)
func reset()
}
5 changes: 4 additions & 1 deletion Sources/Mantis/RotationDial/RotationDial.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ final class RotationDial: UIView {
self.dialConfig = dialConfig
self.viewModel = viewModel
super.init(frame: frame)
setup(with: frame)
}

required init?(coder: NSCoder) {
Expand Down Expand Up @@ -246,4 +245,8 @@ extension RotationDial: RotationDialProtocol {
let newPoint = view.convert(point, to: self)
dialConfig.rotationCenterType = .custom(newPoint)
}

func reset() {
dialConfig.rotationCenterType = .useDefault
}
}

0 comments on commit b10d031

Please sign in to comment.