0

在单击按钮时,我使用以下代码将日期选择器视图添加到我的应用程序主视图中。

- (IBAction)CalenderButton_Click:(id)sender
{
// code to add date picker -mydatepicker

//animating


    [self.view addSubview:myPicker];


    CGRect onScreenFrame=myPicker.frame;
     CGRect offScreenFrame=onScreenFrame;
     offScreenFrame.origin.y=self.view.bounds.size.height;
     myPicker.frame=offScreenFrame;
     [UIView beginAnimations:nil context:nil];
     [UIView setAnimationDuration:0.5f];
     myPicker.frame=onScreenFrame;
     [UIView commitAnimations];

}

我需要实现的两件事是,

1.此代码从底部为视图设置动画,我如何从顶部对其进行动画处理?

2.如果我再次点击按钮添加日期选择器,我应该检查视图是否已经添加,如果是,删除子视图。

提前致谢

4

1 回答 1

2

对于问题 2:使用布尔值检查日期选择器是否已在视图中。 isSubView是一个BOOL检查,你myPicker在子视图中。

更新:

- (IBAction)CalenderButton_Click:(id)sender
{

//animating

   if(!isSubview){
      isSubview = YES;
      [self.view addSubview:myPicker];
 /*    
      CGRect onScreenFrame=myPicker.frame;
      CGRect offScreenFrame=onScreenFrame;
      offScreenFrame.origin.y=self.view.bounds.size.height;
      myPicker.frame=offScreenFrame;
      [UIView beginAnimations:nil context:nil];
      [UIView setAnimationDuration:0.5f];
      myPicker.frame=onScreenFrame;
      [UIView commitAnimations];
 */    

// to slide your view from the top
     [myPicker setFrame:CGRectMake(0, -200, 320, 200)];
     [myPicker setBounds:CGRectMake(0, 0, 320, 200)];

     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationDuration:0.5f];
     [myPicker setFrame:CGRectMake(0, 0, 320, 200)];
     [UIView commitAnimations];

    }else{
      isSubview = NO;
      [myPicker removeFromSuperView];
    }
}
于 2012-10-30T04:50:59.107 回答