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

Posted liguangsunls

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用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下载












以上是关于使用UIScrollView和UIPageControl做一个能够用手势来切换图片的效果的主要内容,如果未能解决你的问题,请参考以下文章

使用 UIScrollView 和自动布局的图像滑块

使用 UIScrollView 和 NSNotificationCenter 继续上课

UIScrollView和UIPageControl的使用(实现图片的循环滚动)

使用 UIScrollView 进行放大和缩小视图

如何同时使用 UIScrollView 和 UITableView 实现平滑滚动?

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