“This is my 39th day of participating in the First Challenge 2022. For more details: First Challenge 2022.”

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

UIScrollView can’t 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

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.

see also

🍅 Contact author: iOS Reverse (public number: iosrev)


🍅 Author profile: CSDN blog expert certification 🏆 international Top 50, Huawei Cloud enjoy expert certification 🏆, iOS reverse public number master


🍅 Resume template, technical assistance. Pay attention to me, all for you.