我有一个快速的模拟时钟。用户可以通过在任意小时或分钟之间点击或滑动来选择时间。我想通过在用户滑动时使光标“跳过”某些值来使时钟上的某些时间“不可选择”。
如何获得用户滑动的方向?我想使用该方向将“跳转”编程到下一个值。
这是模拟时钟视图的Github代码:
只需弄清楚,touchesMoved功能已经跟踪了用户的触摸运动。要跳过一个值,请在touchesMoved函数下添加它:
if roundf(Float(index)) == desiredSkipValue {
index += 1
}
示例用法(跳过2点):
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: timeCircle)
let extraGrip: CGFloat = -25.0
if let last = lastLocation, location.x > extraGrip, location.y > extraGrip {
let rad = radiansBetweenPoint(location, last)
let value = (CGFloat(rad)/CGFloat.pi)*180
let currentAngle: CGFloat = ((radiansBetweenPoint(lineCenter, linePoint!)/CGFloat.pi)*180.0)-90.0
let newValue = value+currentAngle+180.0
var index: CGFloat = (newValue/360.0)*12
if roundf(Float(index)) <= 0 {
index += 12
}
//skip 2 o clock
if roundf(Float(index)) == 2 {
index += 1
}
setLineToPoint(pointForArcByIndex(index: index))
setTimeFromIndex(index)
lastIndex = index
}
}
}
我很欣喜