Dragging Gestures in iOS 8 Today Extensions

2019-02-03 15:20发布

问题:

I'm using a UIView subclass in my Today widget. The view makes use of swiping gestures. However, these gestures either scroll the whole Notification Center up and down, or make the Notification Center switch from Today to Notifications.

Is there any way to prevent the touch events to be bubbled up to the Notification Center scroll view? Using [self setExclusiveTouch:YES]; in the subclass did not solve it unfortunately.

回答1:

Is there any way to prevent the touch events to be bubbled up to the Notification Center scroll view? Using [self setExclusiveTouch:YES]; in the subclass did not solve it unfortunately.

No. Because of the remote view hosting that your Today widget is being presented inside, [self setExclusiveTouch:YES] doesn't quite do what you want.

The rough architecture in iOS 8.0 is:

[User touch creates a UITouch]
            |
            v
Notification Center (UIScrollView)
            |
            v
  UIRemoteView container
  (presents your UIView)
[crosses process boundary]
            |
            v
your Today widget's UIView

Think of the touch as basically becoming cloned when it crosses the process boundary. Your view's exclusive touch desires are only relevant in your widget's process space/window, and don't propagate back outwards to the Notification Center which is hosting you remotely.



回答2:

Apples official advise as mentioned in another answer:

Avoid putting a scroll view inside a widget. It’s difficult for users to scroll within a widget without inadvertently scrolling the Today view.

This is pretty poorly written advice from Apple. It is perfectly fine to use a scroll view in a Today widget but you must disable scrolling so that it doesn't interfere. Apple even use a UITableView in their WWDC talk.

Basically it advises you not to interfere with the Notification Centers existing gestures.
The existing gestures happen to be scrolling in all four directions so you are pretty limited on what you can do with gestures in a widget.

What sort of gesture are you trying to achieve? You mentioned swiping but if you do that you're going to interfere with the existing gestures and break things. This sort of behaviour wouldn't be allowed in a widget as it would effect the UX of the operating system itself.

Maybe you should look into taking a different approach to handle your action?



回答3:

According to Apple's App Extensions Programming Guide:

"Avoid putting a scroll view inside a widget. It’s difficult for users to scroll within a widget without inadvertently scrolling the Today view."