您的位置:首页 > 其它

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

2015-12-16 17:09 387 查看
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(@"点在外面");

        

    }

    

cdd3
}

// 移除视图

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

}

// 点击button和view以外区域的回调方法

- (void)alertViewButtonClick {

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

    

}

- (void)buttonAction {

    

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

                                                             , (MainScreenHitght - 
MainScreenWidth /
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息