热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

AlertView.h#import<UIKitUIKit.h>@protocolAlertViewDelegate<


AlertView.h


#import


@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





推荐阅读
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • vue使用
    关键词: ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • C# WPF自定义按钮的方法
    本文介绍了在C# WPF中实现自定义按钮的方法,包括使用图片作为按钮背景、自定义鼠标进入效果、自定义按压效果和自定义禁用效果。通过创建CustomButton.cs类和ButtonStyles.xaml资源文件,设计按钮的Style并添加所需的依赖属性,可以实现自定义按钮的效果。示例代码在ButtonStyles.xaml中给出。 ... [详细]
author-avatar
辽河儿女
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有