- (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);
}
爲什麼標記爲Android? – erdekhayser