IOS development UI basics-Button Basics

Source: Internet
Author: User

IOS development UI basics-Button Basics

1. Simple Description

Generally, when you click a control, all the buttons will be responded accordingly.

Buttons provide many functions, including displaying text and images, and adjusting the positions of internal images and texts at any time.

2. Three buttons

Normal)

Default)

Corresponding enumerated constant: UIControlStateNormal

 

Highlighted)

When the button is pressed (the finger is not released yet)

Corresponding enumerated constant: UIControlStateHighlighted

 

Disabled (invalid or unavailable)

If the enabled attribute is NO, it is in the disable state, indicating that the button cannot be clicked.

Corresponding enumerated constant: UIControlStateDisabled

 

Iii. Notes

(1) From Xcode5, all image resources are placed in Images. xcassets for management. You can drag and drop the Images used in the project to Images. xcassets.

(2) Multiple controls share a piece of code, usually using tags.

Iv. Sample Code

(1)

1 # import "LFViewController. h "2 3 @ interface LFViewController () 4 5 @ property (weak, nonatomic) IBOutlet UIButton * headImageView; 6 7 @ end 8 9 @ implementation LFViewController10 11 // in OC, the first parameter of the listening method of most controls is the control itself 12 //-(IBAction) left :( UIButton *) button {13 // 14 // NSLog (@ "----"); 15 //} 16-(IBAction) move17 {18 // modify the head position through frame 19 // in OC, you cannot directly modify the "member" 20 of "struct attribute" of "object" // you can modify the "struct attribute" 21 of "object" // 1. retrieve the struct attribute 22 CGRect rect = self. headImageView. frame; 23 // 2. modify the structure member 24 rect. origin. y-= 20; 25 // 3. set the object's struct attribute 26 self. headImageView. frame = rect; 27}

(2)

1 # import "LFViewController. h "2 3/** 4 Use git 5 6 1. select git 7 2 when creating a project. development lags behind. Select "Source Control" "Commit" and write comments 8 */9 10 11 // The enumerated type is essentially an integer, it is used to replace the magic number 12 // In the enumeration type. After the first integer is specified, the subsequent number will increase by 13 typedef enum14 {15 kMovingDirTop = 10, 16 kMovingDirBottom, 17 kMovingDirLeft, 18 kMovingDirRight, 19} kMovingDir; 20 21 # define kMovingDelta 5022 23 @ interface LFViewController () 24 25 @ property (weak, nonatomic) IBOutlet UIButton * headImageView; 26 27 @ end28 29 @ implementation LFViewController30 31-(IBAction) move :( UIButton *) button32 {33 // CGRect rect = self. headImageView. frame; 34 CGPoint p = self. headImageView. center; 35 36 // magic number. When other programmers see the code, they do not know what it means. tag) {38 case kMovingDirTop: 39 p. y-= kMovingDelta; 40 break; 41 case kMovingDirBottom: 42 p. y + = kMovingDelta; 43 break; 44 case kMovingDirLeft: 45 p. x-= kMovingDelta; 46 break; 47 case kMovingDirRight: 48 p. x + = kMovingDelta; 49 break; 50} 51 52 [UIView beginAnimations: nil context: nil]; 53 [UIView setanimation Duration: 1.0]; 54 55 self. headImageView. center = p; 56 57 [UIView commitAnimations]; 58} 59 60-(IBAction) zoom :( UIButton *) button61 {62 CGRect rect = self. headImageView. bounds; 63 64 // in C language, bool is judged to be true if (button. tag) {66 rect. size. width + = 50; 67 rect. size. height + = 50; 68} else {69 rect. size. width-= 50; 70 rect. size. height-= 50; 71} 72 73 // start and end animation 74 // beginAnimations indicates that the subsequent code will be "involved" in the animation 75 [UIView beginAnimations: nil context: nil]; 76 [UIView setAnimationDuration: 2.0]; 77 78 self. headImageView. bounds = rect; 79 // self. headImageView. alpha = 0; 80 81 // commitAnimations: Submit all animations after ininanimation and generate animations 82 [UIView commitAnimations]; 83} 84 85 @ end

 

5. Add notes

1. IBAction Parameters

-(IBAction) left :( UIButton *) button

(1) In OC, the first parameter of most control listening methods is the control itself.

(2) The parameter type of the default link is id.

(3) If you want to facilitate the use of the control in the monitoring method, you can modify the parameter type of the monitoring method during or after the connection.

 

2. Modify the struct member of an object

In OC, it is not allowed to directly modify the "member" of "structure attribute" of "object", but to modify the "structure attribute" of "object"

You can modify the struct attributes as follows:

(1) use temporary variables to record the object's struct attributes

(2) modifying the properties of temporary variables

(3) reset the temporary variables to the object's struct attributes.

 

3. Avoid the occurrence of Magic Number during program development)

Use the enumeration type to avoid the occurrence of magic numbers in the program.

(1) The Enumeration type is essentially an integer, which is used to replace magic numbers.

(2) In the enumeration type, after the first integer is specified, the following numbers increase progressively.

 

4. frame & bounds & center

1> frame can modify the position and size of an object.

2> bounds can modify the object size.

3> the center can modify the object location.

 

5. First-end Animation

// BeginAnimations indicates that the subsequent code will be "involved" in the animation

[UIView beginAnimations: nil context: nil];

// SetAnimationDuration is used to specify the animation duration.

[UIView setAnimationDuration: 2.0];

Self. headImageView. bounds = rect;

......

// CommitAnimations: Submit and generate all animations after beginAnimation

[UIView commitAnimations];

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.