UIScrollView to zoom in and out images and uiscrollview to zoom in

Source: Internet
Author: User

UIScrollView to zoom in and out images and uiscrollview to zoom in
UIScrollView for image enlargement and reduction


1. Add Scroll View to storyboard or use lazy loading to create UIScrollView

-(UIScrollView *) scrollView
{
If (_ scrollView = nil ){
_ ScrollView = [[UIScrollViewalloc] initWithFrame: self. view. bounds];
// Set proxy
_ ScrollView. delegate = self;
// Set the Maximum/minimum scaling ratio
_ ScrollView. maximumZoomScale = 2.0;
_ ScrollView. minimumZoomScale = 0.2;

[Self. viewaddSubview: _ scrollView];
}
Return _ scrollView;
}
Note: The frame size of UIScrollView is the same as that of the parent window view.
2. Add the imageView to UIScrollView-(UIImageView *) imageView.
{
If (_ imageView = nil ){
_ ImageView = [[UIImageViewalloc] init];

[Self. scrollViewaddSubview: _ imageView];
}
Return _ imageView;
}
3. Set the setter of the image // Image
-(Void) setImage :( UIImage *) image
{
_ Image = image;
// Set the image View content
Self. imageView. image = image;
// Enable the Image view to automatically resize Based on the Image
[Self. imageViewsizeToFit];

// Tell scrollView the actual content size
Self. scrollView. contentSize = image. size;
}
Note: Set the Image view to automatically adjust the size based on the image because the frame of UIImageView is not set.
[Self. imageView sizeToFit];
Tells scrollView the actual content size.
Self. scrollView. contentSize = image. size;
The difference between self. scrollView and _ scrollView.

Iv. Set image-(void) viewDidLoad
{
[SuperviewDidLoad];

// Set the image
Self. image = [UIImageimageNamed: @ "minion"];
}
V. Proxy Method for UIScrollView
# Proxy method of pragma mark-UIScrollView
/**
1> set proxy
2> the maximum and minimum scaling ratio is specified.
 
ScrollView can be scaled.
 
The "return value" of the proxy method is actually that the controller tells the scroll view that the UIImageView is to be scaled.
*/
// Tell ScrollView who is scaling the view. The specific scaling implementation is completed by ScrollView.
// 1> scrollView
-(UIView *) viewForZoomingInScrollView :( UIScrollView *) scrollView
{
Return self. imageView;
}

// 2> the scrolling view is about to scale. You do not need to write
-(Void) scrollViewWillBeginZooming :( UIScrollView *) scrollView withView :( UIView *) view
{
NSLog (@ "% s", _ func __);
}

// 3> scaling, usually not required
-(Void) scrollViewDidZoom :( UIScrollView *) scrollView
{
// NSLog (@ "% s", _ func __);
NSLog (@ "% @", NSStringFromCGAffineTransform (self. imageView. transform ));
}

// 4> complete scaling, usually not required
-(Void) scrollViewDidEndZooming :( UIScrollView *) scrollView withView :( UIView *) view atScale :( CGFloat) scale
{
NSLog (@ "% s", _ func __);
}

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.