IOS development-UI (2) Button and Image, ios-ui

Source: Internet
Author: User

IOS development-UI (2) Button and Image, ios-ui

 

Knowledge point:

1. UIButton usage and event mechanism

2. UIImage

3. Custom UIButton

 

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

UIButton

 

1. Creation Method

Button type

UIButtonTypeCustom Custom button

UIButtonTypeRoundedRect system button

UIButtonTypeDetailDisclosure more info button I

UIButtonTypeInfoLight highlighted button I

UIButtonTypeInfoDark dark information button I

UIButtonTypeContactAdd plus button +

 

UIButton * b2 = [UIButton buttonWithType: UIButtonTypeCustom];

 

2. Set the button title

-(Void) setTitle :( NSString *) title forState :( UIControlState) state;

[B setTitle: @ "Click recording" forState: UIControlStateNormal];

UIControlState:

General status of the UIControlStateNormal button

The UIControlStateHighlighted button is highlighted.

 

The UIControlStateDisabled button is disabled.

UIControlStateSelected button selected

 

// Normal

[B setTitle: @ "not selected" forState: UIControlStateNormal];

// Select the status

[B setTitle: @ "select" forState: UIControlStateSelected];

// Disabled

[B setTitle: @ "disabled" forState: UIControlStateDisabled];

 

// Unselected status = NO

// Select = YES

// Btn. selected =! Btn. selected;

// Disable

Btn. enabled = NO;

 

3. Set the button color

1) set button color

-(Void) setTitleColor :( UIColor *) color forState :( UIControlState) state;

 

 

4. event handling

1) iOS event Classification

Touch events (single point, multi-touch, and various gesture operations)

Sensor events (gravity, accelerometer, etc)

Remote Control event (Remote Control of iOS device multimedia playback, etc)

 

2) add events

-(Void) addTarget :( id) target

Action :( SEL) action

ForControlEvents :( UIControlEvents) controlEvents

[B2 addTarget: self action: @ selector (btnAction :) forControlEvents: UIControlEventTouchUpInside];

 

3) delete an event

-(Void) removeTarget :( id) target

Action :( SEL) action

ForControlEvents :( UIControlEvents) controlEvents

 

5. Common events: UIControlEvents

UIControlEventTouchDown

UIControlEventTouchUpInside button

UIControlEventTouchUpOutside

[B addTarget: self action: @ selector (btnAction :) forControlEvents: UIControlEventTouchUpInside];

 

6. UIView Tag: setTag

View1.tag= 100;

 

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

UIImage

 

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

The premise of using this method is that the image must already exist in the project. If the image needs to be displayed multiple times, it will consume the system cache space)

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

 

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

When a file is loaded using this method, the system does not load the image into the program as data,

This method is recommended if you do not need to reuse this image or a large image.

 

UIImage * image2 = [UIImage imageWithContentsOfFile: filePath]

 

Obtain the resource path of a project

NSString * path = [[NSBundle mainBundle] pathForResource: @ "Logo" ofType: @ "png"];

 

NSString * filePath = [[NSBundle mainBundle] pathForResource: @ "3_selected" ofType: @ "png"];

 

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

Custom UIButton

 

1. Image button

1) set the button background image

-(Void) setBackgroundImage :( UIImage *) image forState :( UIControlState) state;

// Set the btn background image (normal)

[B setBackgroundImage: image1 forState: UIControlStateNormal];

 

2) set button images

-(Void) setImage :( UIImage *) image forState :( UIControlState) state;

// Set the picture displayed in btn (normal)

[B setImage: image1 forState: UIControlStateNormal];

[B setImage: image2 forState: UIControlStateSelected];

// Set the picture displayed in btn (highlighted)

[B setImage: image2 forState: UIControlStateHighlighted];

 

2. Implementation Process

1) The buttonType is UIButtonTypeCustom.

// Instantiate btn

UIButton * B = [UIButton buttonWithType: UIButtonTypeCustom];

 

2) set image: setImage: forState: image File Name

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

 

Related Article

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.