1,按钮的创建
(1)按钮有下面四种类型:
UIButtonType.ContactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.DetailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.System:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.Custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
UIButtonType.InfoDark:为感叹号“!”圆形按钮
UIButtonType.InfoLight:为感叹号“!”圆形按钮
1
2
3
4
5
6
7
|
var button: UIButton = UIButton .buttonWithType( UIButtonType . ContactAdd ) as UIButton ;
button.frame= CGRectMake (10, 150, 100, 30);
button.setTitle( "按钮" , forState: UIControlState . Normal )
self .view.addSubview(button);
|
(2)对于Custom定制类型按钮,代码可简化为:
1
|
var button = UIButton (frame: CGRectMake (10, 150, 100, 30))
|
2,按钮的文字设置
1
2
3
|
button.setTitle( "普通状态" , forState: UIControlState . Normal )
button.setTitle( "触摸状态" , forState: UIControlState . Highlighted )
button.setTitle( "禁用状态" , forState: UIControlState . Disabled )
|
3,按钮文字颜色的设置
1
2
3
|
button.setTitleColor( UIColor .blackColor(),forState: . Normal )
button.setTitleColor( UIColor .greenColor(),forState: . Highlighted )
button.setTitleColor( UIColor .grayColor(),forState: . Disabled )
|
4,按钮文字阴影颜色的设置
1
2
3
|
button.setTitleShadowColor( UIColor .greenColor(),forState:. Normal )
button.setTitleShadowColor( UIColor .yellowColor(),forState:. Highlighted )
button.setTitleShadowColor( UIColor .grayColor(),forState:. Disabled )
|
5,按钮背景颜色设置
1
|
button.backgroundColor= UIColor .blackColor()
|
6,按钮文字图标的设置
1
2
3
|
button.setImage( UIImage (named: "icon1" ),forState:. Normal )
button.adjustsImageWhenHighlighted= false
button.adjustsImageWhenDisabled= false
|
7,设置按钮背景图片
1
|
button.setBackgroundImage( UIImage (named: "background1" ),forState:. Normal )
|
8,按钮触摸点击事件响应
1
2
3
4
5
6
7
8
9
10
11
|
button.addTarget( self ,action: Selector ( "tapped" ),forControlEvents: UIControlEvents . TouchUpInside )
func tapped(){
println ( "tapped" )
}
button.addTarget( self ,action: Selector ( "tapped:" ),forControlEvents: UIControlEvents . TouchUpInside )
func tapped(button: UIButton ){
println (button.titleForState(. Normal ))
}
|
常用的触摸事件类型:
TouchDown:单点触摸按下事件,点触屏幕
TouchDownRepeat:多点触摸按下事件,点触计数大于1,按下第2、3或第4根手指的时候
TouchDragInside:触摸在控件内拖动时
TouchDragOutside:触摸在控件外拖动时
TouchDragEnter:触摸从控件之外拖动到内部时
TouchDragExit:触摸从控件内部拖动到外部时
TouchUpInside:在控件之内触摸并抬起事件
TouchUpOutside:在控件之外触摸抬起事件
TouchCancel:触摸取消事件,即一次触摸因为放上太多手指而被取消,或者电话打断
请发表评论