2017-04-22 37 views

回答

0

你可以看看觸摸是否來自使用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]; 

} 

//編輯 OR

子類的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])); 
      } 
    } 

添加觀察員對於包含滾動視圖的類中的這些通知a nd啓用/禁用相應的滾動。

+0

我已經嘗試過這一點,但由於某種原因滾動後觸發的touchesBegan啓動,以便在接下來的時間我摸施加scrollEnabled。 (第一次使用手寫筆滾動滾動時) – MoHaKa

+0

此功能'scrollViewWillBeginDragging'(存在於UIScrollView委託中)在其他任何事情之前觸發(即使在'touchesBegan'之前)。如果在這一點上有任何方法可以獲得觸摸的類型,我認爲它應該起作用。 – MoHaKa

+0

如果是這樣的話,你可以繼承UIApplication並且可以使用sendEvent方法。讓我在答案中添加更多細節。 – Darshana

1

您可以在UIGestureRecognizer上設置allowedTouchTypes屬性。

例如:

scrollView.panGestureRecognizer.allowedTouchTypes = [UITouchType.direct.rawValue as NSNumber]