Implement the image browser function
Effect:
This tutorial involves the use of many category. Note.
Ideas:
1. Obtain the frame value of a view in uiwindow.
2. Get the snapshot of this view
3. animation full screen for this snapshot
4. Remove the snapshot after the full screen disappears
Source code:
Nsobject + weakrelated. h and nsobject + weakrelated. m (Associate two objects)
/// Nsobject + weakrelated. h // scrollviewshowimage /// created by youxianming on 14-9-24. // copyright (c) 2014 youxianming. all rights reserved. // # import <Foundation/Foundation. h> @ interface nsobject (weakrelated) // bind with another object (weak correlation) @ property (nonatomic, weak) ID weakrelatedobject; @ end
/// Nsobject + weakrelated. M // scrollviewshowimage /// created by youxianming on 14-9-24. // copyright (c) 2014 youxianming. all rights reserved. // # import "nsobject + weakrelated. H "# import <objc/runtime. h> @ implementation nsobject (weakrelated) Static char values;-(void) setweakrelatedobject :( ID) weakrelatedobject {values (self, & signature, weakrelatedobject, objc_association_assign/** <specifies a weak reference to the associated object. */);}-(ID) weakrelatedobject {return objc_getassociatedobject (self, & customweakrelatedobject);} @ end
Uiview + convertrect. h and uiview + convertrect. m (the frame value of the current view in another view)
/// Uiview + convertrect. h // scrollviewshowimage /// created by youxianming on 14-9-24. // copyright (c) 2014 youxianming. all rights reserved. // # import <uikit/uikit. h> @ interface uiview (convertrect) // rect value of the current view in another view-(cgrect) rectinview :( uiview *) view; @ end
/// Uiview + convertrect. M // scrollviewshowimage /// created by youxianming on 14-9-24. // copyright (c) 2014 youxianming. all rights reserved. // # import "uiview + convertrect. H "@ implementation uiview (convertrect)-(cgrect) rectinview :( uiview *) view {return [self convertrect: Self. bounds toview: View];} @ end
Uiview + relativetouiscreen. h and uiview + relativetouiscreen. m (convert the size value of the current view to the frame value of another view)
/// Uiview + relativetouiscreen. h // scrollviewshowimage /// created by youxianming on 14-9-24. // copyright (c) 2014 youxianming. all rights reserved. // # import <uikit/uikit. h> @ interface uiview (relativetouiscreen) // return proportional size, but the width is the same as the screen width-(cgrect) scaleaspectfitscreenwidth; // return proportional size, but the height is the same as the screen height-(cgrect) scaleaspectfitscreenheight; // return proportional size for effective filling-(cgrect) scaleaspectfit; @ end
/// Uiview + relativetouiscreen. M // scrollviewshowimage /// created by youxianming on 14-9-24. // copyright (c) 2014 youxianming. all rights reserved. // # import "uiview + relativetouiscreen. H "# define scr_height [uiscreen mainscreen]. bounds. size. height # define scr_width [uiscreen mainscreen]. bounds. size. width @ implementation uiview (relativetouiscreen)-(cgrect) scaleaspectfitscreenwidth {If (self. bounds. size. height & self. bounds. size. width) {cgfloat calculate = scr_width * self. bounds. size. height/self. bounds. size. width; cgrect rect = cgrectmake (0, 0, scr_width, calculate); Return rect;} else {return cgrectzero;}-(cgrect) scaleaspectfitscreenheight {If (self. bounds. size. height & self. bounds. size. width) {cgfloat calculate = scr_height * self. bounds. size. width/self. bounds. size. height; cgrect rect = cgrectmake (0, 0, calculate, scr_height); Return rect;} else {return cgrectzero ;}- (cgrect) scaleaspectfit {If (self. bounds. size. height & self. bounds. size. width) {If (self. bounds. size. height/self. bounds. size. width)> (scr_height/scr_width) {cgfloat calculate = scr_height * self. bounds. size. width/self. bounds. size. height; cgrect rect = cgrectmake (0, 0, calculate, scr_height); Return rect;} else {cgfloat calculate = scr_width * self. bounds. size. height/self. bounds. size. width; cgrect rect = cgrectmake (0, 0, scr_width, calculate); Return rect ;}} else {return cgrectzero ;}@ end
Controller source code:
//// Viewcontroller. M // scrollviewshowimage /// created by youxianming on 14-9-24. // copyright (c) 2014 youxianming. all rights reserved. // # import "viewcontroller. H "# import" nsobject + weakrelated. H "typedef Enum: nsuinteger {enum_blackview = 0x11, enum_snapview,} viewtag; @ interface viewcontroller () @ property (strong, nonatomic) uiscrollview * scrollview; @ property (strong, nonatomic) uiimageview * imageview1; @ property (strong, nonatomic) uiimageview * imageview2; @ end @ implementation viewcontroller-(void) viewdidload {[Super viewdidload]; // scorllview _ scrollview = [[uiscrollview alloc] initwithframe: Self. view. bounds]; _ scrollview. contentsize = cgsizemake (self. view. bounds. size. width, self. view. bounds. size. height * 3); [self. view addsubview: _ scrollview]; // imageview1 + imageview2 _ imageview1 = [[uiimageview alloc] initwithimage: [uiimage imagenamed: @ "1.png"]; _ progress = cgpointmake (50, 50); _ imageview1.userinteractionenabled = yes; // enable user interaction and add gestures [_ scrollview addsubview: _ imageview1]; uitapgesturerecognizer * tap1 = [using alloc] initwithtarget: Self action: @ selector (tapmaxgestureevent :)]; [_ imageview1 addgesturerecognizer: tap1]; _ imageview2 = [[uiimageview alloc] initwithimage: [uiimage imagenamed: @ "2.png"]; _ imageview2.vieworigin = cgpointmake (50,500); _ imageview2.userinteractionenabled = yes; // enable user interaction and add a gesture [_ scrollview addsubview: _ imageview2]; optional * tap2 = [[financialloc] initwithtarget: Self action: @ selector (tapmaxgestureevent :)]; [_ imageview2 addgesturerecognizer: tap2];}-(void) tapmaxgestureevent :( upload *) tap {// obtain the uiwindow, and then go to the uiwindow * window = [@ "window" recoverfromweakdictionary]; // blacklist background uiview * blackview = [[uiview alloc] initwithframe: Self. view. bounds]; blackview. tag = enum_blackview; blackview. alpha = 0; blackview. backgroundcolor = [uicolor blackcolor]; [Window addsubview: blackview]; // copy view + tap gesture uiview * snapview = [tap. view snapshotviewafterscreenupdates: Yes]; snapview. tag = enum_snapview; snapview. frame = [tap. view rectinview: Window]; // copy the frame value of the view. alpha = 1; snapview. weakrelatedobject = tap. view; Parameters * newtap = [[using alloc] initwithtarget: Self action: @ selector (tapmingestureevent :)]; [[snapview addgesturerecognizer: newtap]; [Window addsubview: snapview]; // animation [uiview animatewithduration: 0.5f animations: ^ {blackview. alpha = 1.f; snapview. frame = [institutional scaleaspectfit. center = self. view. center;}];}-(void) tapmingestureevent :( optional *) tap {uiwindow * window = [@ "window" recoverfromweakdictionary]; uiview * view1 = [Window viewwithtag: enum_snapview]; uiview * view2 = [Window viewwithtag: enum_blackview]; uiview * tapview = tap. view; [uiview animatewithduration: 0.5f animations: ^ {tapview. frame = [tap. view. weakrelatedobject rectinview: Window]; view2.alpha = 0;} completion: ^ (bool finished) {[view2 removefromsuperview]; [view1 removefromsuperview];} @ end
Note:
In fact, this example is very convenient. You can easily click and browse images without encapsulating any interfaces.
Implement the image browser function