2015-12-06 79 views
1

有了iOS,我知道所有更新到UI必須在主線程上完成。 是否從UI外部讀取從主線程外部執行?在iOS上,可以在主線程外讀取UI嗎?

我有一些需要使用MKMapViewconvertCoordinate方法,該方法需要toPointToView: UIView計算上昂貴的邏輯。這是否可以安全地在主線外進行?

+0

//邏輯可以在後臺threadCGPoint的startPoint =寫入[ffMapView convertCoordinate:startCoordinate toPointToView:自我。視圖]; 但最終出來像添加子視圖或視圖動畫必須在主線程 – Alok

回答

1

是的,你可以做框架計算或背景的任何邏輯。但像添加子視圖,動畫等UI操作必須在主線程中。

CGPoint annPoint = [self.mapView convertCoordinate:coord toPointToView:self.mapView];  
    mapPic = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic.png"]]; 
    mapPic.frame = CGRectMake(annPoint.x, annPoint.y, 32, 32); 
    [self.view addSubview:macPic]; 

可以寫成按您的要求:

- (void)doCalculation 
{ 
    //you can use any string instead "com.mycompany.myqueue" 
    dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mycompany.myqueue", 0); 

    dispatch_async(backgroundQueue, ^{ 
     CGPoint annPoint = [self.mapView convertCoordinate:coord toPointToView:self.mapView];  
    mapPic = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic.png"]]; 
    mapPic.frame = CGRectMake(annPoint.x, annPoint.y, 32, 32); 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.view addSubview:macPic]; 
     });  
    }); 
} 

希望這將有助於

相關問題