ios UIScrollView 分页显示

1.设置可以分页

     _scrollView.pagingEnabled = YES;

2.添加PageControl

    UIPageControl *pageControl = [[UIPageControl alloc] init];

    pageControl.center = CGPointMake(w * 0.5, h - 20);

    pageControl.bounds = CGRectMake(0, 0, 150, 50);

3.一共显示多少个圆点(多少页)

    pageControl.numberOfPages = kCount; 

4.设置非选中页的圆点颜色

    pageControl.pageIndicatorTintColor = [UIColor redColor];

5.设置选中页的圆点颜色

    pageControl.currentPageIndicatorTintColor = [UIColor blueColor];

6. 禁止默认的点击功能

    pageControl.enabled = NO;

7.UIScrollView的代理方法,当scrollView正在滚动的时候调用

- (void)scrollViewDidScroll:(UIScrollView *)scrollView


  1. MJViewController.h

    #import <UIKit/UIKit.h>

    @interface MJViewController : UIViewController

    @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

    @end

  2. 2

    MJViewController.m

    #import "MJViewController.h"

    #define kCount 8

    @interface MJViewController () <UIScrollViewDelegate>

    {

        UIPageControl *_pageControl;

    }

    @end

    @implementation MJViewController

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        

        CGFloat w = self.view.frame.size.width;

        CGFloat h = self.view.frame.size.height;

        for (int i = 0; i< kCount; i++) {

            UIImageView *imageView = [[UIImageView alloc] init];

            

            // 1.设置frame

            imageView.frame = CGRectMake(i * w, 0, w, h);

            

            // 2.设置图片

            NSString *imgName = [NSString stringWithFormat:@"0%d.jpg", i + 1];

            imageView.image = [UIImage imageNamed:imgName];

            

            [_scrollView addSubview:imageView];

        }

        

        // height == 0 代表 禁止垂直方向滚动

        _scrollView.contentSize = CGSizeMake(kCount * w, 0);

        _scrollView.showsHorizontalScrollIndicator = NO;

        _scrollView.pagingEnabled = YES;

        _scrollView.delegate = self;

        

        // 添加PageControl

        UIPageControl *pageControl = [[UIPageControl alloc] init];

        pageControl.center = CGPointMake(w * 0.5, h - 20);

        pageControl.bounds = CGRectMake(0, 0, 150, 50);

        pageControl.numberOfPages = kCount; // 一共显示多少个圆点(多少页)

        // 设置非选中页的圆点颜色

        pageControl.pageIndicatorTintColor = [UIColor redColor];

        // 设置选中页的圆点颜色

        pageControl.currentPageIndicatorTintColor = [UIColor blueColor];

        

        // 禁止默认的点击功能

        pageControl.enabled = NO;

        

        [self.view addSubview:pageControl];

        _pageControl = pageControl;

    }

    #pragma mark - UIScrollView的代理方法

    #pragma mark 当scrollView正在滚动的时候调用

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView

    {

        int page = scrollView.contentOffset.x / scrollView.frame.size.width;

    //    NSLog(@"%d", page);

        

        // 设置页码

        _pageControl.currentPage = page;

    }

    @end


创建多图像分页滚动:

滚动视图涉及的不仅仅是缩放 。通过UIScrollView 地分页属性,我们可将图像放在滚动视图中,并对它们一次移动一个视图地宽度,

关键是要确保加载地每幅图像水平方向上与滚动视图框架的宽度精确匹配,而在垂直方向上与其高度精确匹配


下面是代码:

#define COOKBOOK_PURPLE_COLOR    [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]

#define BASEHEIGHT    284.0f
#define NPAGES        3

- (void) viewDidLoad
{
    self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
    self.title = @"Image Scroller";
    
    // Create the scroll view and set its content size and delegate
    UIScrollView *sv = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, BASEHEIGHT)] autorelease];
    sv.contentSize = CGSizeMake(NPAGES * 320.0f, sv.frame.size.height);
    sv.pagingEnabled = YES;
    sv.delegate = self;
    
    // Load in all the pages
    int i=0;
    for(i; i < NPAGES; i++)
    {
        NSString *filename = [NSString stringWithFormat:@"image%d.png", i+1];
        UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename]];
        iv.frame = CGRectMake(i * 320.0f, 0.0f, 320.0f, BASEHEIGHT);
        [sv addSubview:iv];
        [iv release];
    }
    
    [self.view addSubview:sv];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值