1

ここで SO と GitHub を検索すると、次の Swift クラスが見つかりました。これは拡大鏡として機能しUIView、ユーザーが画面に触れたポイント周辺の一部を拡大します。これが私が見つけたコードです:

//
//  MagnifyingGlassView.swift
//
//  Created by Roman Kyrylenko on 01/11/15.
//  Copyright (c) 2015 pr0ctopus.com All rights reserved.
//

import UIKit

public class MagnifyingGlassView: UIView {

  var viewToMagnify: UIView?
  var scale: CGFloat = 2.0
  var zoomedPoint:CGPoint?

  var size: CGFloat = 200.0 {
    didSet {
        let c = center
        frame = CGRectMake(c.x - size / 2, c.y - size / 2, size, size)
    }
  }

  var outlineColor: UIColor? {
    didSet {
        layer.borderColor = outlineColor?.CGColor
    }
  }

  var outlineWidth: CGFloat? {
    didSet {
        layer.borderWidth = outlineWidth ?? 0.0
    }
  }

  var touchPoint: CGPoint = CGPointZero {
    didSet {
        self.center = touchPoint
    }
  }

  required public init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    commonInit()
  }

  /*required public override init(frame: CGRect) {
    super.init(frame: frame)
    commonInit()
  }*/

  public init() {
    super.init(frame: CGRectMake(0, 0, size, size))
    commonInit()
  }

  public override func drawRect(rect: CGRect) {
    guard let context = UIGraphicsGetCurrentContext()
        else { return }

    /------!!!------/
    CGContextTranslateCTM(context, frame.size.width / 2, frame.size.height / 2)
    CGContextScaleCTM(context, scale, scale)
    CGContextTranslateCTM(context, -zoomedPoint!.x, -zoomedPoint!.y)
    /------!!!------/

    hidden = true
    viewToMagnify?.layer.renderInContext(context)

    /* color of axes */
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0)
    /* line width of axes */
    CGContextSetLineWidth(context, 0.5)

    /* draw vertical axis inside magnifier */
    CGContextMoveToPoint(context, self.zoomedPoint!.x , self.zoomedPoint!.y - (self.frame.size.height / 2))
    CGContextAddLineToPoint(context, self.zoomedPoint!.x, self.zoomedPoint!.y + (self.frame.size.height / 2))

    /* draw horizontal axis inside magnifier */
    CGContextMoveToPoint(context, self.zoomedPoint!.x - (self.frame.size.width / 2), self.zoomedPoint!.y)
    CGContextAddLineToPoint(context, self.zoomedPoint!.x + (self.frame.size.width / 2), self.zoomedPoint!.y)
    CGContextStrokePath(context)
    hidden = false

}

private func commonInit() {
    layer.cornerRadius = frame.size.width / 2
    self.outlineWidth = 2.0
    self.outlineColor = UIColor.lightGrayColor()
    self.size = 200.0
    layer.masksToBounds = true
}
}

私が理解していない行は、の間に含まれている行/------!!!------/です。つまり、拡大鏡の目的がポイントの周りのゾーンを拡大することだけであるのに、なぜこれらの翻訳が必要なのか理解できません。なぜ2回翻訳する必要があるのですか?彼らはどういう意味ですか?それがどのように機能するかを正確に説明してくれる人はいますか?

私が今までに理解したと思うことは、最初の翻訳では、拡大鏡に、原点が拡大鏡の中心であり、軸の長さが拡大鏡の高さの半分と幅の半分であるグラフィック座標の新しい参照システムを与えることです。自体。次に、拡大縮小操作を実行してそのグラフィック コンテキストを 2 倍にズームし、最後に別の変換を使用して、拡大鏡のグラフィック座標の参照系の原点を、ユーザーが画面に触れたポイントとして再度設定します。これは正しいですか?

4

0 に答える 0