2011-08-19 27 views
0

我創建了一個透明模態視圖,它工作正常。我想要的是使過渡到採取當模態視圖appears..below是代碼的地方褪色..模態視圖中的淡出過渡

UIView *modalView = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
modalView.opaque = NO; 
modalView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.9]; 

UILabel *label1 = [[[UILabel alloc] init] autorelease]; 
label1.text = @"Modal View"; 
label1.textColor = [UIColor whiteColor]; 
label1.backgroundColor = [UIColor clearColor]; 
label1.opaque = NO; 
[label1 sizeToFit]; 
[modalView addSubview:label1]; 

[self.view addSubview:modalView]; 

回答

3

使用下面的代碼中褪色。

// Fade In 

- (void)fadeInModalView { 

    [self.view addSubview:modalView]; 
    modalView.alpha = 0; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.3]; 
    modalView.alpha = 0.9; 
    [UIView commitAnimations]; 
} 

淡出出。

// Fade Out 

- (void)fadeOutModalView { 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.3]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(removeModalView)]; 
    modalView.alpha = 0.0; 
    [UIView commitAnimations]; 
} 

刪除modalView後淡出。

// Remove modalView after it faded out 

- (void)removeModalView { 

    [modalView removeFromSuperview]; 
} 
+0

我得到錯誤「self.alpha」是不是結構或工會 – kingston

+0

對不起。我犯了錯。現在查看我更新的答案。 – EmptyStack

+0

感謝那個工作的人.... – kingston

相關問題