最近在研究K线,以及画线工具,当手指触摸屏幕的时候,指尖的屏幕处看不到,被指尖挡住了,这样我拖动画线的锚点的时候,就不是很灵活准确。为此,做了个预览的功能。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: self)
let scale = UIScreen.main.scale
let rect = CGRect(x: location.x - 50, y: location.y - 50, width: 100, height: 100)
UIGraphicsBeginImageContextWithOptions(rect.size, false, scale)
self.drawHierarchy(in: CGRect(origin: CGPoint(x: -rect.origin.x, y: -rect.origin.y), size: self.bounds.size), afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let imageView = UIImageView(image: image)
imageView.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
imageView.contentMode = .scaleAspectFit
let popup = UIView(frame: UIScreen.main.bounds)
popup.backgroundColor = UIColor(white: 0, alpha: 0.5)
popup.addSubview(imageView)
self.view.addSubview(popup)
}
}
效果图

网友评论