IOS Development Uiscrollview Control detailed _ios

Source: Internet
Author: User

What is a Uiscrollview control?

(1) Mobile device screen ⼤ large ⼩ Small is extremely limited, so direct ⽰ show in the ⽤ user's immediate content is also very limited

(2) When the exhibition ⽰ show more content, than ⼀ a screen, ⽤ users can scroll ⼿ gestures to see outside the screen content

(3) The ordinary UIView does not have the scrolling function, cannot display ⽰ to show excessively the content

(4) Uiscrollview is a scrolling view control that can be ⽤ to show ⽰ ⼤ a large amount of content, and can be scrolled to see all the content

(5) Example: "Settings" on the phone, other ⽰ sample programs

Second, the simple use of Uiscrollview

(1) Add the ⽰ content to the Uiscrollview

(2) Set the Contentsize property of Uiscrollview, tell Uiscrollview all the contents of the ruler ⼨ Inch, that is, tell it the range of scrolling (how far to roll, where to roll to the end of ⾥)

Note: the "Content view" in this article, referred to in the official document, is called the Content-View, which represents the part of the uiscrollview that can be used to show the contents

Iii. Properties and methods

Content View Related

The size of the content view, default to Cgsizezero
@property (nonatomic) cgsize contentsize;

Adds a scrollable region around the content view, default to Uiedgeinsetszero
@property (nonatomic) uiedgeinsets contentinset;

The offset of the origin of the content view relative to the origin of the ScrollView (the upper-left offset is a positive number), and the default is Cgpointzero
@property (nonatomic) cgpoint contentoffset;

Sets the offset of the origin of the content view relative to the origin of the ScrollView
-(void) Setcontentoffset: (Cgpoint) Contentoffset animated: (BOOL) animated;

Sliding correlation

Whether to allow sliding, the default is Yes @property (nonatomic,getter=isscrollenabled) BOOL scrollenabled; is only allowed to slide One direction at a time, the default is no, if set to Yes, the user starts sliding in the horizontal/vertical direction, and then it is prohibited to slide vertically/horizontally (note: When the user starts sliding diagonally, the slide can slide in any direction at the same time) @property (

Nonatomic, getter=isdirectionallockenabled) BOOL directionallockenabled; Allow clicking on the status bar let the nearest ScrollView slide to the top of the status bar, the default is yes (note: If there are multiple ScrollView in the iphone that sets the property to Yes, the method is invalid;

In the ipad the closest scrollview to the status bar is slid to the top) @property (nonatomic) BOOL scrollstotop; Whether to slide by number of pages, the default is no, and if set to Yes, it will only stop at the bounds of the scrollview when sliding @property (nonatomic, getter=ispagingenabled) BOOL

pagingenabled;

Whether there is a bottoming rebound effect, the default is Yes @property (nonatomic) BOOL bounces; Whether there is always a bottoming rebound effect (even if the content view is smaller than the ScrollView size), the default is no (note: The prerequisite for entry is bounces = YES) @property (nonatomic) BOOL
Alwaysbouncehorizontal;

@property (nonatomic) BOOL alwaysbouncevertical; Specifies the rate at which the user's finger slips away from the screen, the default is Uiscrollviewdecelerationratenormal (slowly stops), and the remaining options are uiscrollviewdecelerationratefast (Fast stop) @

Property (nonatomic) CGFloat decelerationrate; Slide the specified range to just the visible (that is, nearest to the edge)-(void) scrollRectToVisible: (CGRect) Rect animated: (BOOL) animated; 

Indicator related

Indicator style, default to Uiscrollviewindicatorstyledefault (black Content white border, applicable to any background), the remaining option is uiscrollviewindicatorstyleblack (All Black) and Uiscrollviewindicatorstylewhite (Kangbai)
@property (nonatomic) Uiscrollviewindicatorstyle Indicatorstyle;

Adds a scrollable area around the indicator, which defaults to Uiedgeinsetszero
@property (nonatomic) uiedgeinsets scrollindicatorinsets;

Whether the indicator is visible when sliding, the default is Yes
@property (nonatomic) BOOL showshorizontalscrollindicator;
@property (nonatomic) BOOL showsverticalscrollindicator;

Flash Indicator (Note: It is recommended to call the user when ScrollView is displayed to remind the user that the place can be slid)
-(void) flashscrollindicators;

Event-related

Uiscrollview Handling Touch Event principle

When the user presses on a child view of Uiscrollview, Uiscrollview does not know whether the user wants to slide the content view or click the corresponding child view, so at the moment of pressing, When an event uievent is passed from uiapplication to Uiscrollview, it intercepts the event without immediately passing it to the corresponding child view, starting a 150ms countdown and listening for the user's next behavior

