swift - 远离接触点的画线
问题描述
我的快速代码试图画一条线。正如您在下面的 gif 中看到的那样。当用户放置一个触摸点时,您可以将线绘制在离光标稍远的地方。我不知道这里发生了什么。但我会假设任何错误都在 touches started func 中,因为一旦用户触摸图像视图,这个问题就会开始。
import UIKit
class ViewController: UIViewController {
var startPoint: CGPoint?
var statPoint = CGPoint.zero
var swipe = false
var pic = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
pic.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pic)
pic.backgroundColor = .brown
view.backgroundColor = .cyan
pic.frame = CGRect(x: 100, y: 100, width: 250, height: 250)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
return}
swipe = false
statPoint = touch.location(in: view)
}
func drawLine(from fromPoint: CGPoint, to toPoint : CGPoint) {
UIGraphicsBeginImageContext( view.frame.size)
guard let context = UIGraphicsGetCurrentContext() else {
return
}
pic.image?.draw(in: view.bounds)
context.move(to: fromPoint)
context.addLine(to: toPoint)
context.setLineCap(.round)
context.setLineWidth(5)
context.setBlendMode(.normal)
context.setStrokeColor(UIColor.black.cgColor)
context.strokePath()
pic.image = UIGraphicsGetImageFromCurrentImageContext()
pic.alpha = 1
UIGraphicsEndImageContext()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard var touch = touches.first else {
return
}
swipe = true
let currentPoint = touch.location(in: view)
drawLine(from: statPoint, to: currentPoint)
statPoint = currentPoint
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if !swipe {
drawLine(from: statPoint, to: statPoint)
}
UIGraphicsBeginImageContext(view.frame.size)
pic.image?.draw(in: view.bounds, blendMode: .normal, alpha: 1)
pic.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
}
///
解决方案
您的图像视图名气的问题:
请像这样设置并检查:
class ViewController: UIViewController {
var startPoint: CGPoint?
var statPoint = CGPoint.zero
var swipe = false
var pic = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
pic.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pic)
pic.backgroundColor = .brown
view.backgroundColor = .cyan
pic.frame = CGRect(x: 100, y: 100, width: 250, height: 250)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
return}
swipe = false
statPoint = touch.location(in: self.pic)
}
func drawLine(from fromPoint: CGPoint, to toPoint : CGPoint) {
UIGraphicsBeginImageContext( pic.frame.size)
guard let context = UIGraphicsGetCurrentContext() else {
return
}
pic.image?.draw(in: pic.bounds)
context.move(to: fromPoint)
context.addLine(to: toPoint)
context.setLineCap(.round)
context.setLineWidth(5)
context.setBlendMode(.normal)
context.setStrokeColor(UIColor.black.cgColor)
context.strokePath()
pic.image = UIGraphicsGetImageFromCurrentImageContext()
pic.alpha = 1
UIGraphicsEndImageContext()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard var touch = touches.first else {
return
}
swipe = true
let currentPoint = touch.location(in: pic)
drawLine(from: statPoint, to: currentPoint)
statPoint = currentPoint
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if !swipe {
drawLine(from: statPoint, to: statPoint)
}
UIGraphicsBeginImageContext(pic.frame.size)
pic.image?.draw(in: pic.bounds, blendMode: .normal, alpha: 1)
pic.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
}
请检查我更新你的控制器的代码
推荐阅读
- java - 如何检查列表中的所有项目是否包含字符串中的任何字符
- html - 如何使用 React.js 移动材质 ui 标签 RTL
- python - Pygame 横向滚轮
- reactjs - ReactJS 和 Mobx RootStore 以及子商店变得未定义
- jquery - 使用 jQuery 确定任何后续元素是否具有特定的数据属性
- firebase - 在 React Native 中一段时间后删除通知 (@react-native-firebase/messaging)
- sql - 查询 cte - 无法绑定多部分标识符。我该如何解决这个问题,以便我可以查询表?SQL 会计背景的新手
- database - 自带密钥 (BYOK) 和客户管理密钥 (CMK) 加密之间的区别?
- angular - 编辑依赖选择
- python - 如何使用 setuptools 将 python 包安装到子目录中?