27th-day notes for learning in iOS (UIButton-UIImageView Introduction), and calculating the uiimage size in ios

Source: Internet
Author: User

27th-day notes for learning in iOS (UIButton-UIImageView Introduction), and calculating the uiimage size in ios

IOS Learning (UI) knowledge point sorting

1. Introduction to UIButton

1) concept: UIButton is a common control that triggers corresponding functions by clicking

2) several common UIButton statuses
1. UIControlStateNormal normal
2. UIControlStateHighlighted highlighted state
3. UIControlStateSelected: select state-> triggered only when the selected of the button is set to yes

3) several common UIButton events
1. Press the UIControlEventTouchUpInside button and lift the event
2. UIControlEventTouchDown
3. Click the UIControlEventTouchDownRepeat button multiple times to trigger the event.

4) UIButton initializes the instance code

1 UIButton * button = [[UIButton alloc] init]; 2 button. frame = CGRectMake (20, 50, 50, 50); 3 button. backgroundColor = [UIColor clearColor]; 4 [button setTitle: @ "button 1 normal state" forState: UIControlStateNormal]; 5 [button setTitle: @ "button 1 highlighted state" forState: UIControlStateHighlighted]; 6 [button setTitle: @ "button 1 Selected State" forState: UIControlStateSelected]; 7 8 // trigger event 9 when the button is clicked [button addTarget: self action: @ selector (buttonTapped :) forControlEvents: UIControlEventTouchUpInside]; 10 // event 11 triggered after the button is pressed [button addTarget: self action: @ selector (buttonTappedDown :) forControlEvents: UIControlEventTouchDown] 12 // double-click the trigger event 13 [button addTarget: self action: @ selector (buttonTappedDown :) forControlEvents: UIControlEventTouchDownRepeat]; 14 // set the font color of the highlighted button. 15 [button setTitleColor: [UIColor lightGrayColor] forState: UIControlStateHighlighted]; 16 // The button font is changed to the bold font of the 35-digit 17 button. titleLabel. font = [UIFont boldSystemFontOfSize: 35]; 18 // set the rounded corner to 19 buttons. layer. cornerRadius = 5.f; 20 // set the Border width to 21 button. layer. borderWidth = 2.1; 22 // set the border color 23 button. layer. borderColor = [UIColor lightGrayColor]. CGColor; 24 // set the button background image 25 UIImage * imageNormal = [UIImage imageNamed: @ "camera"]; 26 // set imageNormal as the normal image of the button 27 [button setImage: imageNormal forState: UIControlStateNormal]; 28 29 UIImage * imageHightLight = [UIImage imageNamed: @ "camera2"]; 30 // set imageHightLight to the highlighted image of the button 31 [button setImage: imageHightLight forState: UIControlStateHighlighted]; 32 // when the button is set to an image and the image is not highlighted, the highlighted state is canceled. The default value is Yes33 button. adjustsImageWhenHighlighted = YES; 34 [self. window addSubview: button];


5) Prevent the button from repeatedly clicking the submitted data instance code

1 [button addTarget: self action: @ selector (buttonTapped :) forControlEvents: UIControlEventTouchUpInside]; 3-(void) buttonTapped :( UIButton *) button 4 {5 // you cannot click 6 buttons for the set button. userInteractionEnabled = NO; 8 // The delayed execution method prevents the button from being clicked quickly or does not want to be clicked to cause an error 9 [self generated mselector: @ selector (delayMethod :) withObject: button afterDelay: 1]; 11} 12 13 // latency method-> set the button to clickable status 14-(void) delayMethod :( UIButton *) button15 {16 button. userInteractionEnabled = YES; 17}

 

II. Introduction to UIImageView

1) concept: UIImageView is a control used by iOS to display images.

2) UIImageView instance initialization code

1 UIImageView * imageView = [[UIImageView alloc] init]; 2 imageView. frame = CGRectMake (0, 0, self. view. frame. size. width, self. view. frame. size. width); 3 imageView. backgroundColor = [UIColor whiteColor]; 4 imageView. center = self. view. center; 5 6 // tag specifies the unique identifier of the control. The value cannot be repeated in 7 imageView. tag = 100; 8 9 // The clipsToBounds attribute of UIImageView. If it is set to yes, the excess part is not displayed. clipsToBounds = YES; 11 12 // read an image 13 UIImage * image = [UIImage imageNamed: @ "icon"]; 14 imageView. image = image; 15 16 // set the image Display Mode 17 imageView. contentMode = UIViewContentModeScaleAspectFill; 18 19 // enable imageview user interaction Note: To implement image click events, this attribute must be set to YES20 imageView. userInteractionEnabled = YES; 21 [self. view addSubview: imageView]; 22 23 // Add a click event 24 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self
Action: @ selector (imageViewTapped :)];
25 [imageView addGestureRecognizer: tap];


3) Several filling modes commonly used in UI_ImageView
1. UIViewContentModeScaleToFill stretch the image to fill it with UIImageView
2. UIViewContentModeScaleAspectFill stretch the image so that it is not deformed and full of UIImageView
3. UIViewContentModeScaleAspectFit stretch imgage so that it is not deformed and is fully displayed in UIImageView.

4) UITapGestureRecognizer in addition to adding a click method to UI_ImageView, you can also add a click method to other controls.
For example, UI_Lable and UI_View...

5) three methods for obtaining images in iOS
Method 1:

1 // load the image object to the memory. 2 UIImage * image1 = [UIImage imageNamed: @ "camera"]; 3 CGSize size = image1.size; 4 NSLog (@ "size. w % f size. h % f ", size. width, size. height); 5 // if the image format is png, the suffix name can be omitted. Other formats cannot be omitted. 6 UIImage * image2 = [UIImage imageNamed: @ "icon.jpeg"];

Method 2:

// Use Case: read large images, which occupy the memory. Use this method to release images in time. // read icon.jpeg NSString * imagePath3 = [[NSBundle mainBundle] pathForResource: @ "icon" ofType: @ "jpeg"]; UIImage * image3 = [[UIImage alloc] initWithContentsOfFile: imagePath3]; NSString * imagePath3_1 = [[NSBundle mainBundle] pathForResource: @ "icon.jpeg" ofType: nil]; UIImage * image3_1 = [[UIImage alloc] initWithContentsOfFile: imagePath3_1];

Method 3:

1 NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"jpeg"];2 3  UIImage *image4 = [UIImage imageWithContentsOfFile:imagePath];

 

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.