如何通过按下弹出窗口 viewcontroller 内的按钮关闭 Viewcontroller(弹出窗口)并触摸 viewcontroller 之外的任何位置?

How to dismis Viewcontroller (popup) by pressing button inside my popup viewcontroller and also touch any where outside of viewcontroller?

我有一个视图控制器和一个 button.When 我按下那个弹出 viewcontroller 将显示 up.And 当我触摸外面的任何地方时它会关闭 viewcontroller 并且 return 到我的主要 viewcontroller。

备注

我通过一个 viewcontroller 使用我的弹出窗口,我使用标识符名称。然后我使用那个故事板标识符显示弹出窗口,并在我的 viewcontroller.m 中声明 btnSelectDatePressed

需要

在那个弹出视图控制器中我有一个按钮,当用户按下时它也应该关闭 viewcontroller 并且应该 return 到我的主要 viewcontroller.My 示例图像是这样的 sample image

这是我的 viewcontroller.m 文件

#import "ViewController.h"
#import "UIViewController+ENPopUp.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}
- (IBAction)btnSelectDatePressed:(id)sender
{

    UIViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"PopUp"];
    vc.view.frame = CGRectMake(0, 0, 309.0f, 531.0f);
    [self presentPopUpViewController:vc];

}

这是我在#import "UIViewController+ENPopUp.h"

中声明的弹出方法
    #import <UIKit/UIKit.h>

    @interface UIViewController (ENPopUp)

    @property (nonatomic, retain) UIViewController *en_popupViewController;
    - (void)presentPopUpViewController:(UIViewController *)popupViewController;
    - (void)presentPopUpViewController:(UIViewController *)popupViewController completion:(void (^)(void))completionBlock;
    - (void)dismissPopUpViewController;
    - (void)dismissPopUpViewControllerWithcompletion:(void (^)(void))completionBlock;
    //- (IBAction)disMe:(id)sender;

    @end


This is my popup method declared in `#import "UIViewController+ENPopUp.m"`

#import "UIViewController+ENPopUp.h"
#import "JWBlurView.h"
#import <objc/runtime.h>

static void * ENPopupViewControllerPropertyKey = &ENPopupViewControllerPropertyKey;

static CGFloat const kAnimationDuration = .4f;
static CGFloat const kRotationAngle = 70.f;

static NSInteger const kENPopUpOverlayViewTag   = 351301;
static NSInteger const kENPopUpViewTag          = 351302;
static NSInteger const kENPopUpBluredViewTag    = 351303;

@implementation UIViewController (ENPopUp)

#pragma mark - Public Methods
- (void)presentPopUpViewController:(UIViewController *)popupViewController
{
    [self presentPopUpViewController:popupViewController completion:nil];
}

- (void)presentPopUpViewController:(UIViewController *)popupViewController completion:(void (^)(void))completionBlock
{
    self.en_popupViewController = popupViewController;
    [self presentPopUpView:popupViewController.view completion:completionBlock];
}





- (void)dismissPopUpViewController
{
    [self dismissPopUpViewControllerWithcompletion:nil];
}

- (void)dismissPopUpViewControllerWithcompletion:(void (^)(void))completionBlock
{
    UIView *sourceView = [self topView];
    JWBlurView *blurView = (JWBlurView *)[sourceView viewWithTag:kENPopUpBluredViewTag];
    UIView *popupView = [sourceView viewWithTag:kENPopUpViewTag];
    UIView *overlayView = [sourceView viewWithTag:kENPopUpOverlayViewTag];
    [self performDismissAnimationInSourceView:sourceView withBlurView:blurView popupView:popupView overlayView:overlayView completion:completionBlock];
}










//- (IBAction)disMe:(id)sender {
   // UIViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"Pop"];

  //  [self dismissViewControllerAnimated:YES completion:Nil];
//}


#pragma mark - Getters & Setters
- (UIViewController *)en_popupViewController
{
    return objc_getAssociatedObject(self, ENPopupViewControllerPropertyKey);
}

