zoukankan      html  css  js  c++  java
  • 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 ");
            
        }
        
    }
  • 相关阅读:
    未能加载包“Microsoft SQL Server Data Tools”
    SharePoint 客户端对象模型共用ClientContext的坑
    安装VisualStudio 2015 x64 中文企业版失败
    Could not load file or assembly 'Microsoft.SqlServer.Management.Sdk.Sfc, Version=11.0.0.0 系统找不到指定的文件。
    为Sharepoint 2010 批量创建SharePoint测试用户
    User Profile Service Application 配置同步连接时,报 MOSS MA not found
    SharePoint 2010 系统账户没完全控制权限了
    百度编辑器 UEditor 报错汇总
    更改SharePoint 2007/2010/2013 Web 应用程序端口号
    SharePoint 2013 报:网站在改进过程中处于只读状态,对此给您带来的不便,我们深表歉意
  • 原文地址:https://www.cnblogs.com/neozhu/p/2567506.html
Copyright © 2011-2022 走看看