您的位置:首页 > 产品设计 > UI/UE

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  宇宙黑客王磊磊