- (void)setEn_popupViewController:(UIViewController *)en_popupViewController
{
    objc_setAssociatedObject(self, ENPopupViewControllerPropertyKey, en_popupViewController, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

#pragma mark - View Handling
- (void)presentPopUpView:(UIView *)popUpView completion:(void (^)(void))completionBlock
{
    UIView *sourceView = [self topView];
    // Check if source view controller is not in destination
    if ([sourceView.subviews containsObject:popUpView]) return;

    // Add overlay
    UIView *overlayView = [[UIView alloc] initWithFrame:sourceView.bounds];
    overlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    overlayView.tag = kENPopUpOverlayViewTag;
    overlayView.backgroundColor = [UIColor clearColor];

    // Add Blured View
    JWBlurView *bluredView = [[JWBlurView alloc] initWithFrame:overlayView.bounds];
    bluredView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    bluredView.tag = kENPopUpBluredViewTag;
    [bluredView setBlurAlpha:.0f];
    [bluredView setAlpha:.0f];
    [bluredView setBlurColor:[UIColor clearColor]];
    bluredView.backgroundColor = [UIColor clearColor];
    [overlayView addSubview:bluredView];

    // Make the background clickable
    UIButton * dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
    dismissButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    dismissButton.backgroundColor = [UIColor clearColor];
    dismissButton.frame = sourceView.bounds;
    [overlayView addSubview:dismissButton];

    [dismissButton addTarget:self action:@selector(dismissPopUpViewController)
            forControlEvents:UIControlEventTouchUpInside];

    // Customize popUpView
    popUpView.layer.cornerRadius = 3.5f;
    popUpView.layer.masksToBounds = YES;
    popUpView.layer.zPosition = 99;
    popUpView.tag = kENPopUpViewTag;
    popUpView.center = overlayView.center;
    [popUpView setNeedsLayout];
    [popUpView setNeedsDisplay];

    [overlayView addSubview:popUpView];
    [sourceView addSubview:overlayView];

    [self setAnimationStateFrom:popUpView];
    [self performAppearAnimationWithBlurView:bluredView popupView:popUpView completion:completionBlock];
}



#pragma mark - Animation
- (void)setAnimationStateFrom:(UIView *)view
{
    CALayer *layer = view.layer;
    layer.transform = [self transform3d];
}

- (CATransform3D)transform3d
{
    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DTranslate(transform, 0, 200.f, 0);
    transform.m34 = 1.0/800.0;
    transform = CATransform3DRotate(transform, kRotationAngle*M_PI/180.f, 1.f, .0f, .0f);
    CATransform3D scale = CATransform3DMakeScale(.7f, .7f, .7f);
    return CATransform3DConcat(transform, scale);
}

- (void)performAppearAnimationWithBlurView:(JWBlurView *)blurView popupView:(UIView *)popupView completion:(void (^)(void))completionBlock
{

    CATransform3D transform;
    transform = CATransform3DIdentity;
    [UIView animateWithDuration:kAnimationDuration
                     animations:^ {
                         [self.en_popupViewController viewWillAppear:NO];
                         [blurView setAlpha:1.f];
                         popupView.layer.transform = transform;
                     }
                     completion:^(BOOL finished) {
                         [self.en_popupViewController viewDidAppear:NO];
                         if (completionBlock != nil) {
                             completionBlock();
                         }
                     }];
}


- (void)performDismissAnimationInSourceView:(UIView *)sourceView
                               withBlurView:(JWBlurView *)blurView
                                  popupView:(UIView *)popupView
                                overlayView:(UIView *)overlayView
                                 completion:(void (^)(void))completionBlock
{
    CATransform3D transform = [self transform3d];
    [UIView animateWithDuration:kAnimationDuration
                     animations:^ {
                         [self.en_popupViewController viewWillDisappear:NO];
                         [blurView setAlpha:0.f];
                         popupView.layer.transform = transform;
                     }
                     completion:^(BOOL finished) {
                         [popupView removeFromSuperview];
                         [blurView  removeFromSuperview];
                         [overlayView  removeFromSuperview];
                         [self.en_popupViewController viewDidDisappear:NO];
                         self.en_popupViewController = nil;
                         if (completionBlock != nil) {
                             completionBlock();
                         }
                     }];
}

#pragma mark - Getters

- (UIView*)topView {
    UIViewController *recentView = self;
    while (recentView.parentViewController != nil) {
        recentView = recentView.parentViewController;
    }
    return recentView.view;
}


@end

我是 ios.Some 的初学者,请提前为我的 problem.Thanks 提供一些解决方案!

我更改了 UIViewController+ENPopUp.m 的代码以获得一个可见的按钮。

在代码中存在一个具有屏幕大小的按钮,用于关闭视图。我改变了这个按钮,现在他是可见的。

presentPopUpView UIViewController+ENPopUp.m 中有这条评论“ // 使背景可点击” 我将代码更改为:

CGFloat buttonSize = 10;

UIButton * dismissButton = [[UIButton alloc] init];
dismissButton.backgroundColor = [UIColor redColor];
dismissButton.frame = CGRectMake((self.view.frame.size.width/2) - (buttonSize/2),(self.view.frame.size.height/2) + (self.view.frame.size.height/4) , buttonSize, buttonSize);

[overlayView addSubview:dismissButton];

[dismissButton addTarget:self action:@selector(dismissPopUpViewController)
            forControlEvents:UIControlEventTouchUpInside];

这将创建一个 10x10 的红色按钮,如图所示:

现在,您可以根据需要更改按钮的颜色和大小。

希望对您有所帮助