xcode uiscrollview with paging and zoom

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 particular 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 ViewController

int numberOfPhotos = 3;

int currentpage;

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // 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:@"iphonescroll%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;

    picScrollView.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:@"iphonescroll%d.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 setFrame: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 = 320*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 pageNumber = 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 ");

    }

}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏c#开发者

一种自动的将自定义类序列化为JSON的方法

最近因为项目需求,需要将一些自定义的类序列化为JSON,网上有很多好用的第三方序列化工具,但都只能自动序列化一些基本类型,如NSNumber,NSString与...

39570
来自专栏Albert陈凯

Programming with Objective-C方法命名规范

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Progr...

21430
来自专栏c#开发者

Iphone 视图跳转方法总结

1.AddInfo *control = [[AddInfo alloc] init];   [self presentModalViewController:...

35570
来自专栏c#开发者

ios5开发-UITableView开启编辑功能

该例子添加UITableView编辑功能 具体功能如下 ? 功能很简单但很实用  @implementation AppDelegate @synthesiz...

39070
来自专栏c#开发者

IOS5开发-http get/post调用mvc4 webapi互操作(图片上传)

目前最流行的跨平台交互是采用http协议通过JSON对象进行互操作。这种方式最简单,也很高效。webservice+xml的方式似乎已经过时。 下面是我做的一个...

71750
来自专栏c#开发者

IOS5开发-控件位置适应屏幕旋转代码

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation  ...

383110
来自专栏c#开发者

Infinite Scrolling in UIScrollView

- (void)viewDidLoad {     [super viewDidLoad];          [theScrollView setConten...

27660
来自专栏iOS技术杂谈

Python Garbage Collection 与 Objective-C ARCPython GC 与 Objective-C ARC

转载请注明出处 https://cloud.tencent.com/developer/user/1605429 Python GC 与 Objective-C...

34070
来自专栏c#开发者

Convert an object into Json using SBJson or other JSON library

Using SBJson, to convert a object to JSON string, you have to override the proxy...

35460

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励