UIButton事件机制
结合空模板, ViewController
button的状态和一些常用定制
buttonType 注意iOS7之后,UIButtonTypeRoundedRect 无效果
title
UIControlState
titleColor
tintColor
currentTitle
setBackgroundImage:forState:
setImage:forState:
layer.cornerRadius
layer.borderColor
layer.borderWidth
其他注意事项:
1. button add 多个target
2. 系统样式 buttonWithType:UIButtonTypexxx
3. titleLabel
4. showTouchWhenHighlighted
5. enabled 效果:点一次label计数加1,点到10不能点
6. image @2x
UIImage:
iPhone 1/3G/3GS 320*480点 <--> 320*480像素 GoogleIcon.png [100*100pixel]
iPhone 5/5c/5s 320*568点 <--> 640*1136像素
[email protected] [200*200pixel]
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(20, 20, 100, 100);
[button setImage:[UIImage imageNamed:@"GoogleIcon.png"]];
根据分辩率的不同,自动选择图片。
iPhone 1: GoogleIcon.png
iPhone 5/5s:
[email protected]
7. button addTarget:self ....内存注意事项
// add target/action for particular event. you can call this multiple times and you can specify multiple target/actions for a particular event.
// passing in nil as the target goes up the responder chain. The action may optionally include the sender and the event in that order
// the action cannot be NULL. Note that the target is not retained.
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
8. NSTimer示例

9. 飘落的雪花
button chess

- (void)buttonClick:(UIButton *)button {
if ([button currentTitle]) {
_lastSelectedButton = button;
} else if (_lastSelectedButton && [button currentTitle] == nil) {
CGRect frame = button.frame;
button.frame = _lastSelectedButton.frame;
_lastSelectedButton.frame = frame;
[self.view bringSubviewToFront:button];
[self.view bringSubviewToFront:_lastSelectedButton];
_lastSelectedButton = nil;
}
}
//button,是我们当前点击的button
// 这样也可以
- (void)buttonClick:(UIButton*)button
{
if(button.currentTitle.length > 0)
{
//判断当前选中的按钮上文字的长度,如果 > 0认为有文字
//记录当前点击的button
_currentMoveButton = button;
}
//移动的条件
//1:当前可以移动的button不为空
//2:要移动到一个标题为空的位置
if (_currentMoveButton != nil && button.currentTitle.length == 0) {
//交换二个button的位置
CGRect rect = _currentMoveButton.frame;
_currentMoveButton.frame = button.frame;
button.frame = rect;
//把button提到最前面,防止被盖掉
[self.window bringSubviewToFront:_currentMoveButton];
[self.window bringSubviewToFront:button];
}
}
作业:计算器