Skip to content

Instantly share code, notes, and snippets.

@victorBaro
Created September 1, 2018 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorBaro/e4df8d2c7359f0753504f8967bce34b0 to your computer and use it in GitHub Desktop.
Save victorBaro/e4df8d2c7359f0753504f8967bce34b0 to your computer and use it in GitHub Desktop.
A magnifying glass effect view to show on top of screen content as seen in this video: https://www.youtube.com/watch?v=MppiS0iD3qY
public class MagnifyingView: UIView {
private weak var viewToMagnify: UIView!
private var touchPoint: CGPoint!
var zoomScale: CGFloat = 2
public required init?(coder aDecoder: NSCoder) { fatalError() }
public init(viewToMagnify: UIView, size: CGSize) {
self.viewToMagnify = viewToMagnify
super.init(frame: CGRect(origin: .zero, size: size))
setup()
}
private func setup() {
self.layer.borderColor = UIColor.lightGray.cgColor
self.layer.borderWidth = 3
self.layer.cornerRadius = bounds.size.height
self.layer.masksToBounds = true
}
public func setTouchPoint(_ point: CGPoint) {
touchPoint = point
self.center = CGPoint(x: point.x, y: point.y)
setNeedsDisplay()
}
public override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return }
context.translateBy(x: self.frame.size.width * 0.5, y: self.frame.size.height * 0.5)
context.scaleBy(x: zoomScale, y: zoomScale)
context.translateBy(x: -touchPoint.x, y: -touchPoint.y)
self.viewToMagnify.layer.render(in: context)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment