UIControl

来源:互联网 发布:ubuntu 打开anaconda 编辑:程序博客网 时间:2024/04/30 13:30
UIControl,相信大家对其并不陌生吧,比如平常最常用的UIButton就是继承自UIControl的。按照惯例,还是先来看看为什么有UIControl这个类?什么时候用到它?
 
查下文档就可以看到其是继承自UIView的,而对于其用途,官方对其有这么一段描述:
 
To observe or modify the dispatch of action messages to targets for particular events
To do this, override sendAction:to:forEvent:, evaluate the passed-in selector, target object, or UIControlEvents bit mask, and proceed as required.
 
To provide custom tracking behavior (for example, to change the highlight appearance)
To do this, override one or all of the following methods: beginTrackingWithTouch:withEvent:, continueTrackingWithTouch:withEvent:,endTrackingWithTouch:withEvent:.
简要点说,就是当你需要自定义一个类似于button的控件,也可自定义响应事件。而要这些,你必须实现相应的方法。
------------------------------------------------------------------
上面背景介绍完毕,看完之后可以大概知道,UIControl就是UIButton的基类。
所以直接使用就是个简易版的button,继承它就可以写自己的button类。

这里上代码:
<span style="font-family:Microsoft YaHei;font-size:14px;">- (instancetype)initWithFrame:(CGRect)frame {    self = [super initWithFrame:frame];    if (self) {        UIControl* control=[[UIControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];        [control addTarget:self action:@selector(clickbackGroud:) forControlEvents:UIControlEventTouchUpInside];        control.backgroundColor=[UIColor blackColor];        control.alpha=0.8;        [self addSubview:control];        //可以在这个self上添加如tableview等等试图,形成不可点击的空间</span><p style="margin-top: 0px; margin-bottom: 0px;"><span style="white-space: pre;"><span style="font-family:Microsoft YaHei;"></span></span><pre name="code" class="objc"><span style="font-family:Microsoft YaHei;font-size:14px;"><span style="white-space: pre;"></span>alertLabel =[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];        [alertLabel setText:@"测试测试测试"];        [self addSubview:alertLabel];</span>

<span style="font-family:Microsoft YaHei;font-size:14px;">    }    return self;}- (void)clickbackGroud:(UIControl*)sender{    [self removeFromSuperview];}</span>

上述的self的view是一个alert的view,是给superview做alert用的 
在这个self<span style="font-family: 宋体;">上添加如tableview等等试图,形成不可点击的空间</span>
这个uicontrol的作用既是点击不可点击区域外的时候就将alertview清除(如坐标10,10宽高100,100的区域)。
0 0
原创粉丝点击