View larger image
#import "HMViewController.h"@interfaceHmviewcontroller () <UIScrollViewDelegate>@property (Strong, nonatomic) Iboutlet Uiscrollview*ScrollView, @property (nonatomic, strong) Uiimageview*ImageView;//Suppose the image was taken from the network@property (nonatomic, strong) UIImage *image;@end@implementationHmviewcontroller/** Zoom in and out 1> set agent 2> Specify maximum/minimum zoom ratio*///the setter of the image- (void) SetImage: (UIImage *) image{_image=image; //set the contents of an image viewSelf.imageView.image =image; //make image view automatically resize based on image[Self.imageview SizeToFit]; //tell ScrollView the actual size of internal contentSelf.scrollView.contentSize =image.size;}/** In Getter Method * If it is the property itself, use the _ member variable * If it is another property, use the self. Getter method, which ensures that if the object is not instantiated, it can be created and loaded in a timely manner.*/-(Uiimageview *) imageview{if(_imageview = =Nil) {_imageview=[[Uiimageview alloc] init]; [Self.scrollview Addsubview:_imageview]; } return_imageview;}-(Uiscrollview *) scrollview{if(_scrollview = =Nil) {_scrollview=[[Uiscrollview alloc] initWithFrame:self.view.bounds]; //Setting Properties//Set Margins_scrollview.contentinset = Uiedgeinsetsmake ( -, -, -, -); //do not display horizontal scroll marks_scrollview.showshorizontalscrollindicator =NO; //do not display vertical scroll marks_scrollview.showsverticalscrollindicator =NO; //* * Offset position_scrollview.contentoffset = Cgpointmake (0,0); //cancels the spring effect, the content is fixed, do not want to appear the spring effect when//don't confuse the bounds attribute._scrollview.bounces =NO; //Set up proxy_scrollview.Delegate=Self ; //set maximum/minimum zoom ratio_scrollview.maximumzoomscale =2.0; _scrollview.minimumzoomscale=0.2; [Self.view Addsubview:_scrollview]; } return_scrollview;}- (void) viewdidload{[Super Viewdidload]; //Set ImageSelf.image = [UIImage imagenamed:@"Minion"]; UIButton*BTN =[UIButton Buttonwithtype:uibuttontypecontactadd]; Btn.center=Self.view.center; [Self.view ADDSUBVIEW:BTN]; [Btn addtarget:self Action: @selector (click) forcontrolevents:uicontroleventtouchupinside];}- (void) click{//move the offset position of a large imageCgpoint offset =Self.scrollView.contentOffset; Offset.x+= -; Offset.y+= -; //Note: Setting Contentoffset ignores ContentsizeSelf.scrollView.contentOffset =offset;}#pragmaProxy methods for Mark-uiscrollview/** 1> Set proxy 2> specifies the maximum and minimum scale representation ScrollView is the "return value" of the proxy method that can be scaled is actually the controller tells the scrolling view, to scale is Uiimageview*///tell ScrollView who the view to zoom to, the specific scaling implementation, is done by ScrollView//1> ScrollView to know who to scale-(UIView *) Viewforzoominginscrollview: (Uiscrollview *) scrollview{returnSelf.imageview;}//2> scrolling View is about to start zooming, usually without writing- (void) Scrollviewwillbeginzooming: (Uiscrollview *) ScrollView Withview: (UIView *) view{NSLog (@"%s", __func__);}//3> is scaling, and usually does not need to implement- (void) Scrollviewdidzoom: (Uiscrollview *) scrollview{//NSLog (@ "%s", __func__);NSLog (@"%@", Nsstringfromcgaffinetransform (Self.imageView.transform));}//4> to complete the scaling, and usually does not need to implement- (void) Scrollviewdidendzooming: (Uiscrollview *) ScrollView Withview: (UIView *) View Atscale: (cgfloat) scale{NSLog (@"%s", __func__);}@end
iOS sixth day (1:scrollview properties and view larger image)