我正試圖淡入淡出UIView中創建的動畫Alpha。我需要淡入它,幾秒鐘後可見,然後淡出它。使用UIView animateWithDuration立即更改Alpha:而不是動畫
淡出功能正常工作。視圖順利消失。但淡入只會使視圖立刻顯示出來,而不是在0.5秒的時間間隔內緩慢出現。
所以看起來像動畫中的淡入淡出不起作用,只是立即將alpha設置爲1.0
。我有點不知所措。任何想法我做錯了什麼?謝謝!
-(void)presentPopupPhrase:(NSString *)phrase
inView:(UIView *)view
withDelegate:(id)delegate
andCompletion:(void (^)(BOOL completed))completion {
MessagePopupView *pv = [[[MessagePopupView alloc] initWithFrame:self.frame andText:phrase] autorelease];
pv.alpha = 0.0;
[view addSubview:pv];
[self fadeInMPV:pv
withDuration:self.fadeDuration
andDelay:self.fadeInDelay];
[self fadeOutMPV:pv
withDuration:self.fadeDuration
afterDelay:self.fadeOutDelay
withCompletion:completion
andDelegate:delegate];
}
-(void)fadeInMPV:(MessagePopupView *)mpv
withDuration:(NSTimeInterval)duration
andDelay:(NSTimeInterval)delay
{
[UIView animateWithDuration:duration
delay:delay
options:UIViewAnimationOptionCurveLinear
animations:^{
mpv.alpha = 1.0;
}
completion:nil];
}
-(void)fadeOutMPV:(MessagePopupView *)mpv
withDuration:(NSTimeInterval)duration
afterDelay:(NSTimeInterval)delay
withCompletion:(void (^)(BOOL completed))completion
andDelegate:(id)delegate
{
[UIView animateWithDuration:duration
delay:delay
options:UIViewAnimationOptionCurveLinear
animations:^{
mpv.alpha = 0.0;
}
completion:completion];
}
編輯:
如果有幫助,這裏是VC代碼,在那裏我從調用它:
-(void)viewDidAppear:(BOOL)animated {
CGRect phraseFrame = CGRectMake(20, 341, 280, 65);
PopupPhraseController *phraseController = [[[PopupPhraseController alloc] initWithFrame:phraseFrame] autorelease];
[phraseController presentPopupPhrase:@"Test Phrase" inView:self.view withDelegate:self andCompletion:^(BOOL completed){
if (completed) {
NSLog(@"completed");
} else {
NSLog(@"not completed");
}
NSLog(@"blocked!");
}];
[super viewDidAppear:animated];
}
太棒了。非常有意義。謝謝你的回覆,保羅。 – Murdock