2

我有一个带有按钮的视图控制器。我希望在按下按钮时将新视图加载到我的视图控制器上。它不会替换现有视图,我希望它小于屏幕并在我点击小视图时隐藏。

它应该如何在代码中实现?

4

2 回答 2

3
- (IBAction)button:(id)sender 
{
UIView *view2=[[UIView alloc]initWithFrame:CGRectMake(0,0,200,200)];
[self.view addSubview:view2];
UITapGestureRecognizer *Tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Tapview)] autorelease];
[view2 addGestureRecognizer:Tap];
}

-(void)Tapview
{
[view2 removeFromSuperview];
}
于 2012-09-19T06:35:39.130 回答
2

像这样将 Tap Gesture 添加到 self.view:

 UITapGestureRecognizer *oneFinger = 
 [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerAction:)] autorelease];

// Set required taps and number of touches
[oneFinger setNumberOfTapsRequired:1];
[oneFinger setNumberOfTouchesRequired:1];

// Add the gesture to the view
[[self view] addGestureRecognizer:oneFinger];

在 .h 文件中添加一个 BOOL 标志;在 ViewDidLoad 方法中添加:

 flag = FALSE;

现在我假设你有 UIView *smallView 像这样添加到屏幕上:

[self.view addSubView:smallView];
flag = TRUE;
smallView.center = self.view.centre;

现在当点击 self.view 时点击手势动作调用

- (void)oneFingerAction:(UITapGestureRecognizer*)sender
{
   if(sender.view == self.view)
   { 
     if(flag){
       if(smallView)
       {
         [smallView removeFromSuperView];
       }
     }
   }
}
于 2012-09-19T06:34:19.610 回答