iOS Development UI Chapter-uiscrollview controls for picture scaling

Source: Internet
Author: User

iOS Development UI Chapter-uiscrollview controls to achieve picture scaling power

First, zoom

1. Brief description:

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 contents. That is, to complete the zoom function, simply add the content that needs to be scaled to the Uiscrollview

2. Scaling principle

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.

Second, realize the zoom function

1. code example:

 1 #import "YYViewController.h" 2 3 @interface Yyviewcontroller () <UIScrollViewDelegate> 4 {5 Uiscrollview *_ ScrollView 6 Uiimageview *_imageview; 7} 8 @end 9 @implementation YYViewController11-(void) ViewDidLoad13 {+ [super Viewdidload];15 16//1 Tim      Add UISCROLLVIEW17//Set Uiscrollview in the same location as screen size _scrollview=[[uiscrollview alloc]initwithframe:self.view.bounds];19 [Self.view addsubview:_scrollview];20 21//2 Add picture 22//There are two ways 23//(1) General way//Uiimageview *imagevie    W=[[uiimageview alloc]init];25//UIImage *image=[uiimage imagenamed:@ "Minion"];26//imageview.image=image;27// Imageview.frame=cgrectmake (0, 0, image.size.width, image.size.height); 28 29//(2) Using construction Method-UIImage *image=[uii Mage imagenamed:@ "Minion"];31 _imageview=[[uiimageview alloc]initwithimage:image];32//Call InitWithImage: Method, it is created by IM The width of the ageview and the width of the picture are as high as [_scrollview addsubview:_imageview];34 35//Set the scroll range of the Uiscrollview and the true ruler of the pictureInch consistent _scrollview.contentsize=image.size;37 38 39//set to achieve scaling 40//Set proxy ScrollView proxy object _scrollview.d ELEGATE=SELF;42//Set maximum scaling ratio _scrollview.maximumzoomscale=2.0;44//set minimum scaling ratio of _scrollview.minimumzoomscale     =0.5;46 47}48 49//Tell ScrollView which child control to scale-(UIView *) Viewforzoominginscrollview: (Uiscrollview *) scrollView51 {52 return _imageview;53}54 @end

2. Code description

4 Steps to implement the Zoom function

(1) Set agent for ScrollView (self)

(2) Allow the controller to comply with ScrollView's proxy protocol

(3) Calling the Proxy method, returning the child controls that need to implement the Zoom function

(4) Set the zoom range (max and min scale)

3. Ideas:

A. Need to tell ScrollView which child control to scale, here is the ImageView control inside the ScrollView

B. Who will tell ScrollView which control to scale? Agent

Iii. Supplementary Knowledge

Two ways to instantiate Uiimageview:

The first type:

Uiimageview *imageview=[[uiimageview Alloc]init];

UIImage *image=[uiimage imagenamed:@ "Minion"];

Imageview.image=image;

Imageview.frame=cgrectmake (0, 0, image.size.width, image.size.height);

The second type:

UIImage *image=[uiimage imagenamed:@ "Minion"];

_imageview=[[uiimageviewalloc]initwithimage:image];

Call Initwithimage: Method, which creates the width of the ImageView and the width of the picture as high as

[_scrollviewaddsubview:_imageview];

Iv. other proxy methods related to scaling

Call when Zoom is complete

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

is being scaled when calling the

-(void) Scrollviewdidzoom: (Uiscrollview *) ScrollView

iOS Development UI Chapter-uiscrollview controls for picture scaling

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.