1. Basic controls for iOS development and basic controls for ios development

Source: Internet
Author: User
Tags uicontrol

1. Basic controls for iOS development and basic controls for ios development
I. UIView

1. Common Properties of UIView

@property(nonatomic) CGRect            frame;@property(nonatomic) CGRect            bounds;@property(nonatomic) CGPoint           center;@property(nonatomic) CGAffineTransform transform;@property(nonatomic,readonly) UIView       *superview;@property(nonatomic,readonly,copy) NSArray *subviews;@property(nonatomic,readonly) UIWindow     *window;

Supplement:

// View content mode @ property (nonatomic) UIViewContentMode contentMode;

Generally, this parameter is set to UIViewContentModeCenter. The image size remains the original size.

* Differences between frame, bounds, and center :*

* Frame: uses the upper-left corner of the parent control as the coordinate origin. You can determine the origin and size of the Control. * bounds: uses the upper-left corner as the coordinate origin, you can determine the size of the control * center: you can determine the control position * Note: OC does not allow modification of object structure attribute members

2. transform attributes

You can use transform to modify the position scaling and rotation of the control.
1> Create a transform attribute

CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx,  CGFloat ty) ;CGAffineTransform CGAffineTransformMakeScale(CGFloat sx, CGFloat sy);CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle)

2> overlay the transform

CGAffineTransform CGAffineTransformTranslate(CGAffineTransform t, CGFloat tx, CGFloat ty);CGAffineTransform CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CGFloat sy);CGAffineTransform CGAffineTransformRotate(CGAffineTransform t, CGFloat angle);

3> clear the previously set transform attributes

view.transform = CGAffineTransformIdentity;

Supplement: Coordinate Transformation

