所以我想要的是当用户点击sideNav按钮时会出现两个单独的动画。当前视图将滑出控制器,新视图将滑入。我为视图设置了两个不同的动画点,但出于某种原因,当用户点击sideNav按钮时,视图会移动,就像它们已连接一样。我做错了什么?
- (IBAction)sideNav:(id)sender {
if (draw1 == 0) {
draw1 = 1;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
navView.frame = CGRectMake(0, 0, 320, 568);
newsView.frame = CGRectMake(320, 0, 320, 568);
[UIView commitAnimations];
} else {
draw1 = 0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
navView.frame = CGRectMake(-320, 0, 320, 568);
newsView.frame = CGRectMake(0, 0, 320, 568);
[UIView commitAnimations];
}
}
答案 0 :(得分:2)
回答问题的一步是简化代码。首先,除非它在其他地方用作整数,draw1
应该是BOOL
,其值为YES
和NO
。其次,不需要所有相同的重复代码。理解类似的东西要容易得多:
- (IBAction)sideNav:(id)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
navView.frame = draw1 ? CGRectMake(-320, 0, 320, 568) : CGRectMake(0, 0, 320, 568);
newsView.frame = draw1 ? CGRectMake(0, 0, 320, 568) : CGRectMake(320, 0, 320, 568);
[UIView commitAnimations];
draw1 = !draw1;
}