Common properties and methods of UIScrollView class

Common properties and methods of UIScrollView class

[java] view plaincopy

CGRect bounds = [ [ UIScreen mainScreen ] applicationFrame ] ;

UIScrollView* scrollView = [ [UIScrollView alloc ] initWithFrame:bounds ];

Once you have created the scroll view, you can glue the content of another view to the blank page of the scroll view. This will create a scrolling content window:

[java] view plaincopy

[scrollView addSubview:myView];

You have to give the actual size of the content so that the scroll view knows how far to scroll:

[java] view plaincopy

scrollView.contentSize = myView.frame.size;

To enable zooming, you need to adjust two properties of the view, maxinumZoomScale and mininumZoomScale. This allows users to resize the content using a pinch gesture:

[java] view plaincopy

scrollView.maxinumZoomScale = 2.0; //Allows 2x zoom

scrollView.mininumZoomScale = 0.5; // Allows zooming in to 0.5 times

To enable zooming, you also need to add a UIScrollViewDelegate delegate with a method called viewForZoomingScrollView.

This method returns the UIView object used when scaling:

[java] view plaincopy

scrollView.delegate = self;

- (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView{

<span style="white-space:pre"> </span>retutn myView;

}

Tips: For large-scale data, you might initially use a zoom factor lower than the actual size (1.0) to allow the user to zoom in smoothly.

2. Attributes

In addition to the zoom properties used above, the scroll view has many other properties that allow you to fine-tune the behavior of the displayed content. You can customize the UIScrollView class in many ways. The following properties are the most commonly used.

1.indicatorStyle

Specifies the type of scroll bar indicator you wish to use. The default behavior is to draw black scroll bars on a white border, which works well on most backgrounds. The following styles are available:

[java] view plaincopy

UIScrollViewIndicatorStyleDefault

UIScrollViewIndicatorStyleBlack

UIScrollViewIndicatorStyleWhite

2. contentOffset

A CGPoint structure that contains the offset of the content to be displayed relative to the upper left corner of the window. The default is to start at 0×0, but you can also put the content at other locations.

3.directionalLockEnabled

The default behavior is to allow the user to scroll both horizontally and vertically. Setting this property to YES will cause the user's scrolling behavior to be locked to only horizontal or vertical, depending on the initial gesture.

4. Bounces

This feature allows the user to drag slightly outside the border when the user reaches the edge of the scrolling area. When the user releases his finger, the area will bounce back to its original position like a rubber band, giving the user a visible hint that he has reached the beginning or end of the document. If you do not want the user to be able to scroll beyond the visible content, 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 into range. If you do not want the user to be able to zoom beyond the range you specify, set this property to NO.

6.pagingEnabled

When paging is enabled, the scroll view is divided into multiple independent sections, and the user's scrolling experience becomes page flipping. You can use this property to perform page flipping.

3. Delegation Method

You can give the scroll view a delegate, and the following delegate methods will be notified at specific times.

[java] view plaincopy

-(void)scrollViewDidScroll:(UIScrollView*)scrollView;//You will receive a notification when the view is scrolled. It includes a pointer to the scrolled view, from which you can read the contentOffset property to determine the scrolled position.

[java] view plaincopy

-(void)scrollViewWillBeginDragging:(UISCrollView*)scrollView;//Get notified when the user*** drags in a certain direction. This method gets the scroll view pointer passed as a parameter, and can also read the contentOffset property from it.

[java] view plaincopy

-(void)scrollViewDidEndDragging:(UIScrollView*)scrollView willDecelerate:(BOOL)decelerate;

//Get notified when the user lifts the dragged finger. You will also get a Boolean value, which reports whether deceleration is needed before the scroll view reaches its final position.

[java] view plaincopy

-(void)scrollViewWillBeginDecelerate:(UIScrollView*)scrollView;//When the user lifts his finger and the view needs to continue moving, you will receive a notification. This method can be used to read the contentOffset property to determine the position where the user last scrolled before lifting his finger, although this position does not make the final stop position of the scroll bar.

[java] view plaincopy

-(void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView;//Get notified when the above-mentioned deceleration is completed and the scroll view stops. When receiving this notification, the contentOffset property of the scroll view will reflect the final stop position of the scroll bar.

[java] view plaincopy

-(void)scrollViewDidEndZooming:(UIScrollview*)scrollView withView:(UIView*)view atScale:(float)scale; //When the user zooms in or out on the content, you will be notified. The zoom ratio is represented as a floating point value and is passed in as a parameter.

[java] view plaincopy

-(BOOL)scrollViewShouldScrollToTop:(UIScrollView*)scrollView;

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

//When the user taps the iPhone status bar, the scroll view delegate can decide whether the view scrolls back to the beginning.

OK, that’s all. I’ll write an example of page flipping later, so stay tuned.

Attribute Effect

CGPoint contentOffSet monitors the current scroll position

CGSize contentSize The size of the scroll range

UIEdgeInsets contentInset The position of the view in the scrollView

id<UIScrollerViewDelegate>

Delegate sets the protocol

BOOL directionalLockEnabled Specifies whether the control can only be scrolled in one direction.

BOOL bounces Controls whether the control bounces when encountering a border

BOOL alwaysBounceVertical controls whether to bounce when encountering a border in the vertical direction

BOOL alwaysBounceHorizontal controls whether to bounce when encountering a border in the horizontal direction

BOOL pagingEnabled controls whether the control flips the entire page

BOOL scrollEnabled controls whether the control can scroll

BOOL showsHorizontalScrollIndicator Controls whether to display the horizontal scroll bar

BOOL

showsVerticalScrollIndicator controls whether to show the vertical scroll bar

UIEdgeInsets scrollIndicatorInsets specifies the position of the scroll bar in the scrollerView

UIScrollViewIndicatorStyle

indicatorStyle sets the scroll bar style

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 decelerating

BOOL delaysContentTouches Controls whether the view delays calling the start scrolling method

BOOL canCancelContentTouches Controls whether the control can cancel touch events

float minimumZoomScale The minimum zoom ratio

float maximumZoomScale The maximum zoom ratio

float zoomScale sets the change ratio

BOOL bouncesZoom controls whether the zoom will bounce back.

BOOL zooming Determines whether the size of the control is changing

BOOL zoomBouncing Determines whether zoom rebound is in progress

BOOL scrollsToTop controls the scrolling of the control to the top

<<:  Why I don't buy an Apple Watch

>>:  Eight technologies that mobile developers cannot ignore

Recommend

Case study of advertising in the home furnishing industry circle of friends

As the growth of the upstream real estate industr...

Android QQ 8.7.0 released: Little yellow face ejects you across the screen

[[396715]] Tencent today released the latest offi...

QQShare: How did an unknown app in China become a national app in India?

[[167125]] In China, not many people know Yan Fei...

Analysis of the user system of Momo live broadcast product

There is a mind map at the end of the article. By...

Puning SEO training: keyword analysis for website optimization

Keyword research has long been the starting point...

Welcome home! What innovative technologies will help Shenzhou 15 return home?

On June 4, the return capsule of the Shenzhou XV ...

3 Tips for Writing an Event Planning Proposal

Let’s talk about planning and writing plans today...

Teacher Yu Yinuo’s Chinese language basics improvement course (9-13 years old)

Resource introduction of Teacher Yu Yinuo’s Chine...

National Disability Day丨Today, forward for love!

May 15th is the 32nd National Day for Persons wit...

11 Mobile App Development Trends in 2017

Smartphones have become an integral part of our d...