This is the 30th day of my participation in the August Gwen Challenge. For details, see: August Gwen Challenge “juejin.cn/post/698796…”

preface

  • Automatic layout:Any control can define the exact location with reference to another control

To allow the programmer to focus on the application and not use frame too much in the code.

  • StoryBoard quick layout method: Copy controls by dragging and dropping them using the Option key

I What is UIScrollView?

Is a scrollable view control that can be used to display a large amount of content and scroll through all of it

Basic use of UIScrollView – the solution to UIScrollView cannot scroll

  1. Check whether the contentSize property is set
  2. Check whether the value of the scrollEnabled attribute is = NO
  3. Check whether userInteractionEnabled is NO
  4. Disable outLayout: To scroll through the scrollView control added to the storyBoard, autoLayout must be disabled.

II Common properties of UIScrollView

@property(nonatomic) CGPoint contentOffset; // This property is used to indicate the position of the UIScrollView scroll
@property(nonatomic) CGSize contentSize; // This property is used to indicate the size of the UIScrollView content, and the scrolling range (how far it can be rolled).
@property(nonatomic) UIEdgeInsets contentInset; // This property adds an extra scroll area to the UIScrollView 4 cycle
Copy the code

Use setters for contentSize, contentInset, and contentOffset in order;

Set offset to inset->size. Or: size->offset

  1. ContentSize adjusts offset according to ContentInset — other actions are implemented besides assignment

  2. ContentInset does not adjust offset based on contentSize — it simply assigns values to properties

Implementation differences of setter methods (can be seen by setting breakpoints)

#import "ViewController.h"
@interface ViewController(a)
@property (weak.nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak.nonatomic) IBOutlet UIButton *lastButton;
@end
@implementation ViewController
 
/ * * 1. - the realization of the setter method difference contentSize will adjust offset according to ContentInset - in addition to the assignment, also implements the other actions ContentInset won't adjust offset according to contentSize - simply attribute assignment * /
 
- (void)viewDidLoad {
 
    [super viewDidLoad];
 
    //1. Set spacing: Just specify the margins on the outside of content and do not automatically adjust contentOffset according to contentSize
 
    [self.scrollView setContentInset:UIEdgeInsetsMake(64.0.0.0)];
 
    //2. Set the scroll view content size
 
    //1> Automatically adjusts contentOffset according to the spacing, if contentInset is present
 
    //2> contentOffset is (0,0)
    [self.scrollView setContentSize:CGSizeMake(0.CGRectGetMaxY(self.lastButton.frame)+10)];//CGRectGetMaxY(self.lastbutton.frame)+10) is used to show the lastButton more clearly
    //3. Set the offset
    [self.scrollView setContentOffset:CGPointMake(0.- 64.)];
}
@end
Copy the code
  • UI Scrollview zoom

Scale the simulator by holding down the Option key

III Steps of agent implementation

3.1 What is the role of agency?

  1. Agent design pattern, the most widely used design pattern in OC; It is used to pass messages or data between two objects when certain events occur

  2. Listen for events that cannot be listened on by addTarget

3.2 background

  1. Target: You want to perform certain actions when the UIScrollView is in the scrolling state, when it has reached a certain position, and when it has stopped scrolling

  2. Prerequisite: Listen for UIScrollView scrolling (event)

  3. This method is implemented by assigning a delegate object to a UIScrollView that automatically informs its delegate object (i.e. method invocation) when a series of scrolls occur

  4. The criteria for becoming a Delegate object: comply with the UIScrollViewDelegate protocol and implement the corresponding methods

Set the ViewController of the UIScrollView as its delegate object

3.3 There are two ways to set the delegate property of a UIScrollView:

  1. By code:

Add a delegate by modifying the properties of the scrollView object

self.scrollView.delegate = self;

Copy the code
  1. Through the dragline of the storyBoard

3.4 Delegate example: The controller wants to know every character the user enters

