我的应用如何在后台运行时访问触摸事件?

时间:2013-09-26 00:42:52

标签: iphone ios objective-c

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"Entered1");

    if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]){

        if ([[UIDevice currentDevice] isMultitaskingSupported]) {
            UIApplication *application = [UIApplication sharedApplication];

            __block UIBackgroundTaskIdentifier background_task;

            background_task = [application beginBackgroundTaskWithExpirationHandler:^{
            [application endBackgroundTask:background_task];

            background_task = UIBackgroundTaskInvalid;

        }];


        dispatch_async(dispatch_get_global_queue
                       (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                           NSLog(@"Running background");

        //Calling method from ViewController


        [application endBackgroundTask:background_task];
        background_task = UIBackgroundTaskInvalid;
                       });

        }
    }

我想从ViewController.m拨打课程AppDelegate.m,以便在后台和应用程序最小化时计算屏幕上的手指位置和大小。因此,它必须确定访问屏幕上不同窗口和应用程序的所有值。到目前为止,ViewController能够在单个窗口中找到这些值。请建议实现相同的逻辑。我们可以从AppDelegate调用这些方法,如果是这样的话?

ViewContoller.m代码中声明的方法如下,

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {


    NSLog(@"Filename =%@", inputText);

    UITouch *theTouch = [touches anyObject];
    startPoint = [theTouch locationInView:self.view];
    CGFloat x = startPoint.x;
    CGFloat y = startPoint.y;
    xCoord.text = [NSString stringWithFormat:@ "x = %f", x];
    yCoord.text = [NSString stringWithFormat:@ "y = %f", y];


    //Calculating finger size in millimeters-

    locate = [[theTouch valueForKey:@"pathMajorRadius"] floatValue];
    value.text = [NSString stringWithFormat:@"pressure = %.2f", locate];
 }

 - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

     UITouch *theTouch = [touches anyObject];
     CGPoint touchLocation = [theTouch locationInView:self.view];
     CGFloat x = touchLocation.x;
     CGFloat y = touchLocation.y;
     xCoord.text = [NSString stringWithFormat:@ "x = %f", x];
     yCoord.text = [NSString stringWithFormat:@ "y = %f", y];

     locate = [[theTouch valueForKey:@"pathMajorRadius"] floatValue];
     value.text = [NSString stringWithFormat:@"pressure = %.2f", locate];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *theTouch = [touches anyObject];
    CGPoint endPoint = [theTouch locationInView:self.view];
    xCoord.text = [NSString stringWithFormat:@ "x:%f, y:%f", startPoint.x, startPoint.y];
    yCoord.text = [NSString stringWithFormat:@ "x:%f, y:%f", endPoint.x, endPoint.y];

    locate = [[theTouch valueForKey:@"pathMajorRadius"] floatValue];
    value.text = [NSString stringWithFormat:@"pressure = %.2f", locate];

    dist_x = (endPoint.x - startPoint.x);
    dist_y = (endPoint.y - startPoint.y);

    EucDistance = sqrtf((dist_x * dist_x) + (dist_y * dist_y));
    NSLog(@"Distance =%f", EucDistance);
}

1 个答案:

答案 0 :(得分:0)

不支持此功能。只有屏幕上的UIView个实例才会收到触动。