我正试图在directions
中找到UIPanGestureRecognizer
我得到了它的一个扩展,其中一个struct
正像这样使用OptionSet
:
extension UIPanGestureRecognizer {
public struct PanGestureDirection: OptionSet {
public let rawValue: UInt8
public init(rawValue: UInt8) {
self.rawValue = rawValue
}
static let Up = PanGestureDirection(rawValue: 1 << 0)
static let Down = PanGestureDirection(rawValue: 1 << 1)
static let Left = PanGestureDirection(rawValue: 1 << 2)
static let Right = PanGestureDirection(rawValue: 1 << 3)
}
private func getDirectionBy(velocity: CGFloat, greater: PanGestureDirection, lower: PanGestureDirection) -> PanGestureDirection {
if velocity == 0 {
return []
}
return velocity > 0 ? greater : lower
}
public func direction(in view: UIView) -> PanGestureDirection {
let velocity = self.velocity(in: view)
let yDirection = getDirectionBy(velocity: velocity.y, greater: PanGestureDirection.Down, lower: PanGestureDirection.Up)
let xDirection = getDirectionBy(velocity: velocity.x, greater: PanGestureDirection.Right, lower: PanGestureDirection.Left)
return xDirection.union(yDirection)
}
}
任何人都可以通过简单的例子来简单地帮助我,例如OptionSet
的确切含义以及我们如何在将来的任务中使用它?
答案 0 :(得分:2)