如果 UIScrollView 来自手写笔(Apple Pencil),我正在尝试取消滚动。
有什么建议吗?
您可以allowedTouchTypes
在 UIGestureRecognizer 上设置属性。
例如:
scrollView.panGestureRecognizer.allowedTouchTypes = [UITouchType.direct.rawValue as NSNumber]
您可以使用 UITouch 的类型来确定触摸是否来自手指的手写笔。
if (touch.type == UITouchTypeStylus) {
//its touch from Stylus.
}
现在,对于滚动视图,您可以创建 UIScrollview 的子类并实现 TouchBegan 方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if (touch.type == UITouchTypeStylus) {
self.scrollEnabled = NO;
}
else
{
self.scrollEnabled = YES;
}
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (touch.type == UITouchTypeStylus) {
self.scrollEnabled = NO;
}
else
{
self.scrollEnabled = YES;
}
[super touchesMoved:touches withEvent:event];
}
// 编辑或
子类 UIApplication :
@interface MyUIApplication : UIApplication
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
NSSet *allTouches = [event allTouches];
if ([allTouches count] > 0) {
/
UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
if (phase == UITouchPhaseBegan){
UITouch *touch = [allTouches anyObject];
if (touch.type == UITouchTypeStylus) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"DisableScroll" object:self];
}
else
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"EnableScroll" object:self];
}
}
}
}
@end
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, NSStringFromClass([MyUIApplication class]), NSStringFromClass([AppDelegate class]));
}
}
在包含滚动视图的类中添加这些通知的观察者并相应地启用/禁用滚动。