2013-01-09 258 views
2

我遇到了UIView子视图动画的一些问题。我想要做的是,当你按下主视图时,子视图将从顶部滑下,然后在下一个水龙头上滑动并移除。但是在当前状态下,它只是执行第一次敲击命令,而在第二次敲击时,它会显示一个nslog,但视图和动画的删除不起作用。UIView动画无法正常工作

这里是事件处理函数的代码:

- (void)tapGestureHandler: (UITapGestureRecognizer *)recognizer 
{ 
NSLog(@"tap"); 

CGRect frame = CGRectMake(0.0f, -41.0f, self.view.frame.size.width, 41.0f); 
UIView *topBar = [[UIView alloc] initWithFrame:frame]; 
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"topbar.png"]]; 
topBar.backgroundColor = background; 

if (topBarState == 0) { 
    [self.view addSubview:topBar]; 
    [UIView animateWithDuration:0.5 animations:^{topBar.frame = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 41.0f);}]; 
    topBarState = 1; 
} else if (topBarState == 1){ 
    [UIView animateWithDuration:0.5 animations:^{topBar.frame = CGRectMake(0.0f, -41.0f, self.view.frame.size.width, 41.0f);} completion:^(BOOL finished){[topBar removeFromSuperview];}]; 

    NSLog(@"removed"); 
    topBarState = 0; 
} 

} 

我如何让这个子视图生命和正常删除吗?

问候

FreeSirenety

回答

2

你总是设定Y = -41顶栏架,所以topBarState = 1,动画作品为y=-41 to y=-41,似乎是不工作

CGRect frame = CGRectMake(0.0f, -41.0f, self.view.frame.size.width, 41.0f); 
UIView *topBar = [[UIView alloc] initWithFrame:frame]; 

每次您创建视图topBar时。
在.h中声明topBar并在viewDidLoad中分配init。

- (void)viewDidLoad { 
    CGRect frame = CGRectMake(0.0f, -41.0f, self.view.frame.size.width, 41.0f); 
    topBar = [[UIView alloc] initWithFrame:frame]; 
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"topbar.png"]]; 
    topBar.backgroundColor = background; 
     [self.view addSubview:topBar]; 
    topBarState = 0; 
} 

- (void)tapGestureHandler: (UITapGestureRecognizer *)recognizer 
{ 
    if (topBarState == 0) { 
      [UIView animateWithDuration:0.5 animations:^{topBar.frame = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 41.0f);}]; 
      topBarState = 1; 
    } else if (topBarState == 1){ 
     [UIView animateWithDuration:0.5 animations:^{topBar.frame = CGRectMake(0.0f, -41.0f, self.view.frame.size.width, 41.0f);} completion:^(BOOL finished){[topBar removeFromSuperview];}]; 
     topBarState = 0; 
    } 
} 
+0

感谢您的回答,使其完美工作! :) – FreeSirenety