Skip to content

Commit

Permalink
Fix word pointer magnifying glass not showing inverted image in dark …
Browse files Browse the repository at this point in the history
…mode
  • Loading branch information
mohamede1945 committed Sep 15, 2024
1 parent 568d197 commit b224ee3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
10 changes: 10 additions & 0 deletions UI/UIx/UIKit/Extensions/UIView+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,13 @@ extension UIView {
)
}
}

extension UIView {
public func snapshot(of area: CGRect? = nil) -> UIImage {
let rect = area ?? bounds
let renderer = UIGraphicsImageRenderer(size: rect.size)
return renderer.image { _ in
drawHierarchy(in: CGRect(origin: CGPoint(x: -rect.origin.x, y: -rect.origin.y), size: bounds.size), afterScreenUpdates: true)
}
}
}
41 changes: 32 additions & 9 deletions UI/UIx/UIKit/Views/MagnifyingGlass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,44 @@ public class MagnifyingGlass: UIView {
}

private class MagnifyingAreaView: UIView {
var viewToMagnify: UIView?
private var scale: CGFloat = 1.5

private let imageView = UIImageView()
private var snapshotImage: UIImage? {
didSet {
imageView.image = snapshotImage
if imageView.superview == nil {
imageView.contentMode = .center
imageView.transform = CGAffineTransform(scaleX: scale, y: scale)
addAutoLayoutSubview(imageView)
imageView.vc.edges()
}
}
}

var scale = CGPoint(x: 1.5, y: 1.5)
var viewToMagnify: UIView?

var touchPoint: CGPoint = .zero {
didSet { setNeedsDisplay() }
didSet {
refreshSnapshot() // Update the snapshot when the touch point changes
}
}

override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else {
private func refreshSnapshot() {
guard let viewToMagnify else {
snapshotImage = nil
return
}
context.translateBy(x: frame.size.width / 2, y: frame.size.height / 2)
context.scaleBy(x: scale.x, y: scale.y) // 1.5 is the zoom scale
context.translateBy(x: -1 * touchPoint.x, y: -1 * touchPoint.y)
viewToMagnify?.layer.render(in: context)

// Define the area to capture around the touch point
let snapshotSize = CGSize(width: bounds.width / scale, height: bounds.height / scale)
let snapshotOrigin = CGPoint(
x: touchPoint.x - snapshotSize.width / 2,
y: touchPoint.y - snapshotSize.height / 2
)
let snapshotRect = CGRect(origin: snapshotOrigin, size: snapshotSize)

// Capture the snapshot of the target area
snapshotImage = viewToMagnify.snapshot(of: snapshotRect)
}
}

0 comments on commit b224ee3

Please sign in to comment.