-
拖拽(UIPanGestureRecognizer)手势代替轻扫(UISwipeGestureRecognizer)手势的简单实现
class AESViewController: UIViewController {
// 自定义属性
var direction: UIPanGestureRecognizerDirection = .undefined
fileprivate lazy var prePoint: CGPoint = .zero
lazy var timer = DispatchSource.makeTimerSource(flags: [], queue:DispatchQueue.main)
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
view.addSubview(imageView)
view.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(AESViewController.handlePanGesture(pan:))))
}
}
// MARK: - 拖拽(UIPanGestureRecognizer)手势代替轻扫(UISwipeGestureRecognizer)手势
enum UIPanGestureRecognizerDirection{
case undefined,up,down,left,right
}
extension AESViewController {
//加载手势
func handlePanGesture(pan: UIPanGestureRecognizer){
switch(pan.state){
case .began:
prePoint = pan.location(in: pan.view!)
case .changed:
let curP = pan.location(in: pan.view!)
let isVerticalGesture = fabs(curP.y) > fabs(prePoint.y) // 上下之分
let isHorizontalGesture = fabs(curP.x) > fabs(prePoint.x) // 左右之分
let isHelpDirection = fabs((curP.y - prePoint.y)/(curP.x - prePoint.x)) <= 1
if isHorizontalGesture && isHelpDirection{
direction = .right
} else if !isHorizontalGesture && isHelpDirection {
direction = .left
} else if isVerticalGesture && !isHelpDirection {
direction = .down
} else if !isVerticalGesture && !isHelpDirection{
direction = .up
}else {
direction = .undefined
}
break
case .ended:
switch direction {
case .up:
handleUpwardsGesture()
case .down:
handleDownwardsGesture()
case .right:
handleRightwardsGesture()
case .left:
handleLeftwardsGesture()
default:
break
}
default:
break
}
}
fileprivate func handleUpwardsGesture() {
print("Up")
}
fileprivate func handleDownwardsGesture() {
print("Down")
}
fileprivate func handleRightwardsGesture() {
print("Right")
}
fileprivate func handleLeftwardsGesture() {
print("left")
}
-
手势一张UIImageView实现简单轮播
-
扩展:
-
使用DispatchSourceTimer定时器
-
-
- ###越来越像了
轻轻点击,关注我简书
轻轻点击,关注我简书
轻轻点击,关注我微博
浏览我的GitHub
扫一扫,关注我