iOS Development Foundation-uiscrollview for picture scaling

Source: Internet
Author: User

When the user uses the pinch gesture on uiscrollview , uiscrollview sends a message to the uiscrollviewdelegate protocol and invokes the proxy's Viewforzoominginscrollview: method, the control that the method returns is the control that needs to be scaled.

Steps to implement the Zoom feature:

1) Let the controller comply with Uiscrollview Agent Protocol uiscrollviewdelegate ;

2) Set the agent for Uiscrollview , _scrollview.  Delegate = self; ;

3) Call the Proxy method Viewforzoominginscrollview: , return the child controls that need to implement the Zoom function;

4) Set the zoom range, _scrollview.maximumzoomscale, _scrollview.minimumzoomscale .

Instance

Create a new single View application, let the Viewcontroller class comply with the uiscrollviewdelegate protocol, and add Uiscrollview to the class extension and Uiimageview properties, as follows:

1 // VIEWCONTROLLER.M 2 @interface Viewcontroller () 3 {4     Uiscrollview *_scrollview; 5     Uiimageview *_imageview; 6 }

The overloaded viewdidload method is as follows:

1- (void) Viewdidload {2 [Super Viewdidload];3     //Add Uiscrollview4_scrollview =[[Uiscrollview alloc] initWithFrame:self.view.bounds];5 [Self.view Addsubview:_scrollview];6     7     //Add a picture8    UIImage *image = [UIImage imagenamed:@ "beauty4.jpg"]; 9     //Initwithimage: Method creates the width of the _imageview and the same as the pictureTen     _imageview = [[Uiimageview alloc] initwithimage:image];  One [_scrollview Addsubview:_imageview]; A_scrollview.contentsize =image.size; -      -     //Set proxy Object the_scrollview.Delegate=Self ; -     //Set the maximum zoom-in ratio -_scrollview.maximumzoomscale =2.0; -_scrollview.minimumzoomscale =0.5; +}

Implement the Viewforzoominginscrollview: method to tell _scrollview that _imageview is a child control that needs to be scaled:

1 // VIEWCONTROLLER.M 2 -(UIView *) Viewforzoominginscrollview: (Uiscrollview *) ScrollView {3     NSLog (@ "  Achieve the zoom of the picture "); 4     return _imageview; 5 }

Add

The Code underline section above, the method of instantiating Uiimageview can be replaced by:

1 _imageview = [[Uiimageview alloc] init]; 2 UIImage *image = [UIImage imagenamed:@ "beauty4.jpg"]; 3 _imageview.image = image; 4 _imageview.frame = CGRectMake (00, Image.size.width, image.size.height);

Other proxy methods related to scaling:

-(void) scrollviewwillbeginzooming: (Uiscrollview *) ScrollView Withview: (UIView *) View Called when the zoom is complete.

-(void) Scrollviewdidzoom: (Uiscrollview *) called when ScrollView is scaling.

Add the following code to the original code:

1 -(void) scrollviewwillbeginzooming: (Uiscrollview *) ScrollView Withview: (UIView *) View { 2     NSLog (@ "%s", __function__); 3 }45 -(void) Scrollviewdidzoom: (Uiscrollview *) ScrollView { 6     NSLog (@ "%s", __function__); 7 }

To debug on a real machine, use pinch gestures to enlarge the image and output the following:

1  .- Geneva- -  -: *:07.780wjqscrollviewdemo[189:3549] -[Viewcontroller Viewforzoominginscrollview:]2  .- Geneva- -  -: *:07.781wjqscrollviewdemo[189:3549] -[Viewcontroller Scrollviewwillbeginzooming:withview:]3  .- Geneva- -  -: *:07.798wjqscrollviewdemo[189:3549]-[viewcontroller Scrollviewdidzoom:]

Reference Blog: iOS Development UI Chapter-uiscrollview controls for picture scaling

Code Download: http://vdisk.weibo.com/s/DiY98QyXCNtD9

iOS Development Foundation-uiscrollview for picture scaling

Related Article

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.