The basic writing method of Button in iOS's UI development is explained
- 2020-05-10 22:58:41
- OfStack
1. Simple instructions
Under normal circumstances, click a control, will make the corresponding response is the button
Button function is more, both can display text, and can display pictures, but also can adjust the position of internal pictures and text at any time
2. Three states of the button
normal (normal state)
Default (Default)
The corresponding enumeration constant: UIControlStateNormal
highlighted (highlighted state)
When the button is pressed (the finger is not released)
The corresponding enumeration constant: UIControlStateHighlighted
disabled (failure state, unavailable state)
If the enabled property is NO, it means that the button cannot be clicked
The corresponding enumeration constant: UIControlStateDisabled
3. Pay attention to the point
(1) starting from Xcode5, image resources are managed in Images.xcassets, and images used in the project can be added to Images.xcassets by dragging and dropping
(2) multiple controls share one code, usually using tag.
4. Code examples
(1)
#import "LFViewController.h"
@interface LFViewController ()
@property (weak, nonatomic) IBOutlet UIButton *headImageView;
@end
@implementation LFViewController
// in OC In most controls, the first of the listening methods 1 The parameters are the control itself
//- (IBAction)left:(UIButton *)button {
//
// NSLog(@"----");
//}
- (IBAction)move
{
// through frame Modify the head The location of the
// in OC , it is not allowed to directly modify the "member" of the "struct property" of the "object".
// Allows you to modify the struct properties of an object
// 1. Take out the struct properties
CGRect rect = self.headImageView.frame;
// 2. Modify structure members
rect.origin.y -= 20;
// 3. Sets the struct properties of the object
self.headImageView.frame = rect;
}
(2)
#import "LFViewController.h"
/**
use git
1. When creating a project, check the box git
2. Development to 1 Segment lag, choose "Source Control""Commit", And write comments
*/
// An enumerated type is essentially 1 An integer used to replace a magic number
// In an enumeration type, specifies the 1 After 3 integers, the Numbers that follow will be incremented
typedef enum
{
kMovingDirTop = 10,
kMovingDirBottom,
kMovingDirLeft,
kMovingDirRight,
} kMovingDir;
#define kMovingDelta 50
@interface LFViewController ()
@property (weak, nonatomic) IBOutlet UIButton *headImageView;
@end
@implementation LFViewController
- (IBAction)move:(UIButton *)button
{
// CGRect rect = self.headImageView.frame;
CGPoint p = self.headImageView.center;
// magic number Magic Numbers, when other programmers see the code, don't know what it means
switch (button.tag) {
case kMovingDirTop:
p.y -= kMovingDelta;
break;
case kMovingDirBottom:
p.y += kMovingDelta;
break;
case kMovingDirLeft:
p.x -= kMovingDelta;
break;
case kMovingDirRight:
p.x += kMovingDelta;
break;
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
self.headImageView.center = p;
[UIView commitAnimations];
}
- (IBAction)zoom:(UIButton *)button
{
CGRect rect = self.headImageView.bounds;
// in C In language, about bool Judgment: either zero or true
if (button.tag) {
rect.size.width += 50;
rect.size.height += 50;
} else {
rect.size.width -= 50;
rect.size.height -= 50;
}
// Fore and aft animation
// beginAnimations Indicates that the following code will "participate" in the animation
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
self.headImageView.bounds = rect;
// self.headImageView.alpha = 0;
// commitAnimations, will beginAnimation All subsequent animations are submitted and generated
[UIView commitAnimations];
}
@end
5. Take notes
1. Parameters of IBAction
- (IBAction)left:(UIButton *)button
(1) in OC, the first parameter of most control listening methods is the control itself
(2) the default parameter type when connecting is id
(3) if you want to facilitate the use of the control in the listening method, you can modify the parameter type of the listening method when or after connecting the line
2. Modify the struct members of the object
In OC, it is not allowed to modify the member of the struct property of the object directly, but it is allowed to modify the struct property of the object.
The member method to modify the structure property is as follows:
(1) use temporary variables to record the structure properties of the object
(2) modify the properties of temporary variables
(3) reset the temporary variable to the structure property of the object
3. Magic Numbers need to be avoided in program development (Magic Number)
With enumerated types, you can avoid magic Numbers in your program
(1) the enumeration type is essentially an integer, whose function is to replace the magic number
(2) in the enumeration type, after the first integer is specified, the subsequent number will be incremented
4. frame & bounds & center
1 > frame can modify the location and size of objects
2 > bounds can modify the size of objects
3 > center can modify the location of objects
5. Head to tail animation
// beginAnimations Indicates that the following code will "participate" in the animation
[UIView beginAnimations:nil context:nil];
// setAnimationDuration Specifies the duration of the animation
[UIView setAnimationDuration:2.0];
self.headImageView.bounds = rect;
......
// commitAnimations That will be beginAnimation All subsequent animations are submitted and generated
[UIView commitAnimations];
Here is a list of the basic attributes of UIButton
1. Definition of UIButton
UIButton *button=[[UIButton buttonWithType:(UIButtonType);
There are six types of button that you can define,
typedef enum {
UIButtonTypeCustom = 0, Custom style
UIButtonTypeRoundedRect, The rounded rectangle
UIButtonTypeDetailDisclosure, Blue arrow button, mainly for detailed instructions
UIButtonTypeInfoLight, Bright exclamation point
UIButtonTypeInfoDark, Dark exclamation point
UIButtonTypeContactAdd, 10 Plus button
} UIButtonType;
2. Set frame
button1.frame = CGRectMake(20, 20, 280, 40);
[button setFrame:CGRectMake(20,20,50,50)];
3. button background color
button1.backgroundColor = [UIColor clearColor];
[button setBackgroundColor:[UIColor blueColor]];
4. state status
forState: this parameter defines the state in which the text or image of the button will appear
enum {
UIControlStateNormal = 0, Regular state manifestation
UIControlStateHighlighted = 1 << 0, The highlighted state appears
UIControlStateDisabled = 1 << 1, The disabled state will appear
UIControlStateSelected = 1 << 2, selected
UIControlStateApplication = 0x00FF0000, When the application flags
UIControlStateReserved = 0xFF000000 Reserved for the internal frame, you can leave it alone
};
@property(nonatomic,getter=isEnabled)BOOL enabled; // default is YES. if NO, ignores touch events and subclasses may draw differently
@property(nonatomic,getter=isSelected)BOOL selected; // default is NO may be used by some subclasses or by application
@property(nonatomic,getter=isHighlighted)BOOL highlighted;
5. Set button fill image and background image
[buttonsetImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];
[buttonsetBackgroundImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];
6. Set button title and title color
[button1 setTitle:@" Click on the " forState:UIControlStateNormal];
[buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];
7. The setting button will glow when pressed
button.showsTouchWhenHighlighted=NO;
Add or remove event handling
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
[btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
9. Set the inner picture space and caption space of the button
UIEdgeInsets insets; // Set the image spacing inside the button
insets.top = insets.bottom = insets.right = insets.left = 10;
bt.contentEdgeInsets = insets;
bt.titleEdgeInsets = insets; // The title spacing