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 @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
Called when scaling is about to begin
-(void) scrollviewwillbeginzooming: (Uiscrollview *) ScrollView Withview: (UIView *) view
is being scaled when calling the
-(void) Scrollviewdidzoom: (Uiscrollview *) ScrollView
Called when the zoom is over
-(void) scrollviewdidendzooming: (Uiscrollview *) ScrollView Withview: (UIView *) View Atscale: (cgfloat) scale;
iOS Development UI Chapter-uiscrollview controls for picture scaling