Xcode uiscrollview with paging and zoom

Source: Internet
Author: User
Here is a simple and sample code that demonstrate the photo slide function with zoom using uiscrollview.






This is the first photo




Sliding



Zooming in particle photo
















1. First drag three photos to your project, size not important.


2 viewcontroller. H's code


# Import <uikit/uikit. h>

@ Interface viewcontroller: uiviewcontroller <uiscrollviewdelegate> {


Uiscrollview * bgscorllview; // background scrollview control the slide Paging
Uiview * bgview; // background view is on the bgscrollview

Uiimageview * picimageview; // The imageview for the photo where in the current screen

Uiscrollview * picscrollview; // The scrollview for the current screen to preform zoom Function

Uiimageview * preimageview; // The imageview for the left side of the current screen

Uiimageview * nextimageview; // The imageview for the right side of the current screen
}

@ End


3. Code for viewcontroller. m


At viewdidload
1. Define the contentsize of the view, it's for sliding and paging on the bgscrollview
2. Run loadpage

At loadpage

1. Remove all subview at bgview first to save memory

2. Load the current screen scroll view and imagview

3. Add imageview at both sides of current screen, so even slide before REACH that page, can see that images

4. Add Tag for the current screen scrollview, so it won't be confused for uiscrollview delegate-(void) scrollviewdidenddecelerating :( uiscrollview *) scrollview







# Import "viewcontroller. H "@ implementation viewcontrollerint numberofphotos = 3; int currentpage;-(void) didreceivememorywarning {[Super didreceivemorywarning]; // release any cached data, images, etc that aren't in use .} -(void) loadpage :( INT) page {currentpage = page; For (uiimageview * sview in bgview. subviews) {nslog (@ "removesubview % @", sview); [sview removefromsuperview];} nslog (@ "page number % D ", page); uiimage * image = [uiimage imagenamed: [nsstring stringwithformat: @" iphonescroll1_d.png ", page]; picscrollview = [[uiscrollview alloc] initwithframe: cgrectmake (page-1) * 320 + 10, 10,300,440)]; picscrollview. delegate = self; picscrollview. maximumzoomscale = 3; picscrollview. minimumzoomscale = 1; picscrollview. zoomscale = 1; picscrollview. clipstobounds = yes; picscrollview. bounces = yes; picscroll View. scrollenabled = yes; picscrollview. autoresizingmask = uiviewautoresizingflexibleheight; picscrollview. tag = 5; picimageview = [[uiimageview alloc] initwithimage: Image]; [picimageview setframe: cgrectmake (0, 0,300,440)]; [picscrollview addsubview: picimageview]; [bgview addsubview: picscrollview]; int nextpage = page + 1; uiimage * nextimage = [uiimage imagenamed: [nsstring stringwithformat: @ "IPHO Nescrollintod.png ", nextpage]; nextimageview = [[uiimageview alloc] initwithimage: nextimage]; [nextimageview setframe: cgrectmake (nextpage-1) * 320 + 10, 10,300,440)]; [bgview addsubview: nextimageview]; int prepage = page-1; uiimage * preimage = [uiimage imagenamed: [nsstring stringwithformat: @ "iphonescroll#d.png", prepage]; preimageview = [[uiimageview alloc] initwithimage: preimage]; [preimageview SETF Rame: cgrectmake (prepage-1) * 320 + 10, 10,300,440)]; [bgview addsubview: preimageview];} # pragma mark-view lifecycle-(void) viewdidload {[Super viewdidload]; // do any additional setup after loading the view, typically from a nib. nslog (@ "% @", [uiscreen mainscreen]); bgscorllview = [[uiscrollview alloc] initwithframe: [uiscreen mainscreen] bounds]; [self. view addsubview: bgscorllview]; int width = 3 20 * numberofphotos; bgview = [[uiview alloc] initwithframe: cgrectmake (0, 0, width, 460)]; [self loadpage: 1]; [bgscorllview setcontentsize: cgsizemake (width, 460)]; bgscorllview. pagingenabled = yes; bgscorllview. delegate = self; bgscorllview. backgroundcolor = [uicolor bluecolor]; [bgscorllview addsubview: bgview]; // [self. view addsubview: bgview];}-(uiview *) viewforzoominginscrollview :( uiscrollview *) Scrollview {// nslog (@ "viewforzooming scrollview tag % d", scrollview. tag); Return picimageview;}-(void) scrollviewdidzoom :( uiscrollview *) scrollview {// nslog (@ "scrollviewdidzoom scrollview tag % d % F", scrollview. tag, scrollview. zoomscale);}-(void) scrollviewdidenddecelerating :( uiscrollview *) scrollview {If (scrollview. tag = 0) {// nslog (@ "viewdisscroll length % F", scrollview. contentoffset. x); int page Number = floor (scrollview. contentoffset. X/320 + 0.5) + 1; // nslog (@ "page number % d", pagenumber); If (pagenumber! = Currentpage) {[self loadpage: pagenumber] ;}}/ *-(void) scrollviewdidscroll :( uiscrollview *) scrollview {// nslog (@ "scrollviewdidscroll scrollview tag % d ", scrollview. tag); If (scrollview. tag = 0) {// nslog (@ "viewdisscroll length % F", scrollview. contentoffset. x); int pagenumber = floor (scrollview. contentoffset. x/320 + 0.5) + 1; // nslog (@ "page number % d", pagenumber); [self loadpage: pagenumber] ;}} */-(void) scrollviewdidendscrollinganimation :( uiscrollview *) scrollview {If (scrollview. tag = 0) {// nslog (@ "viewdidendscroll ");}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.