您现在的位置是:首页 > 文章详情

iOS:转载:UIControl的使用

日期:2017-06-28点击:326

 

主要功能:

UIContol(控件是所有控件的基类 如:(UIButton)按钮主要用于与用户交互,通常情况下我们不会直接使用UIControl,而是子类化它。 

常用属性:

BOOL enabled

 空间默认是启用的,要禁用控件,可以将enabled属性设置为NO,这样将导致控件会略任何触摸控件事件。被禁用后,控件还可以用不同的方式显示自己, 

比如变成灰色不可用。由空间的子类完成的,这个属性却存在于UIControl中。

实例演示:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(40, 60, 100, 60)]; button.backgroundColor = [UIColor orangeColor]; button.enabled = NO; [button addTarget:self action:@selector(onButton) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; - (void)onButton{ NSLog(@"button被点击"); }//不会有输出结果,因为button的事件被禁用。

BOOL selected;

当用户选中控件时,UIControl类会将其selected属性设置为YES。子类有时使用这个属性来让其选择自身,或者来表现不同的行为方式。 

布局方式:

UIControlContentVerticalAlignment:垂直对齐方式

1.UIControlContentVerticalAlignmentCenter   //居中
2.UIControlContentVerticalAlignmentTop       //置顶
3.UIControlContentVerticalAlignmentBottom  //底部
4.UIControlContentVerticalAlignmentFill       //填充全部

UIControlContentHorizontalAlignment:水平对齐方式

1.UIControlContentHorizontalAlignmentCenter  //居中
2.UIControlContentHorizontalAlignmentLeft     //居左
3.UIControlContentHorizontalAlignmentRight   //居右
4.UIControlContentHorizontalAlignmentFill       //填充全部

UIControl介绍(二)

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

UIControl类的提供的标准机制,来进行事件登记和接收。这可以指定控件在发生特定事件时,通知代理的一个方法。此方法用于注册一个事件,事件可以用逻辑OR合并在一起,因此可以在一次单独的addTarget调用中指定多个事件,下列事件为基类UIControl所支持,除非另有说明,也是用所有控件:

UIControlEventTouchDown

单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候触发 
例如:[button addTarget:self action:@selector(onButton) forControlEvents:UIControlEventTouchDown];

UIControlEventTouchDownRepeat

多线触摸按下事件,点触计数大于1:用户按下两根手指以上时候触发

UIControlEventTouchDragInside

当一次触摸在控件窗口内拖动时

UIControlEventTouchDragUotside

当一次触摸在控件窗口之外拖动时

UIControlEventTouchDragEnter

当一次触摸从控件窗口之外拖动到内部时

UIControlEventTouchExit

当以一次触摸从控件窗口内部拖动到外部时

UIControlEventTouchUpInside

所有在控件之内触摸抬起事件

UIControlEventTouchOutside

所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)

UIControlEventTouchCancel

所有触摸取消事件,即依稀触摸因为放上了太多手指而被取消或者被上锁或者电话呼叫打断

UIControlEventTouchChanged

当控件的值发生改变时,发送通知。用于滑块,分段控件以及其他取值的控件

UIControlEventEditingDidBegin

当文本控件中开始编辑是发送通知

UIControlEventEditingChanged

当文本控件的文本被改变时发送通知

UIControlEventEditingDidEnd

当文本控件编辑结束时发送通知

UIControlEventEditingDidOnEit

当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知

UIControlEventEditingEvents

通知所有关于文本编辑的事件

UIControlEventAllEvents

通知所有事件

UIControl介绍(三)

常用方法

- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

删除一个或多个事件的响应动作,可以是使用UIControl类的removeTarget.使用nil值就可以将给定事件的目标所有动作删除:

注册事件的时候,方法签名有两种:

  • -(void)onEventMethod:(UIControl *)sender;
  • -(void)onEventMethod;
程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!


本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4764774.html,如需转载请自行联系原作者
原文链接:https://yq.aliyun.com/articles/362760
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章