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 __);
}