2013-09-26 30 views
-3
- (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; 
         }); 

     } 
    } 

我想從AppDelegate.m調用ViewController.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); 
} 
+2

爲什麼標記爲Android? – erdekhayser

回答

0

這是不支持的。只有屏幕上的UIView實例纔會受到觸動。

+0

那麼請建議在這種情況下應該做什麼? – user2811811

+0

我不確定你想要完成什麼,或者爲什麼。 iOS實現了一個觸摸界面,屏幕上的UIView實例可以接收觸摸事件。如果屏幕上沒有視圖,則沒有可觸摸的東西。這聽起來像你試圖做的是在用戶使用其他應用程序時窺探。 – bneely

+0

是的,我想實施一些。我不想將我的課程綁定到單一視圖,而是像其他任何應用程序(如音頻或VOIP)一樣在後臺運行。所以我現在正在做的是在多屏幕上窺探用戶。是否可以調用整個類或至少3個在AppDelegate.m中的ViewController.m類中聲明的主要方法?感謝您繼續考慮。 – user2811811

相關問題