2013-06-25 25 views
0

我試圖讓三個圖像一次淡入淡出。我認爲我與這段代碼非常接近,但它並不像我預期的那樣工作。定時困難,努力淡入淡出圖像

我希望的是,每幅圖像會在0.5秒內消失,然後在屏幕上保持2秒鐘,然後在0.5秒內淡出。一旦第三張圖像淡出,我希望它停止。

下面的代碼一直在淡出第一個圖像,我似乎無法正確地取得時間。任何援助將是偉大的!

-(void) viewDidLoad { 

    pImageC = [[UIImageView alloc]init]; 
    NSArray *animationArray = [NSArray arrayWithObjects:[UIImage 
    imageNamed:@"p_image_a.png"],[UIImage imageNamed:@"p_image_b.png"],[UIImage 
    imageNamed:@"p_image_c.png"], nil]; //add your images here 
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(crossfade) 
    userInfo:nil repeats:YES]; 
    [pImageC setFrame:CGRectMake(0,0,320,367)]; 
    pImageC.animationImages = animationArray; //mainImageView is imageview 
    pImageC.animationDuration = 2; 
    pImageC.animationRepeatCount = 0; 
    [pImageC startAnimating]; 
    [self.view addSubview:pImageC]; 
} 

- (void)crossfade { 

    [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut 
    animations:^{ 
     pImageC.alpha = !pImageC.alpha; 
    }completion:^(BOOL done){ 
     // 
    }]; 
} 
+0

你pImageC.alpha = pImageC.alpha!;沒有意義。 alpha是一個浮點數,而不是布爾值。 – Vinzzz

回答

0

嘗試像這樣的交叉淡化動畫

- (void)crossfade 
{ 
    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ 
     self.animationImageView.alpha = 0.0; 
    }completion:^(BOOL done){ 
     [self performAnimationOnView]; 

     [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ 
      self.animationImageView.alpha = 1.0; 
     }completion:^(BOOL done){ 


     }]; 


    }]; 

}