IOS Uiscrollview Detailed & Picture zoom feature

Source: Internet
Author: User

A brief introduction to Uiscrollview

Uiscrollview is a scroll-capable view control that can be scrolled to show the class capacity.



Two Uiscrollview Common Properties
Set the position of the Uiscrollview scroll @property (nonatomic) Cgpoint contentoffset; Set the size of the Uiscrollview content, scroll range @property (nonatomic) cgsize contentsize; Set Uiscrollview for 4 weeks to add additional scrolling area @property (nonatomic) uiedgeinsets contentinset; Sets whether Uiscrollview requires a spring effect @property (nonatomic) BOOL bounces;//set Uiscrollview Whether it can scroll @property (nonatomic,getter= isscrollenabled) BOOL scrollenabled; Sets whether Uiscrollview displays a horizontal scrollbar @property (nonatomic) BOOL showshorizontalscrollindicator;// Sets whether Uiscrollview displays a vertical scrollbar @property (nonatomic) BOOL showsverticalscrollindicator;



Three Uiscrollview agent (delegate)

Uiscrollview during scrolling or when you stop scrolling, if you need to do something specific, you can use the proxy (delegate) to listen to the entire uiscrollview scrolling process. When a series of scrolling operations occur in Uiscrollview, its agent (delegate) object is automatically notified, and then the Uiscrollview scrolling process is monitored by the agent.

Uiscrollview the methods delegate need to implement are defined in the Uiscrollviewdelegate protocol, so Uiscrollview delegate must comply with the Uiscrollviewdelegate protocol, Then implement the corresponding method in the protocol, you can listen to the scrolling process of Uiscrollview

Called when the user starts dragging-(void) scrollviewwillbegindragging: (Uiscrollview *) scrollview;//is called when scrolling to a location-(void) Scrollviewdidscroll :(Uiscrollview *) scrollview;//call when user ends drag-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate :(BOOL) decelerate;



Four content Scaling

Uiscrollview not only scrolls through a large amount of content, but also scales its content

When using a pinch gesture on Uiscrollview, Uiscrollview invokes the Viewforzoominginscrollview method of the proxy (delegate) , which is the control that needs to be scaled. , the properties and methods involved in scaling

/**** attribute ****///reduced to small scale @property (nonatomic) cgfloat minimumzoomscale;//magnification maximum ratio @property (nonatomic) cgfloat    Maximumzoomscale; /**** method ****///is called when scaling-(UIView *) Viewforzoominginscrollview: (Uiscrollview *) scrollview;//when the zoom is started-(void) Scrollviewwillbeginzooming: (Uiscrollview *) ScrollView Withview: (UIView *) view//is being scaled when called-(void) scrollviewdidzoom :(Uiscrollview *) ScrollView



Five Uiscrollview solutions that cannot be scrolled

If Uiscrollview cannot scroll, the following may be the reason:

5.1 Contentsize property is not set

5.2 Setting Properties scrollenabled = NO

5.3 No Touch events received (userinteractionenabled = no)

5.4 Cancel the AutoLayout function, if you want to scrollview scrolling, you must cancel the AutoLayout



Six Code areas6.1 Code
#import   "ViewController.h" @interface  ViewController  () <uiscrollviewdelegate>/** * uiscrollview */@property (Nonatomic,strong) Uiscrollview *scrollview;/** *  uiimageview  */@property (nonatomic,strong) uiimageview *imageview; @end @implementation viewcontroller-  ( void) viewdidload {        //2. Settings  UIImageView     uiimage *image = [uiimage imagenamed:@ "Scroll.jpg"];     self.imageview.image = image;        //2.1  Setting picture range      CGFloat imageH = image.size.height;    CGFloat  imagew = image.size.width;    cgfloat imagex = 0;     CGFloat imageY = 0;    self.imageView.frame =  CGRectMake (Imagex, imagey,&nbsP;imagew, imageh);            //3  Set uiscrollview  Properties         //3.2  set the size of Uiscrollview content, scroll range     self.scrollview.contentsize=cgsizemake (Imagew, imageh);         //3.2  set Uiscrollview for 4 weeks to add additional scrolling area     cgfloat distance  = 100.0f;    self.scrollview.contentinset = uiedgeinsetsmake (distance ,  distance, distance, distance);        //3.3  Set Spring effect     self.scrollView.bounces = YES;         //3.4  set scrolling does not display     self.scrollView.showsHorizontalScrollIndicator=NO;     self.scrollView.showsVerticalScrollIndicator=NO;                     //4 uiimageview  Add to  UIScrollView       [self.scrollview addsubview:self.imageview];        / /5 uiscrollview    [self.view addsubview:self.scrollview];             //6  Set up proxy      self.scrollview.delegate = self;             //7  Zoom     self.scrollView.minimumZoomScale=0.2f;     self.scrollview.maximumzoomscale=2.0f;    } #pragma  mark  proxy method//  called when the user starts dragging-   (void) scrollviewwillbegindragging: (uiscrollview *) Scrollview{    nslog (@ "Start dragging") ;}   Call-  (void) Scrollviewdidscroll when scrolling to a location: (uiscrollview *) scrollview{     NSLog (@ "dragging");}   Call-  (void) SCR when user ends dragOllviewdidenddragging: (uiscrollview *) Scrollview willdecelerate: (BOOL) decelerate{     nslog (@ "End drag");} #pragma  mark  zoom/** *   call  * *   @param at end of Zoom  scrollView  < #scrollView  description#> * *   @return  < #return  value  description#> */-  (uiview *) Viewforzoominginscrollview: (uiscrollview *) scrollView{     nslog (@ "Start scaling");     return self.imageview;} /** *   calling  * *   @param  scrollView < #scrollView   during scaling description#> */-  (void) Scrollviewdidzoom: (uiscrollview *) scrollview{     nslog (@ "Scaling");} /** *   call  * *   @param  scrollView < #scrollView   at end of zoom description#> *   @param  view       < #view   Description#> *   @param  scale      < #scale  description#> */ -  (void) scrollviewdidendzooming: (uiscrollview *) Scrollview withview: (uiview *) view  Atscale: (cgfloat) Scale{    nslog (@ "Zoom End");} #pragma  mark  Properties Get method-  (uiscrollview *) scrollview{    if  (!_ ScrollView)  {        _scrollView = [[UIScrollView  Alloc] initwithframe:self.view.frame];    }    return _ ScrollView;} -  (uiimageview *) imageview{    if  (!_imageview)  {         _imageView = [[UIImageView alloc] init];     }    return _imageview;} @end


6.2 Show

After zooming before zooming

IOS Uiscrollview Detailed & Picture zoom feature

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.