#import "ViewController.h" #import "YZUIScrollView.h" #define KUAN ([UIScreen mainscreen].bounds.size.width+20) # Define GAO [UIScreen mainscreen].bounds.size.height@interface viewcontroller () <uiscrollviewdelegate>@ Property (weak, nonatomic) Iboutlet Uiscrollview *huabu; @property (Nonatomic,strong) Nsarray *images; @property ( nonatomic) Nsinteger currentindex; @end @implementation viewcontroller//lazy Loading, initialize the property when the Get method is called-(Nsarray *) images{if (_ Images==nil) {Nsmutablearray *imagearray=[nsmutablearray array]; for (int i=1; i<7; i++) {nsstring *imagename=[nsstring stringwithformat:@ "new_feature_%d", I]; UIImage *image=[uiimage Imagenamed:imagename]; [Imagearray Addobject:image]; } _images=imagearray; } return _images;} -(void) viewdidload {[Super viewdidload]; Set Uiscrollview contentsize _huabu.contentsize=cgsizemake (kuan*3, GAO); Set paging _huabu.pagingenabled=yes; Hide horizontal scroll bar and vertical scroll bar _huabu.showshorizontAlscrollindicator=no; _huabu.showsverticalscrollindicator=no; Set the background color, highlight different pictures _huabu.backgroundcolor=[uicolor Blackcolor]; Set up agents to comply with agreements <UIScrollViewDelegate> _huabu.delegate=self; Call method Add child view [self tianjiazishitu]; Call method Add picture [self Tianjiatupian];} The way to add a child view-(void) tianjiazishitu{//album, is a large uiscrollview put a lot of small uiscrollview, but in order to save memory space, So just add three uiscrollview (image constantly changing position) for (int i=0; i<3; i++) {//create Yzuiscrollview Yzuiscrollview * yzuisv= [[Yzuiscrollview alloc] Initwithframe:cgrectmake (kuan*i, 0, kuan-20, Gao)]; Add Yzuiscrollview [_huabu ADDSUBVIEW:YZUISV]; Set tag value, easy to distinguish yzuisv.tag=1000+i; }}//Add a method picture-(void) tianjiatupian{Yzuiscrollview *leftsc= (Yzuiscrollview *) [_huabu viewwithtag:1000]; Yzuiscrollview *middlesc= (Yzuiscrollview *) [_huabu viewwithtag:1001]; Yzuiscrollview *rightsc= (Yzuiscrollview *) [_huabu viewwithtag:1002]; Leftsc.image=self.images[[self Indexfofenable:_currentindex-1]]; Middlesc.image=self.images[[self Indexfofenable:_currentindex]]; Rightsc.image=self.images[[self Indexfofenable:_currentindex+1]]; Set the offset, this step is important _huabu.contentoffset=cgpointmake (Kuan, 0);} Ensure that the index is available-(Nsinteger) indexfofenable: (Nsinteger) index{if (index<0) {return self.images.count-1; } else if (index>self.images.count-1) {return 0; } else return index; After scrolling is finished, restore all zoom view scales to 1.0-(void) scrollviewdidenddecelerating: (Uiscrollview *) scrollview{for (Id obj in _ Huabu.subviews) {if ([obj Iskindofclass:[uiscrollview class]]) {Uiscrollview *scalesc= (uiscrollv Iew *) obj; scalesc.zoomscale=1.0; }}//To determine the left/right swipe//offset x is 0, that is, the left side of the picture is sliding, that is to see the image on the front of the previous if (scrollview.contentoffset.x==0) {///the corresponding image should be a left-hand image _currentindex--; }//Offset x is two screen wide, which means swipe left, that is, the picture to the right before the other (scrollview.contentoffset.x== kuan*2) {///the corresponding image should be the image to the right _currentindex++; } _CURrentindex=[self Indexfofenable:_currentindex]; [Self Tianjiatupian];} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
A second class
#import "YZUIScrollView.h" @interface Yzuiscrollview () <UIScrollViewDelegate>
Picture of the @property (nonatomic,strong) UIImage *image;//content View
@property (Nonatomic,strong) Uiimageview *imageview; @end @implementation yzuiscrollview-(instancetype) initWithFrame :(CGRect) frame{if (self =[super initwithframe:frame]) {//Add content View Uiimageview *imageview1=[[uiimageview Alloc] initWithFrame:self.bounds]; [Self addsubview:imageview1]; _imageview=imageview1; Set maximum minimum multiplier and proxy self.minimumzoomscale=0.5; self.maximumzoomscale=1.5; self.delegate=self; Double-click event UITapGestureRecognizer *tap=[[uitapgesturerecognizer alloc] initwithtarget:self action: @selector (Shuangji:)] ; tap.numberoftapsrequired=2; [Self addgesturerecognizer:tap]; } return self;} -(void) Shuangji: (UITapGestureRecognizer *) tap{//When scaling is not 1.0, restore scaling if (Self.zoomscale!=1.0) {[Self setzooms cale:1.0 Animated:yes]; return; } cgpoint location =[tap locationinview:self]; CGRect rect =cgrectmake (location.x-100, location.y-100,200,200); [Self Zoomtorect:rect Animated:yes];} Overriding the Setimg Method-(void) SetImage: (UIImage *) image{//set itself the way to accomplish things must be done _image=image; Set the picture of the content view _imageview.image=image;} Uiscrollviewdelegate Proxy Method-(UIView *) Viewforzoominginscrollview: (Uiscrollview *) scrollview{return _imageview;} @end
iOS source OC album, you can cycle to see the picture