2014-03-25 37 views
0

我有一個tabbarcontroller與2個選項卡。我已經設置了一個滑動手勢識別器,允許用戶在標籤頁之間滑動,用戶也可以點擊標籤頁也轉到該標籤頁。切換標籤tabbarcontroller給出不同的結果時,通過gesturerecognizer切換刷卡只是點擊標籤

在第二個標籤的viewcontroller我有一個子視圖在主視圖。使用pan gesturerecognizer設置子視圖,允許用戶用2指拖動移動子視圖。默認子視圖位置是視圖的右下角(來自故事板)。如果用戶將子視圖拖到不同的位置,如果用戶滑動到第一個選項卡或僅點擊選項卡欄上的第一個選項卡,我將視圖中的子視圖的中心點保存就會消失。當用戶返回到第二個選項卡時(通過從第一個選項卡視圖中輕掃或通過點擊第二個選項卡上的第二個選項卡),我將子視圖的中心設置爲viewWillAppear中已保存的centrepoint屬性。

奇怪的是,如果我manulaly從右下方移動子視圖位置(說左上),將出現以下情況選項卡上的變化

  1. ,如果我通過敲擊的TabBar從TAB2到標籤1走,然後通過向左滑動返回到tab2 - 子視圖保持在正確的「移動」位置,即右上角

  2. 但是,如果我通過向右滑動從tab2到tab1,然後通過向左滑動返回到tab2 - 子視圖在消失之前首先暫時出現在默認的右下位置,並在正確的(「移動」)中立即重新出現即左上方

  3. 最後,如果我通過任一方法轉到選項卡1,但隨後通過點擊選項卡欄返回選項卡2,子視圖僅出現在默認的右下角位置(不反映上一步棋)。香港專業教育學院檢查,並在TAB2視圖 - 控制在viewWillAppear中的代碼行確實出現了運行,但在我看來,就好像一些其他的TabBar相關的代碼可能會被後來運行把子視圖回到默認位置

任何想法發生了什麼,以及我如何獲得子視圖的新位置堅持?以及如何我可以停止子視圖暫時出現在上面第2點中描述的默認位置。任何幫助讚賞

這裏是我刷到其他標籤代碼

- (IBAction)nextTab:(id)sender { 

NSInteger fromIndex = [self.tabBarController selectedIndex]; 
NSInteger toIndex = fromIndex + 1; 

if (toIndex > self.tabBarController.viewControllers.count - 1) { 
    return; 
} else { 

    //transition code 
    UIView *fromView = self.tabBarController.selectedViewController.view;//create the from view 
    UIView *toView = [[self.tabBarController.viewControllers objectAtIndex:toIndex] view];//create the to view 

    // Get the size of the view area. 
    CGRect viewSize = fromView.frame; 
    BOOL scrollRight = toIndex > fromIndex; 

    // Add the to view to the tab bar view. 
    [fromView.superview addSubview:toView]; 

    // Position it off screen. 
    UIInterfaceOrientation orientation = self.interfaceOrientation; 
    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { 
     toView.frame = CGRectMake((scrollRight ? 768 : -768), viewSize.origin.y, 768, viewSize.size.height); 
    } else { 
     toView.frame = CGRectMake((scrollRight ? 1024 : -1024), viewSize.origin.y, 1024, viewSize.size.height); 
    } 

    [UIView animateWithDuration:0.3 
        animations: ^{ 

         // Animate the views on and off the screen. This will appear to slide. 
         if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { 
          fromView.frame =CGRectMake((scrollRight ? -768 : 768), viewSize.origin.y, 768, viewSize.size.height); 
          toView.frame =CGRectMake(0, viewSize.origin.y, 768, viewSize.size.height); 
         } else { 
          fromView.frame =CGRectMake((scrollRight ? -1024 : 1024), viewSize.origin.y, 1024, viewSize.size.height); 
          toView.frame =CGRectMake(0, viewSize.origin.y, 1024, viewSize.size.height); 
         } 

// fromView.frame = CGRectMake((scrollRight -768:768),viewSize.origin.y,768,viewSize.size 。高度); // toView.frame = CGRectMake(0,viewSize.origin.y,768,viewSize.size.height); }

     completion:^(BOOL finished) { 
         if (finished) { 
          // Remove the old view from the tabbar view. 
          [fromView removeFromSuperview]; 
          self.tabBarController.selectedIndex = toIndex; 
         } 
        }]; 
} 

}

,這裏是在第二個選項卡

-(void)viewWillAppear:(BOOL)animated{ 

[super viewWillAppear:animated];//call to super ow super will run its own code only? 


//show the clock and then use ns timer to keep it updating 
[self updateClock:nil]; 
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateClock:) userInfo:nil repeats:YES]; 

//position the clock in custom spot if repositioned 
self.clockView.center = self.clockViewCentre; 
NSLog(@"x= %f",self.clockView.center.x); 

}

回答

0

viewWillAppear我的viewWillAppear中的代碼稱爲前佈局完成。所以改變clockView中心的代碼應該在viewDidLayoutSubviews

+0

是的謝謝,似乎已經修復它完美。有沒有一個簡單的方法,我可以看到這樣的事情,如視圖,tabbarcontrollers,視圖控制器的方法調用的順序列表....或者只是閱讀蘋果指南和體驗。 – lozflan

+0

大多數蘋果指南和經驗。我把'NSLog(@「%s」,__func__)'放到所有視圖控制器的viewWill ...和viewDid ...方法中,以查看順序是什麼:) – user3386109