objective c - Symmetric zooming of image within scrollview in iOS -
i new iphone ios development. , want demo zoom in , zoom out (symmetric zooming of image) image within scrollview in ios.this code : in code have taken image view within scrollview(zoomscrollview) , scrollview within scrollview(scrollview_gallery).thanks in advance..
//zoomgalleryviewcontroller.m//
scrollview_gallery.backgroundcolor = [uicolor yellowcolor]; scrollview_gallery.delegate = self; scrollview_gallery.pagingenabled = yes; scrollview_gallery.userinteractionenabled = yes; scrollview_gallery.showshorizontalscrollindicator = no; scrollview_gallery.showsverticalscrollindicator = no; [self.view addsubview:scrollview_gallery]; if(ui_user_interface_idiom()==uiuserinterfaceidiomphone) { [scrollview_gallery setcontentsize:cgsizemake(self.view.frame.size.width*[arrayurl count],self.view.frame.size.height)]; } else if(ui_user_interface_idiom()==uiuserinterfaceidiompad) { [scrollview_gallery setcontentsize:cgsizemake([[uiscreen mainscreen] bounds].size.width*[arrayurl count],[[uiscreen mainscreen] bounds].size.height)]; } (int = 0; < [arrayurl count]; i++) { zoomscrollview = [[zoomscrollview alloc]init]; zoomscrollview.backgroundcolor = [uicolor redcolor]; [zoomscrollview setcontentsize:cgsizemake(zoomscrollview.contentsize.width,zoomscrollview.frame.size.height)]; cgrect frame = scrollview_gallery.frame; frame.origin.x = (frame.size.width)* i; frame.origin.y = 0; zoomscrollview.frame = frame; zoomscrollview.imageview.contentmode = uiviewcontentmodescaleaspectfit; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:[[arrayurl objectatindex:i]objectforkey:@"photopath"]]]; __weak uiimageview *weakimageview = zoomscrollview.imageview; zoomscrollview.imageview.tag = i; zoomscrollview.zoom_view_tag = zoomscrollview.imageview.tag; [zoomscrollview.imageview setimagewithurlrequest:request placeholderimage:nil success:^(nsurlrequest *request, nshttpurlresponse *response, uiimage *image) { nslog(@"image=%f,imageheight=%f",image.size.width,image.size.height); uiimageview *strongimageview = weakimageview; // make local strong reference protect against race conditions strongimageview.frame = cgrectmake(0,0,image.size.width*image.scale, image.size.height*image.scale); strongimageview.center = scrollview_gallery.center; nslog(@"image11 = %f, imageheight11 = %f",image.size.width*image.scale, image.size.height*image.scale); nslog(@"imagescale = %f",image.scale); if (!strongimageview) return; [uiview transitionwithview:strongimageview duration:0.3 options:uiviewanimationoptiontransitioncrossdissolve animations:^{ strongimageview.image = image; } completion:null]; //[scrollview_gallery setcontentoffset:cgpointmake(strongimageview.frame.size.width * (indexnumber+1), 0)]; }failure:^(nsurlrequest *request, nshttpurlresponse *response, nserror *error){ nslog(@"request failed error: %@", error); }]; [scrollview_gallery addsubview:zoomscrollview]; [zoomscrollview setcontentsize:cgsizemake(zoomscrollview.contentsize.width,zoomscrollview.frame.size.height)]; if(ui_user_interface_idiom()==uiuserinterfaceidiomphone) { [scrollview_gallery setcontentoffset:cgpointmake(self.view.frame.size.width * (indexnumber), 0)]; } else if(ui_user_interface_idiom()==uiuserinterfaceidiompad) { [scrollview_gallery setcontentoffset:cgpointmake([[uiscreen mainscreen] bounds].size.width * (indexnumber), 0)]; } // zoomscrollview.h// #import <uikit/uikit.h> @interface mrzoomscrollview : uiscrollview <uiscrollviewdelegate> { uiimageview *imageview; } @property (nonatomic, retain) uiimageview *imageview; @end
// mrzoomscrollview.m//
#import "mrzoomscrollview.h" #define mrscreenwidth cgrectgetwidth([uiscreen mainscreen].applicationframe) #define mrscreenheight cgrectgetheight([uiscreen mainscreen].applicationframe) @interface mrzoomscrollview (utility) - (cgrect)zoomrectforscale:(float)scale withcenter:(cgpoint)center; @end @implementation mrzoomscrollview @synthesize imageview; - (id)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self) { self.delegate = self; self.frame = cgrectmake(0, 0, mrscreenwidth, mrscreenheight); [self initimageview]; } return self; } - (void)initimageview { imageview = [[uiimageview alloc]init]; // imageview can zoomed largest size imageview.frame = cgrectmake(0, 0, mrscreenwidth * 2.5, mrscreenheight * 2.5); imageview.userinteractionenabled = yes; [self addsubview:imageview]; [imageview release]; // add gesture,double tap zoom imageview. uitapgesturerecognizer *doubletapgesture = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(handledoubletap:)]; [doubletapgesture setnumberoftapsrequired:2]; [imageview addgesturerecognizer:doubletapgesture]; [doubletapgesture release]; float minimumscale = self.frame.size.width / imageview.frame.size.width; [self setminimumzoomscale:minimumscale]; [self setzoomscale:minimumscale]; } #pragma mark - zoom methods - (void)handledoubletap:(uigesturerecognizer *)gesture { float newscale = self.zoomscale * 1.5; cgrect zoomrect = [self zoomrectforscale:newscale withcenter:[gesture locationinview:gesture.view]]; [self zoomtorect:zoomrect animated:yes]; } - (cgrect)zoomrectforscale:(float)scale withcenter:(cgpoint)center { cgrect zoomrect; zoomrect.size.height = self.frame.size.height / scale; zoomrect.size.width = self.frame.size.width / scale; zoomrect.origin.x = center.x - (zoomrect.size.width / 2.0); zoomrect.origin.y = center.y - (zoomrect.size.height / 2.0); return zoomrect; } #pragma mark - uiscrollviewdelegate - (uiview *)viewforzoominginscrollview:(uiscrollview *)scrollview { return imageview; } - (void)scrollviewdidendzooming:(uiscrollview *)scrollview withview:(uiview *)view atscale:(float)scale { [scrollview setzoomscale:scale animated:no]; }
look @ this tutorial, helpful me:
Comments
Post a Comment