2

我正在开发一个注入触摸的企业应用程序。我使用的方法与在 KIF 框架中看到的方法相同(参见https://github.com/square/KIF/tree/master/Additions)。

至少有一种情况,这种方法不起作用:

当用户点击设备时,注入不再适用于 scrollviews。当我注入移动触摸事件时,它们不再滚动。当用户自己在设备上滚动时,注入确实再次起作用。似乎不是用 KIFs 方法设置的状态,而是在真正触摸发生时由 iOS 设置的。

有人知道更多吗?也许有一个想法,它可能是什么状态?我已经尝试了几个听起来正确的私有 API 调用,但没有一个有效。

这是一些代码,描述了我使用的方法:

触地:

UITouch *touch = [[[UITouch alloc] initAtLocation:point inWindow:hitView.window] autorelease];
[touch setPhase:UITouchPhaseBegan];

UIEvent *newEvent = [hitView _eventWithTouch:touch];
[_activeTouchEvents addObject:touch];

[[UIApplication sharedApplication] sendEvent:newEvent];

触摸移动 UITouch* touch = _activeTouchEvent;

[touch setPhase:UITouchPhaseMoved];
[touch setLocationInWindow:point];

UIEvent *newEvent = [hitView _eventWithTouch:touch];
[[UIApplication sharedApplication] sendEvent:newEvent];

润色

UITouch* touch = _activeTouchEvent;
[touch setPhase:UITouchPhaseEnded];
[touch setLocationInWindow:point];

UIEvent *newEvent = [hitView _eventWithTouch:touch];
[[UIApplication sharedApplication] sendEvent:newEvent];
[_activeTouchEvent release];
_activeTouchEvent = nil;

UIView(扩展)

- (UIEvent *)_eventWithTouch:(UITouch *)touch;
{
    UIEvent *event = [[UIApplication sharedApplication] performSelector:@selector(_touchesEvent)];

    CGPoint location = [touch locationInView:touch.window];
    KIFEventProxy *eventProxy = [[KIFEventProxy alloc] init];
    eventProxy->x1 = location.x;
    eventProxy->y1 = location.y;
    eventProxy->x2 = location.x;
    eventProxy->y2 = location.y;
    eventProxy->x3 = location.x;
    eventProxy->y3 = location.y;
    eventProxy->sizeX = 1.0;
    eventProxy->sizeY = 1.0;
    eventProxy->flags = ([touch phase] == UITouchPhaseEnded) ? 0x1010180 : 0x3010180;
    eventProxy->type = 3001;

    NSSet *allTouches = [event allTouches];
    [event _clearTouches];
    [allTouches makeObjectsPerformSelector:@selector(autorelease)];
    [event _setGSEvent:(struct __GSEvent *)eventProxy];
    [event _addTouch:touch forDelayedDelivery:NO];

    [eventProxy release];
    return event;
}

UITouch(扩展)

- (id)initAtLocation:(CGPoint)point inWindow:(UIWindow *)window
{
    self = [super init];
    if (self == nil) {
        return nil;
    }

    // Create a fake tap touch
    _tapCount = 1;
    _locationInWindow = point;
    _previousLocationInWindow = _locationInWindow;

    UIView *hitTestView = [window hitTest:_locationInWindow withEvent:nil];

    _window = [window retain];
    _view = [hitTestView retain];
    _gestureView = [hitTestView retain];
    _gestureRecognizers = [[NSMutableArray arrayWithArray:[hitTestView gestureRecognizers]] retain];
    _phase = UITouchPhaseBegan;
    _touchFlags._firstTouchForView = 1;
    _touchFlags._isTap = 1;
    _timestamp = [[NSProcessInfo processInfo] systemUptime];

    return self;
}

- (void)setPhase:(UITouchPhase)phase;
{
    _phase = phase;
    _timestamp = [[NSProcessInfo processInfo] systemUptime];
}

编辑 1:“着陆”的代码块不正确

4

0 回答 0