【问题标题】:NSView touchesMovedWithEvent in subview is pausing an SKScene with built-in trackpad, but not magic trackpad?子视图中的 NSView touchesMovedWithEvent 正在暂停带有内置触控板的 SKScene,但不是魔术触控板?
【发布时间】:2014-08-24 20:19:57
【问题描述】:

我有一个自定义 NSView 子类,它在我的项目https://github.com/torchie/TrackKit 中用作子视图。它检测 NSTouch 并收集有关它们的物理数据,然后在其 drawRect 的详细模式方法中绘制这些东西。同时,SpriteKit 场景渲染它所渲染的内容。

但是,当我在内置触控板上移动手指时,SpriteKit 视图中的所有渲染都会暂停,直到触摸再次静止。 Magic Trackpad,有帧率下降,但渲染并没有完全停止。在这两种情况下,子视图 TKDetectorView 都会继续渲染触摸点。

有问题的 touchesMoved 方法:

-(void)touchesMovedWithEvent:(NSEvent *)event {
    for(NSTouch* touch in [event touchesMatchingPhase:NSTouchPhaseAny inView:self]) {
        [touch_identities setObject:touch forKey:[touch identity]];
    }
    [self phys_record];

    if(![self needsDisplay]) {
        [self setNeedsDisplay:YES];
    }   
}

详细模式函数断点的数据,其中我同时触摸了内置触控板 (#0) 和 Magic Trackpad (#1):

touch_identities    __NSDictionaryM *   2 key/value pairs   0x0000608000055bd0
    [0] (null)  (no summary) : (no summary) 
        key NSConcreteValue *   0x600000054b80  0x0000600000054b80
        value   NSTouch *   0x6000000b2480  0x00006000000b2480
        NSObject    NSObject        
        _index  NSInteger   10  10
        _identity   NSConcreteValue *   0x600000054b80  0x0000600000054b80
        _phase  NSTouchPhase    2   2
        _normalizedPosition NSPoint (x=0.4923553466796875, y=0.4602508544921875)    
            x   CGFloat 0.4923553466796875  0.4923553466796875
            y   CGFloat 0.4602508544921875  0.4602508544921875
        _privateFlags   NSInteger   0   0
        _view   TKDetectorView *    0x6000001ff900  0x00006000001ff900
        _device NSObject *  0x600000013300  0x0000600000013300
        _deviceSize NSSize  (width=368.50393709999997, height=311.81102370000002)   
        previous_positions  NSMutableArray *    nil 0x0000000000000000
        _isResting  BOOL    YES '\xad'
[1] (null)  (no summary) : (no summary) 
    key NSConcreteValue *   0x60800005ea50  0x000060800005ea50
    value   NSTouch *   0x6080000b3740  0x00006080000b3740
        NSObject    NSObject        
        _index  NSInteger   1   1
        _identity   NSConcreteValue *   0x60800005ea50  0x000060800005ea50
        _phase  NSTouchPhase    2   2
        _normalizedPosition NSPoint (x=0.7928314208984375, y=0.3984222412109375)    
        _privateFlags   NSInteger   0   0
        _view   TKDetectorView *    0x6000001ff900  0x00006000001ff900
        _device NSObject *  0x600000014d70  0x0000600000014d70
        _deviceSize NSSize  (width=297.63779534999998, height=215.43307092000001)   
        previous_positions  NSMutableArray *    nil 0x0000000000000000
        _isResting  BOOL    YES '\xad'

我认为这可能是 SpriteKit 渲染线程在手指移动时被位置数组的不断修改中断的结果,但这并不能解释为什么它只发生在内置触控板上MacBook Air 而不是外接 Magic Trackpad。我发现设备之间对 touchesMoved 的解释没有相关差异。

【问题讨论】:

    标签: objective-c macos cocoa nsview trackpad


    【解决方案1】:

    原假设:对于 touchesMoved 事件,内置触控板的轮询率明显高于魔术触控板,导致 drawRect 数量增加,并中断 SpriteKit 视图的绘图线程。

    失败 - 增加 touchesMovedWithEvent 调用次数和 setNeedsDisplay 中 drawRect 调用次数的计数器显示,在同一时间段内,内置和 Magic Trackpad 在数量上没有显着差异。

    解决方案:

    touchesMovedWithEvent 在方法结束时为自定义视图调用了 setNeedsDisplay。

    消除 touchesMovedWithEvent 中的 setNeedsDisplay 调用,而是在 SpriteKit 场景的更新方法中调用自定义视图的 setNeedsDisplay,从而“从上方”对该子视图进行操作解决了该问题。

    TKMyScene:

    -(void)update {
        [detector setNeedsDisplay:YES];
    }
    

    而不是 TKDetectorView

    -(void)touchesMovedWithEvent {
        [self setNeedsDisplay:YES];
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-11
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      • 2022-10-25
      • 1970-01-01
      • 2018-08-07
      相关资源
      最近更新 更多