iphone - 如何在检查是否已添加 subview 后使用相同的按钮操作关闭 subview ?

标签 iphone ios ipad

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

- (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.此代码从底部动画化 View ,我怎样才能从顶部动画化它?

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

提前致谢

最佳答案

对于问题 2:使用 bool 值检查日期选择器是否已在 View 中。 isSubView 是一个 BOOL 来检查,你的 myPicker 在 subview 中。

更新:

- (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];
    }
}

关于iphone - 如何在检查是否已添加 subview 后使用相同的按钮操作关闭 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13132735/

相关文章:

ios - 如何在 appdelegate 中推送导航?

ios - 从另一个自定义应用程序打开自定义企业应用程序时遇到问题

objective-c - 从 Xcode 项目中清除 .png 文件

ios - 按钮的快速图像太大

javascript - Jquery 移动数据-rel ="dialog"在 iphone 或 ipad 上不起作用

iphone - 越狱 iOS 的命令行程序在 iPad 中死亡,但在 iPhone 3G 中没有

iphone - iPad 2 上带有 HDMI 适配器的黑条

ios - 如何在检测到自上次屏幕触摸后用户不活动/空闲时间时注销应用程序

javascript - 如何在iPhone中以HTML iframe自动播放youTube Video

iphone - 如何在调试期间在 iPad 中保持相同的 iPhone 应用尺寸