1, the creation of the button
(1) There are four types of buttons: Uibuttontype.contactadd: Front with "+" icon button, the default text color is blue, with touch when the highlight effectuibuttontype.detaildisclosure: Front with "!" icon button, default text color is blue, with touch highlighting effectuibuttontype.system: Front without icon, default text color is blue, with touch highlighting effectuibuttontype.custom: Custom button, front without icon, default text color is white, no touch highlighting effectUibuttontype.infodark: for exclamation mark "!" Round button uibuttontype.infolight: for exclamation mark "!" Round button
| 1234567 |
//创建一个ContactAdd类型的按钮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) for the custom custom type button, the code can be simplified to:
| 1 |
varbutton = UIButton(frame:CGRectMake(10, 150, 100, 30)) |
2, the text setting of the button
| 123 |
button.setTitle("普通状态", forState:UIControlState.Normal) //普通状态下的文字button.setTitle("触摸状态", forState:UIControlState.Highlighted) //触摸状态下的文字button.setTitle("禁用状态", forState:UIControlState.Disabled) //禁用状态下的文字 |
3, button text color settings
| 123 |
button.settitlecolor ( uicolor .blackcolor (), forstate:. normal ) //the color of text in normal state Button.settitlecolor ( uicolor .greencolor (), Forstate:. highlighted ) //touch-state text color Button.settitlecolor ( uicolor .graycolor (), Forstate:. disabled ) //the color of text in disabled state |
4, Button text shadow color settings
| 123 |
button.setTitleShadowColor(UIColor.greenColor(),forState:.Normal) //普通状态下文字阴影的颜色button.setTitleShadowColor(UIColor.yellowColor(),forState:.Highlighted) //普通状态下文字阴影的颜色button.setTitleShadowColor(UIColor.grayColor(),forState:.Disabled) //普通状态下文字阴影的颜色 |
5, button background color setting
| 1 |
button.backgroundColor=UIColor.blackColor() |
6, button text icon settings
| 123 |
button.setImage(UIImage(named:"icon1"),forState:.Normal) //设置图标button.adjustsImageWhenHighlighted=false//使触摸模式下按钮也不会变暗button.adjustsImageWhenDisabled=false//使禁用模式下按钮也不会变暗 |
7, set button background picture
| 1 |
button.setBackgroundImage(UIImage(named:"background1"),forState:.Normal) |
8, Button Touch Click event Response
| 1234567891011 |
//不传递触摸对象(即点击的按钮)button.addTarget(self,action:Selector("tapped"),forControlEvents:UIControlEvents.TouchUpInside)func tapped(){ println("tapped")}//传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号button.addTarget(self,action:Selector("tapped:"),forControlEvents:UIControlEvents.TouchUpInside)func tapped(button:UIButton){ println(button.titleForState(.Normal))} |
common types of touch events:TouchDown: Single Touch press event, touch screentouchdownrepeat: Multi-touch press event, touch count greater than 1, press 2nd, 3 or 4th fingerTouchdraginside: Touch while dragging inside the controlTouchdragoutside: Touch when dragging outside the controlTouchdragenter: Touch when dragging from outside the control to insideTouchdragexit: When touch is dragged from inside the control to the outsideTouchupinside: Touch and lift events within a controlTouchupoutside: Touch lift events outside the controlTouchcancel: Touch Cancel event, that is, one touch is canceled because it puts too many fingers, or the phone interrupts
Use of Swift-button (UIButton)