UIScollerView&UIPageControl的一些使用方法

- (void)viewDidLoad

{

    CGRect rec = [[UIScreen mainScreen] bounds];

     mainScrollView= [[UIScrollView alloc] initWithFrame:rec];

        //设置实际内容区域大小(当前为水平滚动,高度设置为0是禁止垂直方向滚动)

    picsScrollView.contentSize = CGSizeMake(rect.size.width * 4, 0);

     picsScrollView.backgroundColor = [UIColor blackColor];

     //当值是 YES 会自动滚动到 subview 的边界。默认是NO(相当于启用分页功能,滚动视图为页面翻转效果)

    picsScrollView.pagingEnabled = YES;

    //滚动时是否显示水平滚动条,默认为YES

 picsScrollView.showsHorizontalScrollIndicator = NO;

    mainScrollView.delegate = self;

    

    [self.view addSubview:mainScrollView];

    //定义图片数组

    self.imageArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.jpg"],

                                                [UIImage imageNamed:@"2.png"],

                                                [UIImage imageNamed:@"3.jpg"], nil];

    [self settingScrollView];

    

    //分页控件

    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 400, 320, 0)];

    

    pageControl.numberOfPages = [self.imageArray count];

//    pageControl.pageIndicatorTintColor = [UIColor redColor];

//    pageControl.currentPageIndicatorTintColor = [UIColor blueColor];

    [self scrollViewDidScroll:mainScrollView];  

    

    [self.view addSubview:pageControl];

    

    self.navigationController.navigationBarHidden = NO;

    

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

#pragma mark-自定义方法

-(void)settingScrollView

{

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

        UIImageView *imageView = [[UIImageView alloc] initWithImage:[self.imageArray objectAtIndex:i]];

        

        imageView.frame = CGRectMake(i*mainScrollView.frame.size.width, 0, mainScrollView.frame.size.width, mainScrollView.frame.size.height);

        [mainScrollView addSubview:imageView];

    }

}

#pragma mark - scollerView协议方法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

//    self.navigationItem.title = [NSString stringWithFormat:@"(%.2f,%.2f)",scrollView.contentOffset.x,scrollView.contentOffset.y];

    

    NSInteger pageInt = scrollView.contentOffset.x / scrollView.frame.size.width;

    

    self.navigationItem.title = [NSString stringWithFormat:@"第%d张",pageInt+1];

    pageControl.currentPage = pageInt;

    pageControl.currentPageIndicatorTintColor = [UIColor blueColor];

}

期间

1,因为定义的UIScrollerView的实例变量为scrollView和委托方法中传入的参数重名,导致出现了下面的警告:Local declaration of 'scrollView' hides instance variable

2,因为粗心误认为UIScrollerView不需要实现协议,

3,scrollView.contentOffset指的是每一张内容显示界面左上角的坐标,

4,UIPageControll和UIScrollerView之间没有什么联系,





你可能感兴趣的:(UIScollerView&UIPageControl的一些使用方法)