This code dynamically creates a UIButton, and lists the relevant common attributes. Hope is useful to everyone.
Here to create a rounded rectangle button
UIButton *button1 = [UIButton buttonwithtype:uibuttontyperoundedrect];
There are 6 kinds of button types that can be defined:
typedef enum {
Uibuttontypecustom = 0, custom style
Uibuttontyperoundedrect, Rounded Rectangle
Uibuttontypedetaildisclosure, Blue small arrow button, mainly to do detailed instructions with
Uibuttontypeinfolight, bright exclamation marks
Uibuttontypeinfodark, Dark exclamation
Uibuttontypecontactadd, Cross plus button
} Uibuttontype;
The position of the given button on the view
Button1.frame = CGRectMake (20, 20, 280, 40);
Button background color
Button1.backgroundcolor = [Uicolor Clearcolor];
Set Button Fill picture
[Button1 setimage:[uiimage imagenamed:@ "Btng.png"] forstate:uicontrolstatenormal];
Set Button Caption
[Button1 settitle:@ "click" Forstate:uicontrolstatenormal];
/* Forstate: The function of this parameter is to define the text of the button or the state of the picture in which it will appear */
Here are a few states
enum {
UIControlStateNormal = 0, normal state appearance
uicontrolstatehighlighted = 1 << 0, highlight state appears
uicontrolstatedisabled = 1 << 1, disabled status only appears
uicontrolstateselected = 1 << 2, check status
Uicontrolstateapplication = 0x00ff0000 When the application is marked
uicontrolstatereserved = 0xff000000 is reserved for the internal frame and can be used regardless of his
// };
/*
* By default, when the button is highlighted, the color of the image will be drawn darker, if this property below is set to No,
* Then you can remove this function
*/
button1.adjustsimagewhenhighlighted = NO;
/* As in the above case, by default, when the button is disabled, the image will be drawn deeper, set No to cancel the setting */
button1.adjustsimagewhendisabled = NO;
/* This property is set to Yes in the state, the button will glow when pressed */
button1.showstouchwhenhighlighted = YES;
/* Add an event to the button, there are a number of events, I will open a separate blog to introduce them, the following time means
Pressing the button and triggering the event when the finger leaves the screen is the same as the Click event in the Web.
After triggering this event, execute Butclick: This method, addtarget:self, means that this method in this class
You can also pass in pointers to other classes */
[Button1 addtarget:self Action: @selector (Butclick:) forcontrolevents:uicontroleventtouchupinside];
Display controls
[Self.view Addsubview:button1];
Attention:
[button1 Addtarget:self
Action: @selector (Alarmtimedone:)
Forcontrolevents:uicontroleventtouchupinside
];
Addtarget:self is linked to self, which is generally set
Action: @selector (Alarmtimedone:) Time-processing functions
Forcontrolevents:uicontroleventtouchupinside Control Event handling messages
Cancel all events that have been added to the button: (This is more important, if two events are added two events will be triggered)
[Btn Removetarget:nil Action:nil forcontrolevents:uicontroleventtouchupinside];
When is release UIButton released?
Whether to release the UIButton object in Dealloc depends on how the UIButton is initialized.
If you use [Uibuttonbuttonwithtype:uibuttontyperoundedrect] this way, you do not need to do the release operation, because this method is automatically released. If you are using [[UIButton Alloc]init], you will need to take an active release release operation.
IOS UIButton Event: Uicontroleventtouchdown
Single Touch Press event: The user touches the screen, or when a new finger falls.
Uicontroleventtouchdownrepeat
Multi-touch Press event, touch count greater than 1: when the user presses the second to third or fourth finger.
Uicontroleventtouchdraginside
When a touch is dragged within the control window.
Uicontroleventtouchdragoutside
When a touch is dragged outside the control window.
Uicontroleventtouchdragenter
When a touch is dragged from outside the control window to the inside.
Uicontroleventtouchdragexit
When a touch is dragged from inside the control window to the outside.
UIControlEventTouchUpInside
All touch lift events within the control.
Uicontroleventtouchupoutside
All touch lift events outside the control (the touch must start with the inside of the control to send a notification).
Uicontroleventtouchcancel
All touch cancellation events, that is, one touch is canceled because it has too many fingers, or is locked or interrupted by a phone call.
Uicontroleventtouchchanged
Sends a notification when the value of the control has changed. Used for sliders, segmented controls, and other controls that take values. You can configure when a slider control sends notifications when the slider is dropped, or when it is dragged.
Uicontroleventeditingdidbegin
Sends a notification when the text control starts editing.
Uicontroleventeditingchanged
A notification is sent when the text in the text control is changed.
Uicontroleventeditingdidend
Sends a notification when the edit ends in a text control.
Uicontroleventeditingdidonexit
Sends a notification when the edit is finished by pressing the ENTER key (or equivalent behavior) within the text control.
Uicontroleventalltouchevents
Notifies all touch events.
Uicontroleventalleditingevents
Notifies all events about text editing.
Uicontroleventallevents
Notifies all events.
Instance:
[OBJC]View Plaincopy
- UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];
- //btn.frame = Rect_screen;
- btn.frame = cgrectmake (frame< Span class= "xcodeconstants" >.size.width - 2< Span class= "Xcodenumber" >0.0f - 30.0f, frame.size .height - 50.0f, 30 .0f, 50.0f);
- BTN. backgroundcolor = [Uicolor bluecolor];
- //Uicontroleventtouchdraginside
- //Uicontroleventtouchdragoutside
- [Btn addTarget:self Action:@selector (draginside) forcontrolevents: Uicontroleventtouchdraginside];
- [Btn addTarget:self Action:@selector (dragoutside) forcontrolevents: Uicontroleventtouchdragoutside];
- //Dismissview
- [Btn addTarget:self Action:@selector (upinside) Forcontrolevents:uicontroleventtouchupinside] ;
- [self addsubview:btn];
- return self ;
- }
- -(void) Draginside
- {
- NSLog (@ "Draginside ...");
- }
- -(void) Dragoutside
- {
- NSLog (@ "Dragoutside ...");
- }
- -(void) Upinside
- {
- NSLog (@ "Upinside ...");
- }
Long Press Events
[OBJC]View Plaincopy
- UIButton *abtn=[uibutton Buttonwithtype:uibuttontypecustom];
- [Abtn Setframe:cgrectmake (40, 100, 60, 60)];
- [Abtn setbackgroundimage:[uiimage imagenamed:@ "111.png"]forstate:uicontrolstatenormal];
- //button Click events
- [Abtn addTarget:self Action:@selector (btnshort:) forcontrolevents:uicontroleventtouchupinside];
- //button Long Press events
- Uilongpressgesturerecognizer *longpress = [[Uilongpressgesturerecognizer alloc] initwithtarget: Selfaction:@selector (Btnlong:)];
- Longpress. minimumpressduration = 0. 8; //define the time to press
- [Abtn addgesturerecognizer:longpress];
- -(void) Btnlong: (uilongpressgesturerecognizer*) gesturerecognizer{
- if ([Gesturerecognizer state ] = = Uigesturerecognizerstatebegan) {
- NSLog (@ "Long press event");
- Uialertview *alert=[[uialertview alloc]initwithtitle:@ "Messages" message :@ "OK to delete this mode?" " Delegate:selfcancelbuttontitle:@" Cancel " otherbuttontitles:@" Delete ", nil nil];
- [Alert Show];
- }
- }
IOS UIButton Usage Detailed