在 Objective-C 中显示和隐藏具有相同动画的 UIView?
Show And Hide the UIView With Same Animation In Objective-C?
我有一个 UITextField
searchbox,起初它是隐藏的,我想用动画显示它并且它有效。但是当我再次隐藏它时,动画突然发生,而不是动画中定义的时间。
这是我的代码:
[UIView transitionWithView:searchbox duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{searchbox.hidden = NO;}completion:NULL];
[UIView transitionWithView:searchbox duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{searchbox.hidden = YES;}completion:NULL];
必须使用 .alpha
来隐藏或显示 UIView
。
正确的代码是:
[UIView transitionWithView:searchbox duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{searchbox.alpha=1;}completion:NULL];
[UIView transitionWithView:searchbox duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{searchbox.alpha=0;}completion:NULL];
动画调用之间应该有延迟。
这是一个例子:
- (void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(show) userInfo:nil repeats:NO];
}
- (void) show {
[UIView transitionWithView:searchbox duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{searchbox.hidden = NO;}completion:NULL];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(hide) userInfo:nil repeats:NO];
}
- (void) hide {
[UIView transitionWithView:searchbox duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{searchbox.hidden = YES;}completion:NULL];
}
显示
searchbox.alpha = 0;
searchbox.isHidden = NO;
[UIView transitionWithView:searchbox
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
searchbox.alpha = 1.0
}completion:NULL];
隐藏
searchbox.alpha = 1.0;
searchbox.isHidden = NO;
[UIView transitionWithView:searchbox
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
searchbox.alpha = 0
}completion:^(isComplete){
if (isComplete) {
searchbox.isHidden = YES
}}];
使用视图的 alpha 属性 而不是隐藏
[UIView transitionWithView:searchbox duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{searchbox.alpha = 1.0;}completion:NULL];
[UIView transitionWithView:searchbox duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{searchbox.alpha = 0;}completion:NULL];
我有一个 UITextField
searchbox,起初它是隐藏的,我想用动画显示它并且它有效。但是当我再次隐藏它时,动画突然发生,而不是动画中定义的时间。
这是我的代码:
[UIView transitionWithView:searchbox duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{searchbox.hidden = NO;}completion:NULL];
[UIView transitionWithView:searchbox duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{searchbox.hidden = YES;}completion:NULL];
必须使用 .alpha
来隐藏或显示 UIView
。
正确的代码是:
[UIView transitionWithView:searchbox duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{searchbox.alpha=1;}completion:NULL];
[UIView transitionWithView:searchbox duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{searchbox.alpha=0;}completion:NULL];
动画调用之间应该有延迟。 这是一个例子:
- (void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(show) userInfo:nil repeats:NO];
}
- (void) show {
[UIView transitionWithView:searchbox duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{searchbox.hidden = NO;}completion:NULL];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(hide) userInfo:nil repeats:NO];
}
- (void) hide {
[UIView transitionWithView:searchbox duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{searchbox.hidden = YES;}completion:NULL];
}
显示
searchbox.alpha = 0;
searchbox.isHidden = NO;
[UIView transitionWithView:searchbox
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
searchbox.alpha = 1.0
}completion:NULL];
隐藏
searchbox.alpha = 1.0;
searchbox.isHidden = NO;
[UIView transitionWithView:searchbox
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
searchbox.alpha = 0
}completion:^(isComplete){
if (isComplete) {
searchbox.isHidden = YES
}}];
使用视图的 alpha 属性 而不是隐藏
[UIView transitionWithView:searchbox duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{searchbox.alpha = 1.0;}completion:NULL];
[UIView transitionWithView:searchbox duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{searchbox.alpha = 0;}completion:NULL];