For the picture, in addition to the expression package, almost will be clicked to see a larger image. Let's take a look at the animated effect of viewing and collecting large images today:
, the first is a small map, click on the small map to see a larger image. The larger image will "bounce" from the position and size of the image, while the background becomes a translucent shadow. After clicking on a large image or shadow, close the large image, and then bounce back to the small image, and remove the shadow background as if it were a picture zoomed out.
Now look at how this is achieved. When thinking about an animation's implementation method, the animation of the action to decompose and then one after another to think about how to achieve is a good habit, we have a little decomposition, the animation in the display of large images and a large map to do these things:
- A translucent shadow background is displayed first when opened;
- It then displays a progressively larger picture until it is stretched to the edge of the screen;
- Let the shadow background disappear first;
- The image is then gradually smaller to the original size of the small picture.
This is actually quite simple, see how the code is implemented below.
First we define three properties, because we need to call in more than one method, so we define the @property of the class:
@property (nonatomicstrongUIImageView *smallImageView;// 小图视图@property (nonatomicstrongUIImageView *bigImageView;// 大图视图@property (nonatomicstrongUIView *bgView;// 阴影视图
Then we add the small picture directly to the interface:
- (void) Viewdidload {[SuperViewdidload];//Small map Self. Smallimageview= [[UiimageviewAlloc] Initwithframe:cgrectmake (ScreenWidth- -)/2, (ScreenHeight- -)/2, -, -)]; Self. Smallimageview. Image= [UIImageimagenamed:@"icon"];//Add Click Response Self. Smallimageview. userinteractionenabled=YES; UITapGestureRecognizer *imagetap = [[UITapGestureRecognizer alloc] Initwithtarget: SelfAction@selector(Viewbigimage)]; [ Self. SmallimageviewADDGESTURERECOGNIZER:IMAGETAP]; [ Self. ViewAddsubview: Self. Smallimageview];}
Note Here I used two pre-set constants when setting the size of the small graph: the height and width of the screen, so that the image is always centered according to the screen size of the phone, for these two constants, you can view this blog: iOS get screen width, device model, system version information
OK now the small figure has been added to the interface, we also added to the small map response to the click of the method, just to implement the animation in the response method can be. But before we do that, we'll start with the initialization of the big picture and the shadow background:
//Large Chart view- (Uiimageview*) Bigimageview {if(Nil= = _bigimageview) {_bigimageview = [[UiimageviewAlloc] Initwithframe:cgrectmake (0, (Screenheight-screenwidth)/2, ScreenWidth, ScreenWidth)]; [_bigimageview setimage: Self. Smallimageview. Image];//Set the click response of a large image here for a larger image_bigimageview. userinteractionenabled=YES; UITapGestureRecognizer *imagetap = [[UITapGestureRecognizer alloc] Initwithtarget: SelfAction@selector(Dismissbigimage)]; [_bigimageview Addgesturerecognizer:imagetap]; }return_bigimageview;}//Yin Movie View- (UIView*) Bgview {if(Nil= = _bgview) {_bgview = [[UIViewAlloc] Initwithframe:cgrectmake (0,0, ScreenWidth, ScreenHeight)]; _bgview. BackgroundColor= [UicolorColorwithwhite:0Alpha0.5];//Set the click response of the shadow background, here is a large image to be closed_bgview. userinteractionenabled=YES; UITapGestureRecognizer *bgtap = [[UITapGestureRecognizer alloc] Initwithtarget: SelfAction@selector(Dismissbigimage)]; [_bgview Addgesturerecognizer:bgtap]; }return_bgview;}
You can see that we used two methods alone to initialize the large and shaded backgrounds, the size of the large image is set to vertical center, the width is exactly the same as the screen, the height and width are the same, is a square. The shadow background occupies the entire screen. At the same time, I also set the two views of the corresponding method, are the animation method of the large image, we then go to implement. Now, let's get started with the animation that shows the big picture.
//Display large image- (void) Viewbigimage {[ SelfBigimageview];//Initialize large image //Jean-Datou starting from the location and size of the small picture CGRectOriginfram = _bigimageview. Frame; _bigimageview. Frame= Self. Smallimageview. Frame; [ Self. ViewAddsubview:_bigimageview];//Animation to large image of the size[UIViewAnimatewithduration:0.3animations:^{//change size_bigimageview. Frame= Originfram;//Change position_bigimageview. Center= Self. View. Center;//Set center position to new location}];//Add a negative view[ SelfBgview]; [ Self. ViewAddsubview:_bgview];//Put big picture on top, otherwise it will be covered by shadow added[ Self. ViewBringsubviewtofront:_bigimageview];}
Look at the code, we first called the large map of the initialization method, but note that at this point, there is no large image added to the interface, if this time add, will directly display the large map, before, we first saved the size of the large map itself, and then set its size position as a small graph exactly, and then add it to the interface, From the location and size of the small picture, to the original size of the animation to the big picture, it looks like the smaller image enlarged into a large map, right. Here's the animation we're using is the simplest iOS 7-based UIView animation supported by the block, which is explained in detail in my blog: iOS basic animation tutorial
Then we initialize the shadow background view and add it to the interface, and don't forget to manually push the big picture to the top, otherwise it will be overwritten by the shadow view added later.
Here, the animation to show the big picture is over, quite simple, then we look at the animation of the big picture, the basic is to put the above steps upside down once.
//Close up big picture- (void) Dismissbigimage {[ Self. BgviewRemovefromsuperview];//Remove Shadow //Animate the large image back to the location and size of the small chart[UIViewAnimatewithduration:0.3animations:^{//change size_bigimageview. Frame= Self. Smallimageview. Frame;//Change position_bigimageview. Center= Self. Smallimageview. Center;//Set center position to new location}];//delay execution, move back and then wipe out DoubleDelayinseconds =0.3; __block viewcontroller* bself = Self; dispatch_time_t poptime = Dispatch_time (Dispatch_time_now, (int64_t) (Delayinseconds * nsec_per_sec)); Dispatch_after (Poptime, Dispatch_get_main_queue (), ^ (void) {[bself. BigimageviewRemovefromsuperview]; Bself. Bigimageview=Nil; Bself. Bgview=Nil; });}
We first remove the shadow background and then animate the large image back to the size and position of the thumbnail, which looks as if it were zoomed out into small plots. Then we used a delay function to make sure that after the picture shrinks back to the small image, the image is removed and the animation is guaranteed.
At this point, we have finished our entire animation. In this example, the picture is placed centrally in the center, you can also try to put the small map in other places, in fact, the real app is rarely centered, and from other places scaling zoom to reduce the effect will be more interesting. Of course, if the small map location is not good to get, it is directly set to start from the midpoint of the screen to zoom, the effect is good. In addition, you may wonder why I want to add an object of a larger image without directly animating the size of the small image. In fact, it is completely possible, just in my project there is this demand, so I came directly to say Ha ha.
Here is my example project: Https://github.com/Cloudox/ViewBigImageDemo
All rights reserved: Http://blog.csdn.net/cloudox_
iOS Click to view animation effect of large image