自定义View弹出,周围变暗,点击view以外的区域,view消失及变亮。

本文将指导您如何使用SwiftUI框架快速创建iOS应用,包括界面设计、数据绑定和响应式编程等关键概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


AlertView.h


#import <UIKit/UIKit.h>


@protocol AlertViewDelegate <NSObject>


- (void)alertViewButtonClick ;


@end


@interface AlertVIew : UIView


- (void)diss;// 视图消失

@property (nonatomic, assign) id<AlertViewDelegate>delegate;



@end



AlertView.m


#import "AlertVIew.h"


#define KLeftWitdh 10


@interface AlertVIew ()


@property (nonatomic, strong) UITapGestureRecognizer * tapGesture;


@end


@implementation AlertVIew


-(instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

     

        UIWindow * window = [UIApplication sharedApplication].delegate.window;

        // 如果手势没有值

        if (!self.tapGesture) {

            self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];

        }

        

        // 如果不包含这个手势

        if (![window.gestureRecognizers containsObject:self.tapGesture]) {

            [window addGestureRecognizer:self.tapGesture];

        }

        

        

        UITextField * textFiled = [[UITextField alloc] initWithFrame:CGRectMake(KLeftWitdh

                                                                                , KLeftWitdh

                                                                                , CGRectGetWidth(self.frame) - KLeftWitdh * 2

                                                                                , (CGRectGetHeight(self.frame) - KLeftWitdh *4) / 3)];

        textFiled.backgroundColor = [UIColor redColor];

        [self addSubview:textFiled];

        

        UITextField * textField2 = [[UITextField alloc] initWithFrame:CGRectMake(KLeftWitdh

                                                                                 , KLeftWitdh * 2 + CGRectGetHeight(textFiled.frame)

                                                                                 , CGRectGetWidth(textFiled.frame)

                                                                                 , CGRectGetHeight(textFiled.frame))];

        textField2.backgroundColor = [UIColor redColor];

        [self addSubview:textField2];

        

        UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];

        button.frame = CGRectMake(KLeftWitdh

                                  , KLeftWitdh * 3 + CGRectGetHeight(textFiled.frame) * 2

                                  , CGRectGetWidth(self.frame) - 2 * KLeftWitdh

                                  , CGRectGetHeight(textFiled.frame));

        [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];

        [button setTitle:@"保存" forState:UIControlStateNormal];

        button.backgroundColor = [UIColor blueColor];

        [self addSubview:button];

        

    }

    

    return self;

}


- (void)tapGesture:(UIGestureRecognizer *)gesture {

    NSLog(@"点击了");

    if (gesture.state == UIGestureRecognizerStateEnded) {

        

        // 获取触摸点在window上的坐标

        CGPoint touchPoint = [gesture locationInView:nil];

        // 转换成以alertView为基准的坐标

        CGPoint converPoint = [self convertPoint:touchPoint fromView:self];

        // 判断是否点在alertView之外

        if (!CGRectContainsPoint(self.frame, converPoint)) {

            NSLog(@"不点在AlertView里面");

            if (_delegate) {

                [_delegate alertViewButtonClick];

                [self diss];

                

                UIWindow * window = [UIApplication sharedApplication].delegate.window;

                [window removeGestureRecognizer:self.tapGesture];

                

                return;

            }

        }

        

        NSLog(@"点在外面");

        

    }

    

}


// 移除视图

- (void)diss {

    

    [self removeFromSuperview];

}


// 保存按钮

- (void)buttonAction {

    if (_delegate) {

        [_delegate alertViewButtonClick];

        [self diss];

    }

}



@end


#import "ViewController.h"

#import "AlertVIew.h"


#define MainScreenWidth [UIScreen mainScreen].bounds.size.width

#define MainScreenHitght [UIScreen mainScreen].bounds.size.height


@interface ViewController ()<AlertViewDelegate>


@property (nonatomic, strong) AlertVIew * alertView;



@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    

    

    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.center = CGPointMake(CGRectGetWidth(self.view.frame) / 2, CGRectGetHeight(self.view.frame) / 2);

    button.bounds = CGRectMake(0, 0, 100, 100);

    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];

    button.backgroundColor = [UIColor redColor];

    [self.view addSubview:button];

    

    

}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self.view endEditing:YES];

}


// 点击buttonview以外区域的回调方法

- (void)alertViewButtonClick {

    self.view.alpha = 1;// 改变透明度模拟变亮

    

}



- (void)buttonAction {

    

    _alertView = [[AlertVIew alloc] initWithFrame:CGRectMake((MainScreenWidth - MainScreenWidth / 1.5) / 2

                                                             , (MainScreenHitghtMainScreenWidth / 1.5 / 3.0) / 2

                                                             , MainScreenWidth / 1.5

                                                             , MainScreenWidth / 1.5 / 3.0)];

    _alertView.backgroundColor = [UIColor yellowColor];

    self.view.alpha = 0.5;// 改变透明度模拟变暗

    _alertView.delegate = self;

    [self.view.window addSubview:_alertView];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值