3.5 Steps of proxy implementation summary

  1. Become a proxy for (child) controls; The parent (view Controller) becomes the proxy for the son (textField)

  2. Comply with the protocol –> The purpose is to take advantage of Xcode’s smart hints feature to write code quickly — this step is optional

  3. Implementation protocol method

The protocol is defined by the control, because only the control itself knows best what is happening within itself.

IV UIScrollView scaling principle

  1. When the user kneads on the UIScrollView, the UIScrollView sends a message to the Delegate asking which of its internal child controls (pieces of content) to scale.

- (void) scrollViewWillBeginZooming:(UIScrollView *)scrollView  withView:(UIView*) the view;// called when ready to start scaling- (void) scrollViewDidZoom: (UIScrollView*) scrollView;// called while scaling
Copy the code

When users use kneading gestures UIScrollView will invoke the delegate object viewForZoomingInScrollView: method, this method returns the control is the need to zoom control.

4.1 Steps for scaling

  1. Set the IDDelegate object of the UIScrollView
  2. Set minimumZoomScale and MaximumZoomScale to the maximum reduction and enlargement ratio
  3. The delegate object implementation viewForZoomingInScrollView: method, return to need zoom view controls

V paging

If you set UIScrollView’s PageEnabled property to YES, the UIScrollView will be split into separate pages, and the contents of the UIScrollView will be displayed in separate pages, usually in conjunction with UIPageControl to enhance pagination

5.1 Common Attributes of UIPageControl

// How many pages are there
@property(nonatomic) NSInteger numberOfPages;
// The page number currently displayed
@property(nonatomic) NSInteger currentPage;
// Whether to hide the page number indicator when there is only one page
@property(nonatomic) BOOL hidesForSinglePage;
// Other page number indicator colors
@property(nonatomic.retain) UIColor *pageIndicatorTintColor;
// The current page number indicator color
@property(nonatomic.retain) UIColor *currentPageIndicatorTintColor; 
Copy the code

VI Basic Knowledge

6.1 Getter /setter rewriting details

Override details for getters and setters

/** the setter method for the image; Setter methods, in addition to assigning, also perform the contentSize setting action */
- (void)setImage:(UIImage *)image{
    // The first step of the setter method is to assign a value to the property_image = image;// Sets the contents of the image view
    [self.imageView setImage:_image];
    // Make the image view automatically resize itself according to the image size
    [self.imageView sizeToFit];//Resizes and moves the receiver view so it just encloses its subviews.
    // Tells the image view the actual size of the internal contents
    [self.scrollView setContentSize:_image.size];
}
/ * * 0. Getter method override * when using its own object, using extracted * _ the attribute name when using other member attribute, use self. Gtter method, so that we can timely instantiation of 1 with members of the attribute. Lazy loading of imageView property, overwriting getter method - when using the dot property name of the object, the getter method is called, but the getter method */ is not called for the _ property name
- (UIImageView *)imageView{
    if (nil == _imageView) {
        / / create the imageView
        UIImageView *tmpImageView = [[UIImageView alloc]init];
        _imageView=tmpImageView;
        [self.scrollView addSubview:_imageView];
    }
    return _imageView;
}
Copy the code

6.2 NSTimer

Kunnan.blog.csdn.net/article/det…

6.3 Picture roaster

Development steps:

  1. The scrollView getter method is lazily loaded

Just specify the size, add the image to view 2. viewDidLoad, and calculate position 3. Run the observation and modify the properties of the scrollView……. 4. Instantiate UIPageControl 5. Because the paging control and scroll view are separate, listen for the scroll stop proxy method and change the number of pages in the paging control 6. Define UIPageControl as a property and add a listener method 7. Implement listening method, after the page number changes, modify the scrollView position 8. Add clock, call paging control of the monitoring method, the realization of automatic picture rotation

Please look at the original complete source code: kunnan.blog.csdn.net/article/det…

see also

More content please pay attention to the official number: iOS reverse