IOS UIButton Usage Detailed

Source: Internet
Author: User

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
  1. UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];
  2. //btn.frame = Rect_screen;
  3.     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);   
  4. BTN. backgroundcolor = [Uicolor bluecolor];
  5. //Uicontroleventtouchdraginside
  6. //Uicontroleventtouchdragoutside
  7. [Btn addTarget:self Action:@selector (draginside) forcontrolevents:      Uicontroleventtouchdraginside];
  8. [Btn addTarget:self Action:@selector (dragoutside) forcontrolevents:      Uicontroleventtouchdragoutside];
  9. //Dismissview
  10. [Btn addTarget:self Action:@selector (upinside) Forcontrolevents:uicontroleventtouchupinside]      ;
  11. [self addsubview:btn];
  12. return self ;
  13. }
  14. -(void) Draginside
  15. {
  16. NSLog (@ "Draginside ...");
  17. }
  18. -(void) Dragoutside
  19. {
  20. NSLog (@ "Dragoutside ...");
  21. }
  22. -(void) Upinside
  23. {
  24. NSLog (@ "Upinside ...");
  25. }

Long Press Events

[OBJC]View Plaincopy
  1. UIButton *abtn=[uibutton Buttonwithtype:uibuttontypecustom];
  2. [Abtn Setframe:cgrectmake (40, 100, 60, 60)];
  3. [Abtn setbackgroundimage:[uiimage imagenamed:@ "111.png"]forstate:uicontrolstatenormal];
  4. //button Click events
  5. [Abtn addTarget:self Action:@selector (btnshort:) forcontrolevents:uicontroleventtouchupinside];
  6. //button Long Press events
  7. Uilongpressgesturerecognizer *longpress = [[Uilongpressgesturerecognizer alloc] initwithtarget:   Selfaction:@selector (Btnlong:)];
  8. Longpress. minimumpressduration = 0. 8; //define the time to press
  9. [Abtn addgesturerecognizer:longpress];
  10. -(void) Btnlong: (uilongpressgesturerecognizer*) gesturerecognizer{
  11. if ([Gesturerecognizer state ] = = Uigesturerecognizerstatebegan) {
  12. NSLog (@ "Long press event");
  13. Uialertview *alert=[[uialertview alloc]initwithtitle:@ "Messages" message :@ "OK to delete this mode?"  " Delegate:selfcancelbuttontitle:@" Cancel " otherbuttontitles:@" Delete ", nil nil];
  14. [Alert Show];
  15. }
  16. }

IOS UIButton Usage Detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.