首页 > 其他 > 详细

UI开发----UIScrollView和UIPageControl简单模拟滚动视图

时间:2015-04-20 22:38:28      阅读:227      评论:0      收藏:0      [点我收藏+]

 //  Created By 郭仔

//==================================================

师傅领进门,修行在个人!自学才是王道!

//==================================================

UIScrollView:

UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(20, 50, 200, 200)];
    
    scrollView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:scrollView];
    // 设置内容大小,当contentSize大于scrollView.frame.size才能滑动
    scrollView.contentSize = CGSizeMake(1000, 1000);
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 50, 50)];
    view.backgroundColor = [UIColor redColor];
    [scrollView addSubview:view];
    scrollView.showsHorizontalScrollIndicator = YES;
    scrollView.showsVerticalScrollIndicator =  YES;
   // scrollView.contentOffset = CGPointMake(50, 50);
  //  scrollView.pagingEnabled = YES;
    UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(200, 50, 50, 50)];
    view2.backgroundColor = [UIColor blackColor];
    [scrollView addSubview:view2];
    [view2 release];
    scrollView.bounces = YES;)
    scrollView.scrollsToTop = YES;
    scrollView.scrollEnabled = YES;
    scrollView.alwaysBounceHorizontal = NO;
    scrollView.alwaysBounceVertical = YES;
    scrollView.minimumZoomScale = 0.5;
    scrollView.maximumZoomScale = 2;
    scrollView.zoomScale = 1.5;
    // 设置代理
    scrollView.delegate = self;
如果要实现缩放功能,则需要遵循UIScrollViewDelegate,设置自身为代理。

例如:

- (UIView *)
            viewForZoomingInScrollView:
            (UIScrollView *)scrollView
{
   // NSLog(@"%@",NSStringFromCGSize(scrollView.contentSize));
    return [scrollView.subviews objectAtIndex:0];
   
}


// ==================================================================
UIPageView:

 UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(50, 400, 200, 50)];
    pageControl.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:pageControl];
    
    pageControl.numberOfPages = 5;
    pageControl.currentPage = 2;
    pageControl.pageIndicatorTintColor = [UIColor blueColor];
    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    [pageControl addTarget:self action:@selector(pageControlChange:) forControlEvents:UIControlEventValueChanged];
    
// ==================================================================

滚动视图的模拟:

UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    //
    _scrollView = scrollView;
    [self.view addSubview:scrollView];
    scrollView.pagingEnabled = YES;
    //
    scrollView.delegate = self;
    
    scrollView.contentSize = CGSizeMake(320*5, 568);
    // 把图片名放到数组里
    NSArray * imagesName = [NSArray arrayWithObjects:@"1.png",@"2.png",@"3.png",@"4.png",@"5.png", nil];
    
    for (int i = 0; i< imagesName.count; i ++)
    
    {
        
        
        UIImage * img = [UIImage imageNamed:[imagesName objectAtIndex:i]];
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*320, 0, 320, 568)];
        imageView.image = img;
        

        [scrollView addSubview:imageView];
        [imageView release];
    }
    
        UIPageControl * pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(50, 400, 200, 50)];
        pageControl.numberOfPages = 5;
        [pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
        pageControl.pageIndicatorTintColor = [UIColor redColor];
        pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
        //
        self.pageControl = pageControl;
        [self.view addSubview:pageControl];
        [pageControl release];

    
    [scrollView release];
    
方法的实现:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint offSet =  scrollView.contentOffset;
    int pageCount = offSet.x/320;
    _pageControl.currentPage = pageCount;
    
}

- (void)pageChanged:(UIPageControl *)control
{
    long currentPage = control.currentPage;
    CGPoint offSet = CGPointMake(currentPage * 320, 0);
    // 设置scrollView的偏移量
    [self.scrollView setContentOffset:offSet animated:YES];
}
//================================================================
思想思想还是思想,你懂的~~~~~~~~



UI开发----UIScrollView和UIPageControl简单模拟滚动视图

原文:http://blog.csdn.net/guoxianzhuang/article/details/45156681

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!