1, when the countdown is over, if the user's finger has moved, then the direct scrolling content view, will not pass the event to the corresponding child view;

2, when the countdown is over, if the user's finger position has not changed, the method that calls itself -touchesShouldBegin:withEvent:inContentView: asks whether to pass the event to the corresponding child view (if it returns no, the event is not passed to the corresponding child view, and if yes is returned, the event is passed to the corresponding child view, the default is YES)

3, when the event is passed to the child view, if the position of the finger moved again, then call its own -touchesShouldCancelInContentView: method to ask whether to cancel the event passed to the child view

Returns whether the user has touched the content view ready to slide (note: When the value is set to Yes it is possible that the user just touched the content view but did not start sliding) @property (nonatomic,readonly,getter=istracking

) BOOL tracking;

Returns whether the user has started sliding the content view (note: The value is set to Yes before it may need to slide for a period or distance) @property (nonatomic,readonly,getter=isdragging) BOOL dragging;

Returns whether it is in a deceleration state (that is, the finger has left the screen, but the scrollview is still sliding) @property (nonatomic,readonly,getter=isdecelerating) BOOL decelerating; Whether to delay event delivery by default Yes, if set to No,scrollview, call-touchesshouldbegin:withevent:incontentview: Method immediately for next action @property (

nonatomic) BOOL delayscontenttouches; Whether the content view can be touched, the default is yes, and if set to No, if you start tracking events, the events that have been passed to the child view will not be canceled even if the finger moves @property (nonatomic) BOOL

Cancancelcontenttouches; Override this method in a subclass of Uiscrollview to return the event to the corresponding child view, return Yes by default, and if no, the event is not passed to the corresponding child view-(BOOL) Touchesshouldbegin: (nsset<

Uitouch *> *) touches withevent: (Uievent *) event Incontentview: (UIView *) view; Override this method in Uiscrollview subclasses to return the event that has been passed to the child view, default to return no when the child view is Uicontrol, or return Yes (note: The prerequisite for this method being invoked is Cancancelcontenttouches = YES)-(BOOL) Touchesshouldcancelincontentview: (UIView *) view;

Scaling related

When the user uses two fingers for scaling, we adjust the offset and scaling ratio of the content view (note: After the user has two fingers, it is possible to still have a finger in operation when the event is not passed to the child view)

Minimum scaling, default to 1.0
@property (nonatomic) cgfloat Minimumzoomscale;

Maximum scaling, the default is 1.0 (must be greater than minimumzoomscale to work)
@property (nonatomic) cgfloat Maximumzoomscale;

The scaling ratio defaults to 1.0
@property (nonatomic) cgfloat Zoomscale;

Set Zoom scale
-(void) Setzoomscale: (cgfloat) scale animated: (BOOL) animated;

Zoom to the specified area
-(void) Zoomtorect: (cgrect) Rect animated: (BOOL) animated;

Whether to allow bottoming rebound, the default is Yes
@property (nonatomic) BOOL bounceszoom;

Returns whether the
@property (nonatomic,readonly,getter=iszooming) BOOL zooming is being scaled;

Returns whether the bottoming is rebounding
@property (nonatomic,readonly,getter=iszoombouncing) BOOL zoombouncing;

Keyboard related

Hides the keyboard mode, defaults to Uiscrollviewkeyboarddismissmodenone (does not hide the keyboard), and the remaining options are Uiscrollviewkeyboarddismissmodeondrag ( Hide the keyboard when dragging ScrollView) and uiscrollviewkeyboarddismissmodeinteractive (hide the keyboard when dragging over the keyboard)
@property (nonatomic) Uiscrollviewkeyboarddismissmode Keyboarddismissmode;

Agent

Sliding correlation

Call-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView when the contentoffset of the ScrollView is changed;

Call when you are about to start dragging (Note: This method may need to slide for some time or distance before being invoked)-(void) scrollviewwillbegindragging: (Uiscrollview *) ScrollView; Called when the user stops dragging (note: The application can adjust the content view Targetcontentoffset location by modifying the value of the parameter)-(void) Scrollviewwillenddragging: (

Uiscrollview *) ScrollView withvelocity: (cgpoint) Velocity targetcontentoffset: (inout cgpoint *) Targetcontentoffset; Called when the user stops dragging (note: The decelerate parameter is yes if content view contents views continue to move after stopping dragging)-(void) scrollviewdidenddragging: (Uiscrollview *)

