Use of UIButton in ios development (1)

Source: Internet
Author: User

Use of UIButton in ios development (I) I. Simple Description generally, after clicking a control, the corresponding response is that buttons have many functions, including displaying text and images, you can also adjust the positions of internal images and text at any time. 2. The enumerated constants corresponding to the normal (normal) status (Default): UIControlStateNormal highlighted (highlighted) when the button is pressed (the finger has not been released), the corresponding enumerated constant: UIControlStateHighlighted disabled (invalid, unavailable). If the enabled attribute is NO, it is in the disable state, it indicates that the button cannot be clicked with the corresponding enumerated constant: UIControlStateDisabled 3. Note (1) that image resources are placed in Images starting from xcode5. xcassets for management, you can use the drag-and-drop Add Images used in the project to Images. xcassets (2) Multiple controls share a piece of code, usually using tags. 4. Sample Code (1) Copy code 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} copy code (2) Copy code 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 copy Code 5. add NOTES 1. IBAction parameter ================================================= ===- (IBAction) left :( UIButton *) button 1> in OC, the first parameter of most control listening methods is the control itself. 2. The parameter type of default connection is id. 3. If you want to use the control in the listening method, you can modify the parameter type of the listener method during or after the connection. modify the struct member of an object ==================================================== ===== in OC, you cannot directly modify the "member" of "struct attribute" of "object", but you can modify the "struct attribute" of "object" to modify the members of the struct attribute as follows: 1> use temporary variables to record the object's struct attributes 2> modify the properties of the temporary variables 3> Reset the temporary variables to the object's struct attributes 3. avoid Magic Number during program development) ========================================================== use Enumeration type, it can be avoided in the program that the magic number 1> Enumeration type is essentially an integer, which is used to replace the magic number 2> Enumeration type, after specifying the first integer, the following numbers increase by 4. frame & bounds & center ====================================== =====1> frame can modify the position and size of an object 2> bounds can modify the size of an object 3> center can modify the position of an object 5. first-end animation ================================================== ===// beginAnimations indicates that the subsequent code will "Participate" 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 all animations after beginAnimation and generate an animation [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.