我是IOS App Development的新手。我有一个2 UIView和1 ScrollView的视图。其中一个UIView是一个页脚,我想以这样的方式设置动画,它会根据某些条件隐藏/显示进出屏幕。但是,当我更改页脚的框架(显示/隐藏页脚)时,没有任何反应。
所以我做了一个页脚的NSLog和self.view.subviews [2],发现页脚框架已成功更改,但同样的更改未反映在self.view中。
如果我每次调用hideFooter / showFooter时都会 [self.view addSubview:footer] 解决我的问题,但是我的滚动视图(我无法纠正,因此这不是很糟糕)一个选项)。
我在这里遗漏了什么吗?有没有办法修改UIView的框架,以便更改反映在UI中? (我已经阅读了关于修改UIView框架的大部分答案。如果没有将UIView再次添加到self.view,似乎没有。)
请注意,我已经在@implementation中的类中全局声明了footer,并且只在viewDidLoad调用的方法中初始化了一次。
@implementation PlayAudioViewController
UIButton *play;
UIButton *next;
UIButton *previous;
UIView* header;
UIView* footer;
- (void)viewDidLoad
{
[self addFooter];
}
-(void)addFooter{
footer = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height * 1, self.view.frame.size.width, self.view.frame.size.height * 0.1)];
UIImageView *headerImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * 0.1)];
headerImage.image = [UIImage imageNamed:@"Wooden-Background.jpg"];
[footer addSubview:headerImage];
footer.backgroundColor = [UIColor redColor];
queue = [[NSMutableArray alloc] init];
play = [[UIButton alloc]initWithFrame:CGRectMake(footer.frame.size.width * 0.475, footer.frame.size.height * 0.10, footer.frame.size.height * 0.80, footer.frame.size.height * 0.80)];
[play setBackgroundImage:[UIImage imageNamed:@"play_1.png"] forState:UIControlStateNormal];
[play setBackgroundImage:[UIImage imageNamed:@"stop.png"] forState:UIControlStateSelected];
[play addTarget:self action:@selector(universalplay:) forControlEvents: UIControlEventTouchUpInside];
play.enabled = YES;
[footer addSubview:play];
next = [[UIButton alloc]initWithFrame:CGRectMake(footer.frame.size.width * 0.675, footer.frame.size.height * 0.20, footer.frame.size.height * 0.60, footer.frame.size.height * 0.60)];
[next setBackgroundImage:[UIImage imageNamed:@"next.png"] forState:UIControlStateNormal];
[next addTarget:self action:@selector(nextplay:) forControlEvents: UIControlEventTouchUpInside];
next.enabled=NO;
[footer addSubview:next];
previous = [[UIButton alloc]initWithFrame:CGRectMake(footer.frame.size.width * 0.29, footer.frame.size.height * 0.20, footer.frame.size.height * 0.60, footer.frame.size.height * 0.60)];
[previous setBackgroundImage:[UIImage imageNamed:@"previous.png"] forState:UIControlStateNormal];
[previous addTarget:self action:@selector(previousplay:) forControlEvents: UIControlEventTouchUpInside];
previous.enabled=NO;
[footer addSubview:previous];
NSLog(@"%@", footer.subviews[1]);
[self.view addSubview:footer];
[self hideFooter];
}
-(void)showFooter{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
footer.frame = CGRectMake(0, self.view.frame.size.height * 0.9, self.view.frame.size.width, self.view.frame.size.height * 0.1);
//[self.view addSubview:footer];
[UIView commitAnimations];
NSLog(@"%@", self.view.subviews[0]);
NSLog(@"%@", self.view.subviews[1]);
NSLog(@"%@", ((UIButton *)((UIView *)self.view.subviews[2]).subviews[1]));
}
-(void)hideFooter{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
footer.frame = CGRectMake(0, self.view.frame.size.height * 1, self.view.frame.size.width, self.view.frame.size.height * 0.1);
//[self.view addSubview:footer];
[UIView commitAnimations];
}
如前所述,如果我取消注释addFooter和showFooter中的注释语句,动画效果很好,但我的scrollView被搞砸了(新添加的子视图停止反映)。
此外,我还面临着播放,暂停和下一个按钮的类似问题。这些按钮将不时启用或禁用。当我更改.enable属性时,更改不再反映在self.view中。
请帮助我理解这种不寻常行为背后的原因。