前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >xcode uiscrollview with paging and zoom

xcode uiscrollview with paging and zoom

作者头像
阿新
发布2018-04-12 15:40:47
1.1K0
发布2018-04-12 15:40:47
举报
文章被收录于专栏:c#开发者

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 ");

    }

}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2012-06-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档