【发布时间】:2011-08-13 06:05:41
【问题描述】:
我在视图控制器的自定义按钮上附加了一些动画:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *natSenButton = [UIButton buttonWithType:UIButtonTypeCustom];
natSenButton.frame = CGRectMake(114, 4, 93, 30);
[natSenButton setTitle:@"Comment" forState:UIControlStateNormal];
[natSenButton setTitleColor: [UIColor colorWithRed: 255 / 255.0 green:0 blue: 0 alpha:1.0] forState: UIControlStateNormal];
// text: color with dark red
[natSenButton addTarget:self action:@selector(displayNativeSentence) forControlEvents:UIControlEventTouchUpInside];
natSenButton.tag = 333;
[[natSenButton layer] setCornerRadius:8.0f];
[[natSenButton layer] setMasksToBounds:YES];
natSenButton.layer.borderWidth = 2.0;
natSenButton.layer.borderColor =
[UIColor colorWithRed: 25 / 255.0 green:255/ 255.0 blue: 255/255.0 alpha:1.0].CGColor;
// above is light blue color
natSenButton.layer.backgroundColor = [UIColor colorWithRed: 255 / 255.0 green:255/ 255.0 blue: 255/255.0 alpha:1.0].CGColor;
// background is white
// natSenButton.center = self.center;
natSenButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
//Create an animation with pulsating effect
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
theAnimation.duration=1.0;
theAnimation.repeatCount= 999;
theAnimation.autoreverses=YES;
theAnimation.fromValue=[NSNumber numberWithFloat:1.0];
theAnimation.toValue=[NSNumber numberWithFloat:0.2];
[natSenButton.layer addAnimation:theAnimation
forKey:@"animateOpacity"];
[self.view addSubview:natSenButton];}
但是,有时我需要隐藏这个按钮:
for( UIView *view in self.view.subviews ) {
if( [view isKindOfClass:[UIButton class]] ) {
if( view.tag == 333 )
[view setHidden:YES];
}
}
...过了一会儿我需要重新打开它:
for( UIView *view in self.view.subviews ) {
if( [view isKindOfClass:[UIButton class]] ) {
if( view.tag == 333 )
[view setHidden:NO];
[view setNeedsDisplay];
}
}
这一切都很好,除非我点击后退按钮并返回父视图控制器,然后返回此视图控制器。在这些情况下,我的“view setNeedsDisplay”不再起作用,我的动画也没有打开。
这是我在父视图控制器中创建后栏项目的地方:
- (id)init {
[super initWithStyle:UITableViewStylePlain];
[[self navigationItem] setTitle:@"Wordplay"];
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
temporaryBarButtonItem.title = @"<";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];
return self;
}
....这里是父视图控制器推回失去动画的问题视图控制器的地方:
[[self navigationController] pushViewController:sentenceViewController animated:YES];
谁能告诉我为什么我的动画会丢失以及如何重新打开它?谢谢。
【问题讨论】:
标签: iphone animation uiviewcontroller