我试图通过调整 alpha 属性来实现 UIView 渐变显示的效果。我需要先渐变显示,然后保持可见状态几秒钟,最后再渐隐消失。
但是,这个渐变显示的效果不起作用,视图直接瞬间出现,而非按照0.5秒的时间间隔缓慢地呈现出来,而渐隐消失的效果却很流畅。
因此似乎渐变显示的动画并没有生效,它只是立即将 alpha 设置为 1.0。我有点不知道该怎么办了。有什么建议吗?谢谢!
但是,这个渐变显示的效果不起作用,视图直接瞬间出现,而非按照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];
}