• 使用UIScrollView和UIPageControl做一个能够用手势来切换图片的效果


    利用UIScrollView的滚动效果来实现,先上图:




    实现过程是:在viewController里先增加UIScrollView和UIPageControl:

    -(void) loadView {
        [super loadView];
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320,480)];
        UIPageControl* pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 438, 320, 42)];
        self.scrollView = scrollView;
        self.pageControl = pageControl;
        self.pageControl.opaque = 1;
        [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
        
        [self.view addSubview:self.scrollView];
        [self.view addSubview:self.pageControl];
    
    }
    


    然后来初始化UIScrollView:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.contentList = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
        NSMutableArray* controllers = [[NSMutableArray alloc] init];
        for (unsigned i = 0 ; i < kNumberOfPages; i++) {
            [controllers addObject:[NSNull null]];
        }
        
        self.viewControllers = controllers;
        self.scrollView.pagingEnabled = YES;
        
        //contentSize是内容的宽度
        self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width*kNumberOfPages,
                                                 self.scrollView.frame.size.height);
    
        
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.scrollsToTop = NO;
        self.scrollView.delegate = self;
        
        self.pageControl.numberOfPages = kNumberOfPages;
        self.pageControl.currentPage = 0;
        
        [self loadScrollViewWithPage:0];
        [self loadScrollViewWithPage:1];
        // Do any additional setup after loading the view.
    }
    
    //UserImageViewController里仅仅有一个UIImageView
    -(void) loadScrollViewWithPage:(int)page{
        if (page < 0) {
            return;
        }else if(page >= kNumberOfPages) {
            //do some thing,like jump to other screen
            
            CalendarViewExample* calendarView = [[CalendarViewExample alloc] init];
            [self presentViewController:calendarView animated:YES completion:^{}];
            return;
        }
        
        UserImageViewController* controller = [self.viewControllers objectAtIndex:page];
        if ((NSNull*)controller == [NSNull null]) {
            controller = [[UserImageViewController alloc] init];
            [self.viewControllers replaceObjectAtIndex:page withObject:controller];
        }
        
        if (controller.view.superview == nil) {
            CGRect frame = self.scrollView.frame;
            frame.origin.x = frame.size.width * page;
            frame.origin.y = 0;
            controller.view.frame = frame;
            [self.scrollView addSubview:controller.view];
            
            
            NSString* name =[self.contentList objectAtIndex:page];
            NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"jpg"];
            controller.numberImage.image = [UIImage imageWithContentsOfFile:path];
           
            
        }
    }


    假设用手指来滚动的时候:

    -(void) scrollViewDidScroll:(UIScrollView *)scrollView{
        if (pageControlUsed) {
            
            return;
        }
        
        CGFloat pageWidth = scrollView.frame.size.width;
        //page表示当前滚动哪一页的标识
        int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth)+1;
        
        self.pageControl.currentPage = page;
        
        [self loadScrollViewWithPage:page-1];
        [self loadScrollViewWithPage:page];
        [self loadScrollViewWithPage:page+1];
        
    }
    
    -(void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
        pageControlUsed = NO;
    }
    
    -(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        pageControlUsed = NO;
    }
    

    然后假设切换到下一页:

    -(void) changePage:(id)sender{
        int page = self.pageControl.currentPage;
        NSLog(@"chanepage page=%d",page);
        
        [self loadScrollViewWithPage:page-1];
        [self loadScrollViewWithPage:page];
        
        
        CGRect frame = self.scrollView.frame;
        frame.origin.x = frame.size.width*page;
        frame.origin.y = 0;
        [self.scrollView scrollRectToVisible:frame animated:YES];
        
        pageControlUsed = YES;
    }
    代码能够在http://download.csdn.net/detail/baidu_nod/7547659下载


  • 相关阅读:
    【小程序】订阅消息
    【小程序】轮播图
    【小程序】全局变量的设置、使用、修改、全局方法执行
    【RN】标题栏右边添加自定义按钮或加事件
    【RN】阴影react-native-shadow
    【vue】点击复制到剪贴板的方法( clipboard )
    Q-learning和Sarsa的区别
    Q-learning之一维世界的简单寻宝
    使用tensorflow时,关于GPU的设置
    安装Matlab出现弹出DVD1插入DVD2的提示怎么办?
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6900676.html
Copyright © 2020-2023  润新知