// UIButton 是一个按钮控件,负责处理用户的触摸事件
- (void)aboutButton{
//1.实例化一个button
#if 0
//手动内存管理 要去手动释放内存
UIButton *button = [[UIButton alloc] init];
#else
//这里和上面的实例化的内存管理有什么区别
// UIButtonTypeCustom = 0, // no button type // 无样式的,自定义的
// 用的最多的是无样式的
// UIButtonTypeSystem //系统样式NS_ENUM_AVAILABLE_IOS(7_0), // standard system button
//
// UIButtonTypeDetailDisclosure,
// UIButtonTypeInfoLight,
// UIButtonTypeInfoDark,
// 以上三种打印出来的button中间都有感叹号 用的少
// UIButtonTypeContactAdd, //打印出来的button中间有加号“+”
//这种方式进行的实例化,会自动的帮我们去做样式上的设置
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//两种实例化的方式,区别在于上面的那种,在MRC里需要手动释放内存
#endif
//设置frame
button.frame = CGRectMake(100, 100, 200, 100);
button.backgroundColor = [UIColor redColor];
//有关button 的常用的属性、方法
//0.关于Button的状态
/*
UIControlStateNormal = 0, //正常状态 写在那里,不管他
UIControlStateHighlighted = 1 << //高亮状态(所谓高亮就是点下去的状态), // used when UIControl isHighlighted is set
UIControlStateDisabled = 1 << 1,//禁用状态
UIControlStateSelected = 1 << 2,//选中状态 // flag usable by app (see below)
*/
//button的标题颜色、标题、背景图片:如果没有给其他状态设置,缺省(默认)使用正常状态的设置的颜色/标题
//1. 给button 设置标题
[button setTitle:@"我是小强" forState:UIControlStateNormal];
[button setTitle:@"xiaoQiang" forState:UIControlStateHighlighted];
[button setTitle:@"jinyong" forState:UIControlStateDisabled];
[button setTitle:@"xuanzhong" forState:UIControlStateSelected];
//button 的禁用状态,需要单独设置
//禁用状态用户不能点击
// button.enabled = NO;
//button 的选中状态也是需要单独设置
//选中状态下,点下BUtton不再是高亮状态,而是正常状态
// button.selected = YES;
//2.给button设置标题的颜色
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//3. 设置button的标题字体
//button 是由Label 和其他元素组成的,其中Label负责标题展示的
//button 系统默认是18号字体
button.titleLabel.font = [UIFont systemFontOfSize:25];
//最好不要用UIbuttion.tiltleLabel.text,因为我们不知道他是在那个状态
// ==============================
//UIImage 是处理图片的类
// ==============================
#if 0
// 1. 获取图片的路径,通过方法来获取路径最好
//bundle 工程包
//前面的是图片名字,后面是资源的格式
// pathForResource 表示图片文件的名字
//ofType 图片文件的类型是png类型还是jpg类型等等
NSString *path = [[NSBundle mainBundle] pathForResource:@"back" ofType:@"png"];
NSString *path = [[NSBundle mainBudle] pathFor];
//2.读取这个路径下面的内容
NSData *data = [NSData dataWithContentsOfFile:path];
//3.用NSData 生成一张图片
// UIImage *image = [UIImage imageWithData:data];
// 方式二:
UIImage *image = [UIImage imageWithContentsOfFile:path];
#else
//通过一个图片的名字来生成image
//这种方式实例化的图片,会把图片缓存到内存中,就算没有对象使用这张图片,这张图片依然再内存中,无法释放
//适合加载小图片
//其中.png可以省略,但是,如果不是.png,那么后缀一定要加上去
UIImage *image = [UIImage imageNamed:@"back"];
#endif
//4.设置背景图片
[button setBackgroundImage:image forState:UIControlStateNormal];
//5. 设置button的标题图片
//标题图片和标题整体居中
[button setImage:[UIImage imageNamed:@"logo"] forState:UIControlStateNormal];
#warning button 使用的重点
//6. 事件交互
//给button 注册事件
//target: 接收消息的对象。
//action: 发送的消息。
/**
UIControlEventTouchDown //点下去就触发(实时性比较强)
UIControlEventTouchDownRepeat //快速点击触发
UIControlEventTouchDragInside //拖拽触发
UIControlEventTouchDragOutside //点击之后再到外面拖拽触发
UIControlEventTouchDragEnter //点击之后,拖拽到外面,然后又拖拽进去就可以触发
UIControlEventTouchDragExit
UIControlEventTouchUpInside //点下去在内部松开(常用)
UIControlEventTouchUpOutside //点下去在外部松开
UIControlEventTouchCancel //触摸取消
**/
[button addTarget:self action:@selector(showLog) forControlEvents: UIControlEventTouchDown];
//2. 添加到屏幕上显示
[self.view addSubview:button];
}
- (void)showLog{
NSLog(@"点了button了");
}
@end
网友评论