override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
swiped = true
if let touch = touches.first{
let currentPoint = touch.location(in: self.view)
drawLines(fromPoint: lastPoint, toPoint: currentPoint)
lastPoint = currentPoint
segmentTop = touch.location(in: self.view)
segmentBottom = touch.location(in: self.view)
segmentLeft = touch.location(in: self.view)
segmentRight = touch.location(in: self.view)
}
}
我目前正在开展一个项目,我的目标是在图纸周围裁剪一个细分。为了实现这一点,我必须检查触摸位置,以便我可以输出触摸最接近每个边缘的位置。例如,片段的顶部将是触摸位置最接近顶边的位置,这是我想要输出的,但是我没有任何基础。这有点像在游戏中获得高分,而在这里你根本没有以高分为基础。
答案 0 :(得分:0)
您的问题并不完全清楚,但我认为您正在寻找一种方法来跟踪最后和最大X以及最后一个触摸点的最小和最大Y.
一种解决方案是这样的:
声明属性以跟踪当前的最小和最大X和Y值:
@Many2Many(other = Course.class, join = "registrations", sourceFKName = "student_uid", targetFKName = "course_uid")
public class Student extends Model {
}
@Many2Many(other = Student.class, join = "registrations", sourceFKName = "course_uid", targetFKName = "student_uid")
public class Course extends Model {
}
然后对每个触点进行比较:
var minX: CGFloat?
var maxX: CGFloat?
var minY: CGFloat?
var maxY: CGFloat?
对其他三个值执行类似的计算。
一个更简单但类似的解决方案是从初始值开始并避免使用选项:
if let minX = minX {
// See if this new point is less than the current min
if lastPoint.x < minX {
self.minX = lastPoint.x
}
} else {
// No current minX so use this value
self.minX = lastPoint.x
}
然后每张支票变为:
var minX = CGFloat.greatestFiniteMagnitude
var maxX = CGFloat.leastNormalMagnitude
var minY = CGFloat.greatestFiniteMagnitude
var maxY = CGFloat.leastNormalMagnitude
重复Y值。
答案 1 :(得分:0)
试试这个
segmentTop = max(segmentTop, touch.location(in: self.view).y)
segmentBottom = min(segmentBottom, touch.location(in: self.view).y)
segmentLeft = min(segmentLeft, touch.location(in: self.view).x)
segmentRight = max(segmentRight, touch.location(in: self.view).x)
确保segmentTop,segmentBottom,segmentLeft和segmentRight最初设置为0