UIButton基础以及使用block+UIButton处理点击事件
2016-01-21 22:24
363 查看
// UIButton * btn = [[UIButton alloc] init];//custom类型,一般不使用 //UIButtonTypeSystem 只能得到图片的轮廓,不能得到纹理 UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; //不能这么设置按钮颜色 // btn.titleLabel.text = @"123"; /* ypedef NS_OPTIONS(NSUInteger, UIControlState) { UIControlStateNormal = 0, // 常规状态显现 UIControlStateHighlighted = 1 << 0, // 高亮状态显现 UIControlStateDisabled = 1 << 1, // 禁用的状态才会显现 UIControlStateSelected = 1 << 2, // 选中状态 UIControlStateApplication = 0x00FF0000, // 当应用程序标志时 UIControlStateReserved = 0xFF000000 //为内部框架预留 // 后两个可以不管他 }; */ [btn setTitle:@"正常按钮" forState:UIControlStateNormal]; [btn setTitle:@"高亮状态" forState:UIControlStateHighlighted]; [btn setTitle:@"禁用状态" forState:UIControlStateDisabled]; [btn setTitle:@"选中状态" forState:UIControlStateSelected]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; [btn setTitleColor:[UIColor greenColor] forState:UIControlStateDisabled]; [btn setTitleColor:[UIColor magentaColor] forState:UIControlStateSelected]; [btn setImage:[UIImage imageNamed:@"apply_sex_normal"] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@"apply_sex_selected"] forState:UIControlStateSelected]; [btn setBackgroundImage:[UIImage imageNamed:@"beijing"] forState:UIControlStateSelected]; btn.backgroundColor = [UIColor grayColor]; //设置字体 btn.titleLabel.font = [UIFont systemFontOfSize:20]; // NSDictionary * dict = @{NSFontAttributeName:[UIFont systemFontOfSize:30]}; // NSAttributedString * att = [[NSAttributedString alloc] initWithString:@"Attributed" attributes:dict]; // // [btn setAttributedTitle:att forState:UIControlStateNormal]; //tintColor只有在systemtype时有效,tintColor是子视图及以上视图的颜色。 // btn.tintColor = [UIColor orangeColor]; //开启禁用状态 // btn.enabled = NO; //开启选中状态 // btn.selected = YES; /* iOS UIButton事件: UIControlEventTouchDown 单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。 UIControlEventTouchDownRepeat 多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。 UIControlEventTouchDragInside 当一次触摸在控件窗口内拖动时。 UIControlEventTouchDragOutside 当一次触摸在控件窗口之外拖动时。 UIControlEventTouchDragEnter 当一次触摸从控件窗口之外拖动到内部时。 UIControlEventTouchDragExit 当一次触摸从控件窗口内部拖动到外部时。 UIControlEventTouchUpInside 所有在控件之内触摸抬起事件。 UIControlEventTouchUpOutside 所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。 UIControlEventTouchCancel 所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。 UIControlEventTouchChanged 当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。 UIControlEventEditingDidBegin 当文本控件中开始编辑时发送通知。 UIControlEventEditingChanged 当文本控件中的文本被改变时发送通知。 UIControlEventEditingDidEnd 当文本控件中编辑结束时发送通知。 UIControlEventEditingDidOnExit 当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。 UIControlEventAlltouchEvents 通知所有触摸事件。 UIControlEventAllEditingEvents 通知所有关于文本编辑的事件。 UIControlEventAllEvents 通知所有事件。 */ //添加响应事件 [btn addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside]; // [btn removeTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside]; // [btn addTarget:self action:@selector(doAction2) forControlEvents:UIControlEventTouchUpOutside]; // [btn addTarget:self action:@selector(doAction3) forControlEvents:UIControlEventAllTouchEvents]; // btn.contentEdgeInsets = UIEdgeInsetsMake(50, 50, 50, 50); btn.titleEdgeInsets = UIEdgeInsetsMake(50, 0, 0, 100); btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); //点击按钮有光照效果 // btn.showsTouchWhenHighlighted = YES; //高亮状态按钮颜色是否变深 btn.adjustsImageWhenHighlighted = NO; btn.frame = CGRectMake(100, 100, 200, 200); [self.view addSubview:btn];
UIButton使用block重写点击事件
1 buttonBlock.h
#import <UIKit/UIKit.h> typedef void(^Block)(UIButton * button); @interface BlockButton : UIButton @property (nonatomic, copy) Block block; @end
2 buttonBlock
#import "BlockButton.h" @implementation BlockButton - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside]; } return self; } - (void)doAction:(UIButton *)button { self.block(button); } @end
3 在ViewController.m里面调用
#import "ViewController.h" #import "BlockButton.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; BlockButton * btn = [BlockButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(100, 100, 100, 100); [btn setTitle:@"点我" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; btn.block = ^(UIButton * button) { NSLog(@"%@",button); }; [self.view addSubview:btn]; }
相关文章推荐
- 蓝懿IOS学习核心动画和UIImagePicker
- iOS开发 UItextFielddialing方法详解
- [转] mysql_query("set names gb2312")设置客户端字符集
- leetcode之 Implement Queue using Stacks
- 改变UITextField Placehold位置颜色
- easyui-combobox 实现简单的自动补全功能
- IQueryable与IEnumerable区别
- iOS个人整理12-UIControl与子类:UIPageControl、UIStepper
- UEFI启动视频详解:启动分析+N项操作实例
- Install caffe Some questions--in ubuntu 14.04
- (一)UI AUtoMonkey:xcode 里直接执行monkey
- 1月21号 UITabBarController
- Codeforce 163 A. Substring and Subsequence DP
- 使用WebRequest类下载文件
- iOS学习__UI测试题及答案
- UI组件之ProgressBar及其子类(二)SeekBar拖动条和RatingBar星级评分条的使用
- UIImagePickerController从拍照、图库、相册获取图片
- 2016.01.21 UITabBarController
- Easyui的Dialog的toolbar的自定义添加
- NSURLRequestCachePolicy 缓存策略