简体   繁体   中英

Open modal view and dismiss on iPhone

如何显示全屏模式视图,然后如果用户触摸视图上的任何位置,该视图将自行删除。

you can present a modal view that has a custom button as the background, and then when you press the button, or "background", you can call [self dismissModalViewControllerAnimated:YES];

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
   UITouch *touch = [touches anyObject];

 if ([touch view] == self) {
      if ([touch tapCount] == 2) {
         /* 2 touches here, you can dismiss your view */
      }
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   UITouch *touch = [touches anyObject];

   if ([touch view] == self) {
      if ([touch tapCount] == 1) {
  /* 1 touch, dismiss your view */   
  }
}

If you were doing this inside of a subclass of UIViewController you could do something like this. This would trigger the modal to appear when the view is initially loaded, and trigger the modal to disappear when the screen is tapped.

-(void) viewDidLoad{
    UIViewController *modalViewController = [[UIViewController alloc] init];

    [modalViewController addTarget:self action:@selector(_dismissModal) forControlEvents:UIControlEventTouchUpInside];
    [self presentModalViewController:modalViewController animated:YES];
}

-(void)_dismissModal{
    [self dismissModalViewControllerAnimated:YES];
}
  1. Put a custom UIButton on top of everything on the view you want to present modally. (A custom UIButton is transparent)
  2. Hook up the UIButton's Touch Up Inside with one of the view's method (so that it can inform its delegate view controller about the event).
  3. Implement a corresponding method in the delegate view controller so the modal view can be dismissed when the delegate hears from it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM