Basic use of buttons what is a button
- The Uikit framework provides a very wide range of UI controls, but not every one is often used, some controls may not be available for 1 years, and some controls are used every day, such as UIButton, UILabel, Uiimageview, UITableView, and so on.
- Now let's learn a very important and relatively basic UI control---UIButton, commonly known as "button"
- In general, when you click on a control, the corresponding response is the button
- The function of the button is more, can display the text, but also can display the picture, can adjust the internal picture and the text position at any time
Small case
- Next, a small case study of the basic use of buttons
Master
- creating, adding controls Using code
- Set the buttons in different states of background, text, and text colors by storyboard and code respectively
- Click through storyboard and code listener buttons respectively
- Modify the position and dimensions of a control
- Make a simple animation effect
- Mastering the use of frame, center, bounds, transform
Homework
- Use plain code to rewrite small cases about buttons (do not drag controls)
Add a picture resource
- From Xcode5 onwards, the picture resources are put into the images.xcassets to manage
- Add the images used in the project to the Images.xcassets
To modify the dimensions of a control
- Is the size of the set control is 35x35
Remove AutoLayout
- If you find that you cannot modify the position or size of the control by code, you should remove the AutoLayout function from the storyboard, which is the feature that began to appear iOS6
- As the name implies, AutoLayout is used for automatic layout, which is used to constrain the position and size of the control. With this feature removed, the position and size of the control will no longer have a fixed binding.
The state of UIButton
- Defaults (default)
- The corresponding enumeration constants: UIControlStateNormal
- Highlighted (highlight state)
- When the button is pressed (the finger is not released)
- The corresponding enumeration constants: Uicontrolstatehighlighted
- Disabled (fail State, unavailable state)
- If the Enabled property is no, it is in the disable state, which means the button cannot be clicked
- The corresponding enumeration constants: Uicontrolstatedisabled
Set the background picture of the button
- Set the background picture of the button in different states (in order to ensure that the picture in the highlighted state is normal, you must set the type of the button to be custom)
Set Avatar button
- Normal Status: Red text "Dot Me"
- Highlight Status: Blue text "What do you Touch me for?"
Change the position of the Avatar button
- You can modify the position and size of the control on the screen by modifying the frame property of the control
- For example, click the "Up" button to reduce the Y value of the button
-(Ibaction) Top: (UIButton *) sender
{
CGRect btnframe = self.headBtn.frame;
BTNFRAME.ORIGIN.Y-= 10;
Self.headBtn.frame = Btnframe;
}
- The following code is wrong, and the OC Syntax stipulates that the member that directly modifies the struct property of an object is not allowed
SELF.HEADBTN.FRAME.ORIGIN.Y-= 10;
Code creation button
- In the development process, not every time through the storyboard drag control to complete the UI interface, because storyboard above the interface is "fixed dead", and sometimes may be in the process of running the program to dynamically add some new controls to the interface
- such as QQ chat information, is someone sent a message after the dynamic display. Therefore, you need to know how to add controls dynamically in code
- In fact, the essence of storyboard is to switch to the corresponding code according to the graphical interface description.
Storyboard to code conversion
Implement simple animations
- In iOS development, it's very easy to implement some small animations.
- The system automatically animates according to the change of a property value
- For example, the X value would have been 10, and then the X value was suddenly changed to 100, and the system shifted the X value from 10 to 100 by panning the animation.
- There are roughly 2 ways to easily animate:
- Tail type
[UIView Beginanimations:nil Context:nil];
/** code to perform the animation **/
[UIView commitanimations];
[UIView animatewithduration:0.5 animations:^{
/** code to perform the animation **/
}];
Modify the position and dimensions of a control
- You can modify the position of the control by using the following properties
- Frame.origin
- Center
- You can modify the size of a control by using the following properties
- Frame.size
- Bounds.size
Transform Property
- Use the Transform property to modify the control's displacement (position), scale, rotation
- Create a Transform property
Cgaffinetransform cgaffinetransformmaketranslation (cgfloat tx, cgfloat ty);
Cgaffinetransform Cgaffinetransformmakescale (cgfloat sx, CGFloat sy);
Cgaffinetransform cgaffinetransformmakerotation (cgfloat angle)
(Angle is radian system, not angle system)
Cgaffinetransform cgaffinetransformtranslate (Cgaffinetransform t, cgfloat tx, cgfloat ty);
Cgaffinetransform Cgaffinetransformscale (Cgaffinetransform T, cgfloat SX, CGFloat sy);
Cgaffinetransform cgaffinetransformrotate (Cgaffinetransform t, cgfloat angle);
View.transform = cgaffinetransformidentity;
Common types
- A uicolor represents a color that can be obtained in many common colors by means of the Uicolor class method.
- + (Uicolor *) Blackcolor; 0.0 white Black
- + (Uicolor *) Darkgraycolor; 0.333 White dark Grey
- + (Uicolor *) Lightgraycolor; 0.667 White Bright Grey
- + (Uicolor *) Whitecolor; 1.0 White
- + (Uicolor *) Graycolor; 0.5 White Grey
- + (Uicolor *) Redcolor; 1.0, 0.0, 0.0 RGB Red
- + (Uicolor *) Greencolor; 0.0, 1.0, 0.0 RGB Green
- + (Uicolor *) Bluecolor; 0.0, 0.0, 1.0 RGB Blue
- + (Uicolor *) Cyancolor; 0.0, 1.0, 1.0 RGB Cyan
- + (Uicolor *) Yellowcolor; 1.0, 1.0, 0.0 RGB Yellow
- + (Uicolor *) Magentacolor; 1.0, 0.0, 1.0 RGB Magenta
- + (Uicolor *) Orangecolor; 1.0, 0.5, 0.0 RGB Orange
- + (Uicolor *) Purplecolor; 0.5, 0.0, 0.5 RGB Purple
- + (Uicolor *) Browncolor; 0.6, 0.4, 0.2 RGB Brown
- + (Uicolor *) Clearcolor; 0.0 White, 0.0 alpha clear color (empty color)
(filename can omit extension)
UIImage *image = [UIImage imagenamed:@ "btn_01"];
Style of the button
UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];
- Uibuttontypecustom: No type, the content of the button needs to be customized
- Uibuttontypedetaildisclosure:
Uibuttontypeinfolight:
Uibuttontypeinfodark:
Uibuttontypecontactadd
05-Basic use of buttons