2012-10-16 83 views
0

我有一個包含兩個其他UIViews的UIView。我試圖讓他們按下按鈕。這裏是我的代碼,它們翻轉的按鈕操作:UIView翻轉過渡只出現一次

- (IBAction)btMapView:(id)sender 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:1]; 
    [UIView transitionFromView:mView1 toView:m_MapView duration:3 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL]; 
    [UIView commitAnimations]; 
} 

- (IBAction)btListView:(id)sender 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:1]; 
    [UIView transitionFromView:m_MapView toView:mView1 duration:3 options:UIViewAnimationOptionTransitionFlipFromRight completion:NULL]; 
    [UIView commitAnimations]; 
} 

我在viewDidLoad中初始化m_MapView如下:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [m_MapView setMapType:MKMapTypeHybrid]; 
    [m_MapView setZoomEnabled:YES]; 
    [m_MapView setScrollEnabled:YES]; 
    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
    region.center.latitude = 42.761738 ; 
    region.center.longitude = -71.463180; 
    region.span.longitudeDelta = 0.01f; 
    region.span.latitudeDelta = 0.01f; 
    [m_MapView setRegion:region animated:YES]; 
    m_MapView.showsUserLocation = YES; 
    [m_MapView setDelegate:self]; 

    CLLocationCoordinate2D zoomLocation; 
    zoomLocation.latitude = 42.761738; 
    zoomLocation.longitude = -71.463180; 

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, .25*METERS_PER_MILE, .25*METERS_PER_MILE); 
    MKCoordinateRegion adjustedRegion = [m_MapView regionThatFits:viewRegion]; 

    m_MapView.autoresizingMask = 
    (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); 

    [m_MapView setRegion:adjustedRegion animated:YES]; 

    DisplayMap * myLoc = [[DisplayMap alloc] init]; 
    myLoc.title = @"myLoc"; 
    myLoc.subtitle = @"Main Office"; 
    myLoc.coordinate = region.center; 
    [m_MapView addAnnotation:myLoc]; 
} 

我的第二個觀點只是上有一個標籤。

當我觸摸btMapView按鈕時,翻轉發生到另一個視圖,但是當我按下btListView按鈕時它不會翻轉回來。

任何人都可以闡明我copuld做錯了什麼?

感謝您的幫助。

編輯:

得到這個移動我到viewWillAppear中的代碼從viewDidLoad中和改變兩個按鈕的方法來此工作:

- (IBAction)btMapView:(id)sender 
{ 
    [UIView transitionFromView:mView1 
         toView:m_MapView 
         duration:1 
         options:UIViewAnimationOptionTransitionFlipFromLeft | UIViewAnimationOptionShowHideTransitionViews 
        completion:nil]; 
} 

- (IBAction)btListView:(id)sender 
{ 
    [UIView transitionFromView:m_MapView 
         toView:mView1 
         duration:1 
         options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionShowHideTransitionViews 
        completion:nil]; 
} 
+0

你不需要把transitionFromView放在begin/commitAnimations對裏面...... – Goz

+0

所以,我確實刪除了這些行,只是離開了transitionFromView,它會翻轉一次然後翻轉回去,但是在那之後視圖是空白的。 – LilMoke

回答

0

嘗試以下操作:

- (IBAction)btMapView:(id)sender 
{ 
    [mView1.superview addSubview: m_MapView]; 
    [UIView transitionFromView:mView1 toView:m_MapView duration:3 options:UIViewAnimationOptionTransitionFlipFromLeft | UIViewAnimationOptionShowHideTransitionViews completion: ^(BOOL finished) { [mView1 removeFromSuperview]; }]; 
} 

- (IBAction)btListView:(id)sender 
{ 
    [m_MapView.superview addSubview: mView1]; 
    [UIView transitionFromView:m_MapView toView:mView1 duration:3 options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionShowHideTransitionViews completion:^(BOOL finished) { [m_MapView removeFromSuperview]; }]; 
} 
+0

是的,我做了那個,但意見過渡一次,然後意見是空白的。 – LilMoke

+0

@LilMoke:你真的嘗試過我的代碼嗎?我添加了一個完成塊。嘗試一下。 – Goz

+0

我添加了您發送的代碼,但它做了同樣的事情......第一次後視圖爲空。 – LilMoke

0

使用下面的代碼在UIButton行動,它是正確的woking:

SecondView *second=[[SecondView alloc] init]; 
    [UIView transitionFromView:self.view toView:second.view duration:2.0 options:UIViewAnimationOptionTransitionFlipFromRight completion:nil]; 

這裏SecondView是SecondView的名稱。