Last two times, the use of ScrollView is the most common in album use. This is true for every camera picture. The next step is to put the code on the line and readers can figure it out for themselves.
First create a Photoscrollview class, inherit from Uiscrollview, can not always write in Viewcontroller. Since it is a photo album, the pit capital is a picture and so on (except video), need is a global imageview to display pictures.
// Show Picture @property (nonatomic,strong) Uiimageview *imageview;
Overriding the initWithFrame method in PHOTOSCROLLVIEW.M
1 //overriding the Init method2- (ID) initWithFrame: (CGRect) Frame {3 if(self =[Super Initwithframe:frame]) {4 //Create a picture view5_imageview =[[Uiimageview alloc] initWithFrame:self.bounds];6 //display on the screen7 [self addsubview:_imageview];8 //Set up proxy9Self.Delegate=Self ;Ten //Set zoom in and zoom out multiple OneSelf.minimumzoomscale =0.5; ASelf.maximumzoomscale =2; - } - returnSelf ; the}
After you set up the agent, be sure to declare the agent in. h, or you will be warned of an error. In the above code we see the maximum minimum multiplier (custom) that can be set to zoom out, so why is it possible to display the changed picture after zooming? That's going to work. The following method:
1 -(UIView *) Viewforzoominginscrollview: (Uiscrollview *) ScrollView {2 // After zooming back to the image after the change looks like 3 return _imageview; 4 }5/ / Additional English document explanation 6//return a view that would be Scaled. If delegate returns nil, nothing happens
The next implementation in the Controller. I add the imported picture order to the array (the simplest method) and then you can display it. But there is no need to cycle, albums are generally not cycle.
The question is, how do you know the next picture after sliding? Then you must first record the index of the previous chart.
{ // record last picture index nsinteger _prepageindex;}
The specific code is implemented as follows:
1- (void) Viewdidload {2 [Super Viewdidload]; 3Uiscrollview *scrollview =[[Uiscrollview alloc] Initwithframe:[uiscreen mainscreen].bounds];4 //Scrollview.backgroundcolor = [Uicolor blackcolor]; 5 //Set the paging function6scrollview.pagingenabled =YES;7 //Set Content size8Scrollview.contentsize = Cgsizemake (Kscreenwidth *5, Kscreenheight- the);9 //ShowTen [Self.view Addsubview:scrollview]; One //Add a picture A for(inti =0; I <5; i + +) { -NSString *imagename = [NSString stringWithFormat:@"%d.jpg", I]; -UIImage *image =[UIImage imagenamed:imagename]; thePhotoscrollview *photoview = [[Photoscrollview alloc] Initwithframe:cgrectmake (i * kscreenwidth, -, Kscreenwidth, Kscreenheight-113)]; - //set up a picture -PhotoView.imageView.image =image; - //set to I, corresponds to the picture, when the i=0, the condition occurs + //Photoview.tag = i; -Photoview.tag = About+i; + [ScrollView Addsubview:photoview]; A } at //Set up proxy -ScrollView.Delegate=Self ; - //Show First picture -_prepageindex =0; -}
Where Kscreenwidth, Kscreenheight is a custom macro definition, easy to use, the reader knows no need to say (1, 2 have been used).
Here is the post-stop method:
1 //when the deceleration stops ,2- (void) Scrollviewdidenddecelerating: (Uiscrollview *) ScrollView {3 4 //Gets the index = Scrollview.contentoffset.x/kscreenwidth of the current picture;5Nsinteger CurrentPageIndex = scrollview.contentoffset.x/Kscreenwidth;6 if(CurrentPageIndex! =_prepageindex) {7 //Get previous picture (1. Use the tag value, so set Photoview.tag 2. Using offsets)8Photoscrollview *photoview = (photoscrollview*) [ScrollView Viewwithtag:_prepageindex + About];9 //when you toggle a picture, restore the previous picture sizeTenPhotoview.zoomscale =1; One //Reset _prepageindex A_prepageindex =CurrentPageIndex; - } -}
Uo
We can see that there are horizontal, vertical scroll bars. And when you swipe to the last one, you can't slide again, but there's a bounce effect (you can cancel the bounce effect)
// whether bounce scrollview.bounces = no/YES; // whether the horizontal touch frame bounces scrollview.alwaysbouncehorizontal = no/YES; // whether the vertical touch frame bounces scrollview.alwaysbouncevertical = No/yes;
The album implementation principle is the above (just simple, not comprehensive) code.
Readers are welcome to check, please leave a message. If reproduced please indicate the source.
ScrollView (3)-Album View Simple zoom