CGPoint * newpoint = [view1 convertPoint: <# (CGPoint) #> toView: view2]; Convert coordinates from view1 to view2 // CGRect * newRect = [view1 convertPoint: <# (CGPoint) #> fromView: view2]; // convert coordinates from view2 to view1CGPoint. The former is frame, the former is subviewCGPoint, and the former is bounds.

3. Common Methods of UIView

- (void)addSubview:(UIView*)view;- (void)removeFromSuperView;- (UIView*)viewWithTag:(NSInteger)tag;

1> meal adding (Listening to the process of adding sub-controls)

// Add a subcontrol-(void) didAddSubview :( UIView *) subview; // remove the subcontrol-(void) willRemoveSubview :( UIView *) subview; // to add to parent control-(void) willMoveToSuperview :( UIView *) newSuperview; // Add to control-(void) didMoveToSuperview;

4. UIView for simple animation

Two animation Methods
1> headers and tails

[UIView beginAnimations: nil context: nil]; // The animation to be executed [UIView commitAnimations];

2> Block type

[UIView animateWithDuration: 0.5 animations: ^ {// code of the animation to be executed}];

**

Ii. UIButton

**

  • Features: displays images, texts, and clicks in response to listener events
  • Four statuses:
1> normal--> UIControlStateNormal2> highlighted-->UIControlStateHighlighted3> selected-->UIControlStateSelected4> disabled-->UIControlStateDisabled

1. common attributes

@property(nonatomic,readonly,retain) NSString *currentTitle;@property(nonatomic,readonly,retain) UIColor  *currentTitleColor;@property(nonatomic,readonly,retain) UIImage  *currentImage;@property(nonatomic,readonly,retain) UIImage  *currentBackgroundImage;

2. Common attribute settings

- (void)setTitle:(NSString *)title forState:(UIControlState)state;- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;- (void)setImage:(UIImage *)image forState:(UIControlState)state;- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;btn.titleLabel.font = [UIFont systemFontOfSize:13];

3. Obtain attributes

- (NSString *)titleForState:(UIControlState)state;- (UIColor *)titleColorForState:(UIControlState)state;- (UIImage *)imageForState:(UIControlState)state;- (UIImage *)backgroundImageForState:(UIControlState)state;

4. manually create UIButton code

UIButton * btn = [UIButton buttonWithType: bytes]; [btn setBackgroundImage: [UIImage imageNamed: @ "btn_01"] forstate: UIControlStateNormal]; [btn setTitle: @ "Click me" forstate: UIControlStateNormal]; [btn settitleColor: [UIColor redColor] forstate: UIControlStateNormal]; [btn addTarget: self action: @ selector (btnClick) forControlEvents: UIcontrolEventTouchUpInside];

5. layout of the UIControl Control

@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;
3. UIImageView

1. Frame Animation attributes and Methods
1> attributes

// Sequence frame image array @ property (nonatomic, copy) NSArray * animationImages; // duration of Frame Animation @ property (nonatomic) NSTimeInterval animationDuration; // Number of frames of animation execution @ property (nonatomic) NSInteger animationRepeatCount;

2> Method

- (void)startAnimating;- (void)stopAnimating;- (BOOL)isAnimating;

2. Two loading methods of UIImagede:

1> cache (the memory occupied by the program will remain in the Program)

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

2> no cache (the memory occupied by images will be cleared after some specific operations)

+ (UIImage*)imageWithContentsOfFile:(NSString*)path;- (id)initWithContentsOfFile:(NSString*)path;

Supplement:

// Rendering mode. You can change the color of the original image-(UIImage *) imageWithRenderingMode :( UIImageRenderingMode) renderingMode
Iv. UILabel

1. Attributes

text font textColor textAlignment

2. UIFont setting method

// System default font + (UIFont *) systemFontOfSize :( CGFloat) fontSize; // bold + (UIFont *) boldSystemFontOfSize :( CGFloat) fontSize; // italic + (UIFont *) italicSystemFontOfSize :( CGFloat) fontSize;
V. UIAlertViewt prompt box

The Controller UIViewController complies with the proxy protocol of UIAlertView.

<UIAlertViewDelegate>

1. Create

[[UIAlertView alloc] initWithTitle: @ "title" message: @ "message" delegate: self cancelButtonTitle: @ "title of the cancel button" otherButtonTitles: @ "Other button Title 1", @ "Other button Title 2" nil] show];

2. Implement proxy Methods

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{}
6. UIActionSheet

1. The controller UIViewController complies with the proxy protocol of UIActionSheet.

<UIActionSheetDelegate>

2. Create

UIActionSheet * sheet = [[UIActionSheet alloc] initWithTitle: @ "are you sure you want to log out? "Delegate: self cancelButtonTitle: @" cancel "destructiveButtonTitle: @" OK "otherButtonTitles: nil, nil];

3. Display

[sheet showInView:self.view];

4. Implement proxy Methods

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{}
VII. UITextField

1. You can use the UITextField proxy method to listen to the button clicking at the bottom right corner of the keyboard.
1> become the UITextField proxy

self.textField.delegate = self;

2> comply with the UITextFieldDelegate Protocol to implement proxy Methods

- (BOOL)textFieldShouldReturn:(UITextField*)textField;

3> put a view on the left of UITextField (input box for keyboard)

self.textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0,0,8,0)];self.textField.leftViewMode = UITextFieldViewModeAlways;

2. Listen to UITextField to determine whether the text box has input.

// Listener notification [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (textChange) name: UITextFieldTextDidChangeNotification object: self. accountField]; // listens to the method executed when input is present-(void) textChange {}// removes the listener-(void) dealloc {[[nsicationicationcenter defacenter center] removeObserve: self];}

3. Add events (UITextField also inherits from UIControl)

[self.accountField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
8. Pop-up and exit the keyboard

1. Remove the keyboard

1> cancel the responder

[self.textField resignFirstResponder];

2> no keyboard input is allowed for the view of the entire controller.

[self.view endEditing:YES];

2. keyboard pop-up

[self.textField becomeFirstResponder];
IX. Supplement:

1. Comparison of JPG and PNG image formats

JPG: the compression ratio is relatively high. It is usually used for photos and web pages. It is lossy compression. during decompression, the CPU consumption is high.
PNG: high compression, lossless compression, and high decompression efficiency. It is recommended to use

2. Use of weak and strong

1> Why does the UI control use weak:

The UI control should be added to the _ view attribute of UIViewController (addSubview: method), where addSubview: the operation is a strong reference, so there is no need to set the UI control to strong reference again in the UIViewController attribute.

2> Why is the delegate proxy weak:

The UI control that is strongly referenced in the UIViewController. Its proxy is generally set to the Controller's own UIViewController. If you set the delegate proxy to strong, it is a circular reference, which may cause memory leakage.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.