2012-07-04 35 views
0

我試圖創建一個淡出按鈕,然後打開頁面。用子查看按鈕淡入UIView

下面是我使用的當前代碼,這是行不通的,以淡入/淡出按鈕:

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view 
{ 
    if (index == 0) { 
     view = [[[NSBundle mainBundle] loadNibNamed:@"nib1" owner:self options:nil] lastObject]; 
    }else { 
     view = [[[NSBundle mainBundle] loadNibNamed:@"nib2" owner:self options:nil] lastObject]; 
     UIView *containView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 60, 100)]; 
     _btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
     _btn.frame = CGRectMake(30, 90, 40, 30); 
     [_btn setBackgroundImage:[UIImage imageNamed:@"playButton.png"] forState:UIControlStateNormal]; 
     [_btn addTarget:self action:@selector(fadeButton:) forControlEvents:UIControlEventTouchUpInside]; 
     //[self fadeOut:_btn withDuration:1.0 andWait:1.0]; 
     [containView addSubview:_btn]; 
     [containView setAlpha:0.0]; 
     [view addSubview:containView]; 
     [UIView beginAnimations:nil context:nil]; 
     [containView setAlpha:1.0]; 
     [UIView commitAnimations]; 
    } 
return view; 
} 

我使用也嘗試:

[UIView animateWithDuration:0.5 animations:^{_btn.alpha = 1.0;}]; 

無這些工作。有沒有人有關於如何淡出子視圖的任何提示?由於

回答

2

當您調用[UIView commitAnimations]時,動畫會同時進行動畫處理。 當你想要一個其他動畫之後,動畫的東西嘗試拼圖動畫塊竣工:

[UIView animateWithDuration:0.5 animations:^ 
{ 
    //animate out 
} 
completion:^ (BOOL finished) 
{ 
    [UIView animateWithDuration:0.5 animations:^ 
    { 
     //animate in 
    } 
    completion:^ (BOOL finished) 
    { 
     //Optional 
    }]; 
}]; 
+0

這是完美的,它現在爲我工作。謝謝 – mhorgan

1

淡出,你必須阿爾法動畫爲0,而不是1

[UIView animateWithDuration:0.5 animations:^{_btn.alpha = 0.0;}]; 

這應該工作。

+0

我的意思是在我原來的職位說,每當我嘗試,如設置透明度爲0.0,按鈕只是從未出現。它總是隱藏的。 – mhorgan