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

 //  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];
}
//================================================================
思想思想还是思想,你懂的~~~~~~~~



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值