IPhone, воспроизводит эффект лупы
Я хотел бы иметь возможность создавать подвижную лупу (например, ту, что у вас есть, когда вы копируете и вставляете) в пользовательском режиме, для увеличения части моего представления.
Я не знаю, как начать, у вас есть идеи?
Заранее благодарим за помощь:)
Ответы
Ответ 1
Мы делаем это в кроссвордах. В методе drawRect маскируйте круг (используя монохромное растровое изображение, содержащее "маску" вашего увеличительного стекла) и нарисуйте изображение вашего объекта там с преобразованием масштаба 2x. Затем нарисуйте изображение увеличительного стекла над этим, и все готово.
- (void) drawRect: (CGRect) rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect bounds = self.bounds;
CGImageRef mask = [UIImage imageNamed: @"loupeMask"].CGImage;
UIImage *glass = [UIImage imageNamed: @"loupeImage"];
CGContextSaveGState(context);
CGContextClipToMask(context, bounds, mask);
CGContextFillRect(context, bounds);
CGContextScaleCTM(context, 2.0, 2.0);
//draw your subject view here
CGContextRestoreGState(context);
[glass drawInRect: bounds];
}
Ответ 2
Здесь приведен полный пример . В загруженном проекте есть небольшая ошибка, но в остальном он отлично работает и делает именно то, что вам нужно.
Ответ 3
Я использую этот код в Swift 3:
class MagnifyingGlassView: UIView {
var zoom: CGFloat = 2 {
didSet {
setNeedsDisplay()
}
}
weak var readView: UIView?
// MARK: - UIVIew
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
override func draw(_ rect: CGRect) {
guard let readView = readView else { return }
let magnifiedBounds = magnifyBounds(of: readView, zoom: zoom)
readView.drawHierarchy(in: magnifiedBounds, afterScreenUpdates: false)
}
// MARK: - Private
private func setupView() {
isOpaque = false
backgroundColor = UIColor.clear
}
private func magnifyBounds(of view: UIView, zoom: CGFloat) -> CGRect {
let transform = CGAffineTransform(scaleX: zoom, y: zoom)
var bounds = view.bounds.applying(transform)
bounds.center = view.bounds.center
return view.convert(bounds, to: self)
}
}
extension CGRect {
var center: CGPoint {
get {
return CGPoint(x: origin.x + width / 2, y: origin.y + height / 2)
}
set {
origin.x = newValue.x - width / 2
origin.y = newValue.y - height / 2
}
}
}
Вам нужно позвонить setNeedsDisplay
в scrollViewDidScroll:
, если ваше чтение представляет собой scrollView.