iOS Development-ui (ii) button and image

Source: Internet
Author: User

Knowledge Points:

1.UIButton usage and event mechanism

2.UIImage

3. Custom UIButton

==================

UIButton

1. How to create

Button type

Uibuttontypecustom User Custom button

Uibuttontyperoundedrect System button

Uibuttontypedetaildisclosure More Info button i

Uibuttontypeinfolight Highlight Info button i

Uibuttontypeinfodark Dark Info button i

Uibuttontypecontactadd plus button +

UIButton *b2 = [UIButton buttonwithtype:uibuttontypecustom];

2. Set the caption of the button

-(void) Settitle: (NSString *) title forstate: (uicontrolstate) state;

[B settitle:@ "Click to Record" forstate:uicontrolstatenormal];

Uicontrolstate:

UIControlStateNormal button General status

uicontrolstatehighlighted button is pressed (highlighted) status

uicontrolstatedisabled button disabled state

uicontrolstateselected button is selected

Normal state

[B settitle:@ "not selected" forstate:uicontrolstatenormal];

Checked status

[B settitle:@ "selected" forstate:uicontrolstateselected];

Disabled state

[B settitle:@ "Disable" forstate:uicontrolstatedisabled];

Unchecked State ==no

Check Status ==yes

btn.selected =!btn.selected;

Disable

btn.enabled = NO;

3. Set the color of the button

1) Set Button color

-(void) Settitlecolor: (Uicolor *) color forstate: (uicontrolstate) state;

4. Event Handling

1) iOS event category

Touch Events (single point, multi-touch, and various gesture actions)

sensor events (gravity, acceleration sensors, etc.)

Remote Control Events (remote-controlled iOS device multimedia playback, etc.)

2) Add Event

-(void) AddTarget: (ID) target

action: (SEL) Action

forcontrolevents: (uicontrolevents) controlevents

[B2 addtarget:self Action: @selector (btnaction:) Forcontrolevents:uicontroleventtouchupinside];

3) Delete Event

-(void) Removetarget: (ID) target

action: (SEL) Action

forcontrolevents: (uicontrolevents) controlevents

5. Common events: Uicontrolevents

Uicontroleventtouchdown Button Press

UIControlEventTouchUpInside Button Press to play

Uicontroleventtouchupoutside button pressed, button out of the shell

[B addtarget:self Action: @selector (btnaction:) forcontrolevents:uicontroleventtouchupinside];

6.UIView Tags: settag

View1.tag = 100;

==========================

UIImage

+ (UIImage *) imagenamed: (NSString *) name;

The premise of using this method is that the picture must already exist with the project if the picture needs to be displayed multiple times (it consumes the system cache space)

UIImage *image1 = [UIImage imagenamed:@ "3_normal"];

+ (UIImage *) UIImage imagewithcontentsoffile: (NSString *);

When you use this method to load a file, the system does not load the image into the program in a data way.

This method is recommended if you do not need to reuse the image or if it is a large image

UIImage *image2 = [UIImage Imagewithcontentsoffile:filepath]

Get the resource path in the project

NSString * Path = [[NSBundle mainbundle] pathforresource:@ "Logo" oftype:@ "PNG"];

nsstring *filepath = [[NSBundle mainbundle] pathforresource:@ "3_selected" oftype:@ "png"];

==========================

Custom UIButton

1. Picture button

1) Set Button background image

-(void) SetBackgroundImage: (UIImage *) image forstate: (uicontrolstate) state;

Set the background picture of the btn (normal state)

[b setbackgroundimage:image1 Forstate:uicontrolstatenormal];

2) Set Button picture

-(void) SetImage: (UIImage *) image forstate: (uicontrolstate) state;

Set the picture displayed by the btn (normal state)

[b setimage:image1 Forstate:uicontrolstatenormal];

[b setimage:image2 forstate:uicontrolstateselected];

Set the picture displayed by the BTN (highlighted state)

[b setimage:image2 forstate:uicontrolstatehighlighted];

2. Implementation process

1) buttontype type is Uibuttontypecustom

Instantiate BTN

UIButton *b = [UIButton buttonwithtype:uibuttontypecustom];

2) Set Picture: setImage:forState:image: The file name of the picture

UIImage *image1 = [UIImage imagenamed:@ "3_normal"];

iOS Development-ui (ii) button and image

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.