iOS Development Uiscrollview

Source: Internet
Author: User

1 , Uiscrollview usage of :

(1) Add the content that needs to be displayed to Uiscrollview

(2) Set the Contentsize property of the Uiscrollview, tell Uiscrollview all the dimensions of the content, that is, tell it the range of scrolling (how far to roll, where to roll to the end)

2 , Uiscrollview solutions that cannot be scrolled

If Uiscrollview cannot scroll, this may be the reason:

(1) No scrolling range is set contentsize

Settings such as: Self.scrollView.contentSize = self.myimageView.frame.size;

Equivalent to: Self.scrollView.contentSize = self. MyimageView.image.size;

(2) scrollenabled = NO

(3) No Touch event Received: userinteractionenabled = no

(4) No cancellation of AutoLayout function (if you want to scrollview scrolling, you must cancel AutoLayout)

3 , Uiscrollview Properties of:

    • @property (nonatomic) Cgpoint contentoffset;
    • Ø This property is used to indicate the position of the Uiscrollview scroll
    • @property (nonatomic) cgsize contentsize;
    • Ø This attribute is used to indicate the size of the Uiscrollview content, the scrolling range (how far to roll)
    • @property (nonatomic) uiedgeinsets contentinset;
    • Ø This attribute can add additional scrolling area for 4 weeks of Uiscrollview

-po9

    • @property (nonatomic) BOOL bounces;
    • Set Uiscrollview If spring effect is required
    • @property (nonatomic,getter=isscrollenabled) BOOL scrollenabled;
    • Ø set whether Uiscrollview can scroll
    • @property (nonatomic) BOOL showshorizontalscrollindicator;
    • Ø whether to display horizontal scroll bar
    • @property (nonatomic) BOOL showsverticalscrollindicator;
    • Ø whether to display the vertical scroll bar

4 , Content scaling

There are times when we might want to zoom in on some content, as shown in:

Uiscrollview not only scrolls through a large amount of content, but also scales its content, meaning that to complete the zoom function, only the content that needs to be scaled is added to the Uiscrollview.

Uiscrollview principle of Scaling:

When the user uses a pinch gesture on Uiscrollview, Uiscrollview sends a message to the agent asking the agent to zoom in on which child control (which piece of content) it is inside.

When the user uses the pinch gesture on Uiscrollview, Uiscrollview invokes the proxy's Viewforzoominginscrollview: method, which is the control that the method returns is the one that needs to be scaled

Scaling implementation steps:

    1. Set id<uiscrollviewdelegate> delegate proxy object for Uiscrollview
    2. Set Minimumzoomscale: Minimum scale to zoom out
    3. Set Maximumzoomscale: Maximum scale magnification
    4. Let the proxy object implement the following method to return the view control that needs to be scaled

-(UIView *) Viewforzoominginscrollview: (Uiscrollview *) ScrollView;

Other proxy methods related to scaling:

    • Ø call when Zoom is complete

-(void) Scrollviewdidzoom: (Uiscrollview *) ScrollView

    • Ø is being scaled when the call

-(void) scrollviewwillbeginzooming: (Uiscrollview *) ScrollView Withview: (UIView *) view

5 , pagination

As long as the pageenabled property of the Uiscrollview is set to Yes,uiscrollview will be split into separate pages, the contents can be paginated. Paging is often used for picture carousel, such as ad strips, new version features.

Generally will cooperate with uipagecontrol enhance the paging effect, with the Uipagecontrol to make the picture carousel below the navigation dot.

Uipagecontrol The common properties of the control are as follows :

    • Ø Total number of pages

@property (nonatomic) Nsinteger numberofpages;

    • Ø the page number currently displayed

@property (nonatomic) Nsinteger currentpage;

    • Ø If you need to hide the page number indicator when there is only one page

@property (nonatomic) BOOL hidesforsinglepage;

    • Ø Color of other page indicator

@property (Nonatomic,retain) Uicolor *pageindicatortintcolor;

    • Ø the color of the current page indicator

@property (Nonatomic,retain) Uicolor *currentpageindicatortintcolor;

6 , Uiscrollview the agent ( Delegate )

Most of the time, we want to do certain things when uiscrollview is scrolling or scrolling to a certain location or stopping scrolling.

To accomplish these functions, the precondition is to be able to monitor the entire rolling process of the Uiscrollview.

When Uiscrollview a series of scrolling operations, it will automatically notify its agent (delegate) object, send the corresponding message to its agent, let the agent know its scrolling situation.

In other words, to listen to the Uiscrollview scrolling process, you must first set up a proxy object Uiscrollview, and then through the agent to know the Uiscrollview scrolling process.

Uiscrollview the delegate needs to be implemented is defined in the Uiscrollviewdelegate protocol, so in order to become Uiscrollview delegate, You must follow the Uiscrollviewdelegate protocol and rewrite the corresponding method in the protocol to listen to the Uiscrollview scrolling process.

Uiscrollview and Controller:

In general, set the Uiscrollview controller as the Uiscrollview delegate.

There are 2 ways to set the controller to Uiscrollview delegate:

( 1 ) by code ( Self is the controller)

Self.scrollView.delegate = self;

( 2 ) by Storyboard Drag Line (right-click Uiscrollview )

For example:

#import "MJViewController.h"

@interface Mjviewcontroller () <UIScrollViewDelegate>

@property (Weak, nonatomic) Iboutlet Uiscrollview *scrollview;

@property (Weak, nonatomic) Iboutlet Uiimageview *minionview;

@end

Agent \ Delegate

@implementation Mjviewcontroller

-(void) viewdidload

{

[Super Viewdidload];

Set Content Size

Self.scrollView.contentSize = self.minionView.frame.size;

Set up

Self.scrollView.delegate = self;

Setting the maximum and minimum zoom ratios

Self.scrollView.maximumZoomScale = 2.0;

Self.scrollView.minimumZoomScale = 0.2;

}

/**

* When the user starts to drag and drop the ScrollView, it is called

*/

-(void) scrollviewwillbegindragging: (Uiscrollview *) ScrollView

{

NSLog (@ "Start dragging-----");

}

/**

* As long as the scrollview is scrolling, it will be called

*/

-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView

{

NSLog (@ "----is rolling--%@", Nsstringfromcgpoint (Scrollview.contentoffset));

}

/**

* Called when the user uses the pinch gesture

*

* @return returned controls are controls that need to be scaled

*/

-(UIView *) Viewforzoominginscrollview: (Uiscrollview *) ScrollView

{

NSLog (@ "----start scaling");

return self.minionview;

}

/**

* will be called when zooming in

*/

-(void) Scrollviewdidzoom: (Uiscrollview *) ScrollView

{

NSLog (@ "----is scaling");

}

-(void) didreceivememorywarning

{

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

@end

iOS Development Uiscrollview

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.