ScrollView willdecelerate: (BOOL) decelerate;

Called when a slowdown is about to begin (only when the drag is stopped and the move is called)-(void) scrollviewwillbegindecelerating: (Uiscrollview *) ScrollView;

Called at the end of the deceleration (only if you stop dragging and then move on)-(void) scrollviewdidenddecelerating: (Uiscrollview *) ScrollView; Returns whether the Click Status bar allows ScrollView to slide to the top and, if not implemented, the default is yes (called only if the Scrollstotop property is Yes)-(BOOL) Scrollviewshouldscrolltotop: (

Uiscrollview *) ScrollView; Called when the ScrollView has been slid to the top (only when the click of the status bar lets ScrollView slide to the top)-(void) SCROLLVIEWDIDSCROlltotop: (Uiscrollview *) ScrollView; Called when the-setcontentoffset:animated:/-scrollrectvisible:animated: Method animation ends (called only if animated is set to Yes)-(void) Scrollviewdidendscrollinganimation: (Uiscrollview *) ScrollView;

Scaling related

Call
-(void) Scrollviewdidzoom when scaling changes: (Uiscrollview *) ScrollView;

A child view that participates in scaling
-(UIView *) Viewforzoominginscrollview: (Uiscrollview *) ScrollView;

Call when scaling starts
-(void) scrollviewwillbeginzooming: (Uiscrollview *) ScrollView Withview: (UIView *) view;

Call at end of scaling
-(void) scrollviewdidendzooming: (Uiscrollview *) ScrollView Withview: (UIView *) View Atscale: (cgfloat ) scale;

Analysis of common requirements principle

Navigation bar semitransparent Effect


Principle Analysis:

By default, when a uinavigationbar exists, the Contentinset and Scrollindicatorinsets properties of the system are set to UIEdgeInsetsMake(64, 0, 0, 0); prevent Uiscrollview from being blocked. In the presence of Uitabbar, the Contentinset and Scrollindicatorinsets properties of the system are set to prevent Uiscrollview from being obscured.UIEdgeInsetsMake(0, 0, 49, 0)

Therefore, in order to use this semitransparent effect, you can set the Uiscrollview frame to the size of the entire screen directly

Note 1: The system automatically modifies the Contentinset and Scrollindicatorinsets properties only when Uiscrollview is the No. 0 child view of the Controller view

NOTE 2: If you do not want the system to automatically modify the Contentinset and Scrollindicatorinsets properties, you can setself.automaticallyAdjustsScrollViewInsets = NO;

Control hover


Principle Analysis:

Mode one: Place a control in the hover position that is the same as the hover control, track the change of the -scrollViewDidScroll: Contentoffset by proxy method, set the control hidden property to Yes when the hover condition is not satisfied, and when the hover condition is satisfied, Set the control hidden property to No

Mode two: Through the -scrollViewDidScroll: agent method to track changes in Contentoffset, when the hover condition is not satisfied, the hover control belongs to the Uiscrollview child view, when the hover condition is satisfied, the hover control belongs to the Uiscrollview parent view of the child view

Take "mode two" as an example
-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView
{
 if (scrollview.contentoffset.y >=)
 {
  CGRect rect = label.frame;
  RECT.ORIGIN.Y = 0;
  Label.frame = rect;

  [Self.view Addsubview:label];
 }
 else
 {
  CGRect rect = label.frame;
  RECT.ORIGIN.Y = m;
  Label.frame = rect;

  [ScrollView Addsubview:label];
 }

Pull down the head image to enlarge


Principle Analysis:

-scrollViewDidScroll:tracking the changes of Contentoffset by proxy method and dynamically setting the zoom ratio of the picture according to Contentoffset

As an example of "dynamically modifying a picture scaling 1 time times and twice Times"
-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView
{
 CGFloat scale = 1 -(scrollview.contentoffset.y/100);
 Scale = (scale >= 1)? scale:1;
 Scale = (scale <= 2)? Scale:2;
 Imageview.transform = Cgaffinetransformmakescale (scale, scale);
}

Picture Infinite Carousel


Principle Analysis:

When the array of known images has n elements, a n+2 Uiimageview is created in the Uiscrollview, where the 1th to nth picture is true, the No. 0 is the same as the nth, and the first n+1 is the same as the 1th, through -scrollViewDidScroll: Agent method to track the changes in Contentoffset, sliding to the end of the two pictures directly set Contentoffset to the real picture can be

Summarize

The above is the entire content of this article, I hope the content of this article for your iOS developers can help, if you have questions you can message exchange.

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.