Summary of UIScrollerView scrolling view of ios

  • 2021-09-20 21:41:26
  • OfStack

The UIScrollView class is responsible for all UIKit-based scrolling operations.

1. Create


CGRect bounds = [ [ UIScreen mainScreen ] applicationFrame ] ; 
UIScrollView* scrollView = [ [UIScrollView alloc ] initWithFrame:bounds ]; 

When you have created the scroll view, you can glue the contents of another view onto the blank page of the scroll view. Create a scrolling content window this time:


[ scrollView addSubview:myView]; 

You must give the actual size of the content so that the scrolling view knows the scrolling scope:


scrollView.contentSize = myView.frame.size; 

To turn on zoom, you need to adjust two properties of the view, maxinumZoomScale and mininumZoomScale. This allows the user to resize the content using the kneading gesture:


scrollView.maxinumZoomScale = 2.0;// Allowable magnification 2 Times  
scrollView.mininumZoomScale = 0.5;// Allow zoom in to 0.5 Times  

To turn on the scaling function, you also need to add an UIScrollViewDelegate agent through an viewForZoomingScrollView
Responds to the method of. This method returns the UIView object used for scaling:


scrollView.delegate = self; 
- (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView{ 
 retutn myView; 
} 

Tips: For large-scale data, you may initially use a scaling ratio that is lower than the actual size (1.0), so that users can zoom in smoothly.

2. Attributes

In addition to the zoom properties used above, scrolling views give you additional properties that allow you to fine-tune the behavior of real content. You can customize the UIScrollView class in many directions. The following attributes are most commonly used.

1.indicatorStyle

Specify the type of scroll bar indicator you want to use. The default behavior is to draw a black scroll bar on a white border, which is applicable in most backgrounds. Available styles are as follows:

UIScrollViewIndicatorStyleDefault UIScrollViewIndicatorStyleBlack UIScrollViewIndicatorStyleWhite

2.contentOffset

1 CGPoint structure containing the offset of the content to be displayed relative to the upper left corner of the window. The default is 0 × 0, but you can also put the display in other places.

3.directionalLockEnabled

The default behavior is to allow users to scroll horizontally and horizontally at the same time. Setting this property to YES causes the user's scrolling behavior to be locked to allow only horizontal or vertical, depending on the initial attitude.

4.bounces

When the user reaches the edge of the scroll area, this function allows the user to drag slightly to 1 point outside the boundary. When the user releases his finger, this area will bounce back like a rubber band, giving the user a visible prompt that he has reached the beginning or end position of the document. If you don't want the user to scroll beyond what is visible, you can set this property to NO.

5.bouncesZoom

Similar to the bounces option, this method allows the user to zoom beyond the maximum or minimum zoom level and then bounce back within range. Set this property to NO if you don't want users to be able to scale beyond the range you specify.

6.pagingEnabled

When paging is turned on, the scrolling view is divided into separate sections, and the user's scrolling experience becomes page flip, which you can use this property to flip.

3. Delegation method

You can assign a delegate to the scrolling view, and the following delegate methods will be notified at a specific time.


-(void)scrollViewDidScroll:(UIScrollView*)scrollView;

// You are notified when the view scrolls. Include 1 Pointers to the scrolled view, from which you can read contentOffset Property to determine where it scrolls.  

-(void)scrollViewWillBeginDragging:(UISCrollView*)scrollView;
// The user is notified when dragging in a certain direction for the first time. This method will get the scrolling view pointer passed as a parameter, which can also be read from  contentOffset  Property. 

-(void)scrollViewDidEndDragging:(UIScrollView*)scrollView willDecelerate:(BOOL)decelerate; 
// When the user lifts and drags to the finger, he is notified. You will also get 1 Boolean value, well-known to report whether deceleration is needed before scrolling the last position of the view.  

-(void)scrollViewWillBeginDecelerate:(UIScrollView*)scrollView;
// When the user raises his finger and the view needs to move on, he will be notified. This method can be used to read  contentOffset Attribute, so as to judge when the user lifts his finger and finally 1 Second scroll to the position, although this position does not make the scroll bar finally stop.  

-(void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView;
// When the above-mentioned deceleration is completed and the scrolling view stops, it is notified. The moment this notification is received, scroll through the contentOffset Property reflects where the scroll bar finally stops.  

[ scrollView addSubview:myView]; 
0

[ scrollView addSubview:myView]; 
1

OK, all have been summarized, and an example of page flipping will be written later. Please pay attention.

Attributes and Functions

CGPoint contentOffSet monitors the current scrolling position CGSize contentSize Size of scroll range The location of the UIEdgeInsets contentInset view in scrollView id < UIScrollerViewDelegate > delegate setup protocol BOOL directionalLockEnabled Specifies whether the control can only scroll in one direction BOOL bounces controls whether the control bounces when it encounters a border BOOL alwaysBounceVertical controls whether the border bounces when encountered in the vertical direction BOOL alwaysBounceHorizontal controls whether the border bounces when encountered horizontally BOOL pagingEnabled control whether the control flips the whole page BOOL scrollEnabled controls whether the control can scroll BOOL showsHorizontalScrollIndicator controls whether the horizontal scroll bar is displayed BOOL showsVerticalScrollIndicator controls whether the vertical scroll bar is displayed UIEdgeInsets scrollIndicatorInsets Specifies the position of the scroll bar in scrollerView UIScrollViewIndicatorStyle indicatorStyle Styling Scroll Bar float decelerationRate changes the deceleration point position of scrollerView BOOL tracking monitors whether the current target is being tracked BOOL dragging monitors whether the current target is being dragged BOOL decelerating monitors whether the current target is slowing down BOOL delaysContentTouches Controls whether views delay calling methods to start scrolling BOOL canCancelContentTouches controls whether the control touches an event that cancels touch float minimumZoomScale Minimum Scale Reduction Maximum Scale of float maximumZoomScale Amplification float zoomScale setting change ratio BOOL bouncesZoom controls whether it bounces back when zooming BOOL zooming Determines whether the size of the control is changing BOOL zoomBouncing determines if a scaling rebound is in progress BOOL scrollsToTop control control scrolls to top

Related articles: