Modern User Interaction on iOS - 笔记

WWDC 2017 Session 219 地址:[https://developer.apple.com/videos/play/wwdc2017/219/

目录:

  • 手势 The UIGestureRecognizer system
  • 系统手势 System gesture interaction
  • 拖拽 Drag and Drop

1. 手势

基础:

  • UIGestureRecognizer 比 UITouch 优先级高

    1
    2
    3
    4
    5
    class UIGestureRecognizer: NSObject {
    open var delaysTouchesEnded: Bool // default is true.
    open var cancelsTouchesInView: Bool // default is true.
    open var delaysTouchesBegan: Bool // default is false.
    }

  • UIGestureRecognizer 只有一个的胜出相应手势

  • UIGestureRecognizer 是否允许同时响应其他手势:在delegate可控制

    1
    2
    3
    4
    5
    public protocol UIGestureRecognizerDelegate: NSObjectProtocol {

    optional func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool

    }

  • UIGestureRecognizer 是否让其他手势失效

    1
    2
    3
    4
    open class UIGestureRecognizer: NSObject {
    open func shouldRequireFailure(of otherGestureRecognizer: UIGestureRecognizer) -> Bool
    open func shouldBeRequiredToFail(by otherGestureRecognizer: UIGestureRecognizer) -> Bool
    }
  • Hit Testing

  • 新属性 name,便于调试

手势系统的注意事项:

  • 复查你的构建步骤(Revisit your setups )
  • 排除和失效的标准(Exclusion and failure requirements)
  • 手势是否在正确的视图上?( Are your gesture recognizers on the right views?)

2. 系统手势

应用手势和系统手势之争!如非沉浸式的游戏或画画的应用,别推迟系统手势。

1
2
3
4
5
6
7
8
9
10
11
12
13
class MyViewController: UIViewController {

// override to return which screen edges to defer system gestures
override func preferredScreenEdgesDeferringSystemGestures() -> UIRectEdge {
return deferControlCenter ? .bottom : UIRectEdge()
}

// call whenever your method would return a different screen edge
var deferControlCenter: Bool {
didSet { setNeedsUpdateOfScreenEdgesDeferringSystemGestures() }
}

}

3. 拖拽

1
let dragInteraction = UIDragInteraction(delegate: myDelegate) myView.addInteraction(dragInteraction)

总结

手势部分关于同时兼容或禁止其他的应该都很熟悉,尤其是分页加 table view经常处理。iOS可以延迟系统边缘手势很不错的选择,很多app手势经常与系统同步调出。拖拽的话,主要在iPad上应用,就不深入学习了。