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 can you scroll through a lot of content, but you can also zoom in on its content. 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 Uiscrollview When you use pinch gestures on your body, Uiscrollview sends a message to the agent asking the agent to scale which child control within itself (which piece of content)

when the user Uiscrollview When you use pinch gestures on your body, Uiscrollview will invoke the proxy's Viewforzoominginscrollview: method, the control returned by this method is the control that needs to be scaled.

Second, realize the zoom function

1. code example:

1 #import "YYViewController.h"2 3 @interfaceYyviewcontroller () <UIScrollViewDelegate>4 {5Uiscrollview *_scrollview;6Uiimageview *_imageview;7 }8 @end9 Ten @implementationYyviewcontroller One  A- (void) Viewdidload - { - [Super Viewdidload]; the      -     //1 Adding Uiscrollview -     //set Uiscrollview in the same location as screen size -_scrollview=[[Uiscrollview alloc]initwithframe:self.view.bounds]; + [Self.view Addsubview:_scrollview]; -      +     //2 Add a picture A     //There are two ways of at     //(1) General method - //Uiimageview *imageview=[[uiimageview Alloc]init]; - //UIImage *image=[uiimage imagenamed:@ "Minion"]; - //Imageview.image=image; - //imageview.frame=cgrectmake (0, 0, image.size.width, image.size.height); -      in     //(2) using the construction method -UIImage *image=[uiimage imagenamed:@"Minion"]; to_imageview=[[Uiimageview alloc]initwithimage:image]; +     //Call Initwithimage: Method, which creates the width of the ImageView and the width of the picture as high as - [_scrollview Addsubview:_imageview]; the      *     //set the scrolling range of the Uiscrollview to match the true dimensions of the picture $_scrollview.contentsize=image.size;Panax Notoginseng      -      the     //Setting Implementation Scaling +     //set proxy object for proxy ScrollView A_scrollview.Delegate=Self ; the     //Set maximum scaling ratio +_scrollview.maximumzoomscale=2.0; -     //set the minimum scaling scale $_scrollview.minimumzoomscale=0.5; $      - } -  the //tell ScrollView which child control to scale --(UIView *) Viewforzoominginscrollview: (Uiscrollview *) ScrollViewWuyi { the     return_imageview; - } Wu  - @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

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.