iOS学习笔记-042.UIScrollView分页加强

UIScrollView分页加强

一、多图分页代码

//
//  ViewController.m
//  03_UIView33_UIScrollView分页
//
//  Created by 杞文明 on 16/2/24.
//  2016-02-24 22:41:36 星期三
//  Copyright © 2016年 杞文明. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property(weak,nonatomic)UIScrollView * scrollView;
// 分页控件
@property (weak, nonatomic) UIPageControl *pageControl;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.创建滑动视图   ------------------------------------------------
    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:scroll];
    self.scrollView = scroll;

    //2.创建图像数组   ------------------------------------------------
    NSMutableArray *imageArray = [[NSMutableArray alloc]initWithCapacity:5];
    for (NSInteger i=1; i<=5; i++) {
        //1.创建名字%
        NSString *imageName = [NSString stringWithFormat:@"%ld.jpg",i];
        //2.创建图像
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        //3.把图像添加到数组中
        [imageArray addObject:imageView];
    }


    //3.给scrollView设置图像数组 ------------------------------------------------
    CGFloat width = scroll.bounds.size.width;
   [imageArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //1.取出UIImageView
        UIImageView * imageView = obj;
        //2.设置位置
        [imageView setFrame:CGRectMake(idx*width, 0, width, scroll.bounds.size.height)];
        //3.添加到scrollView中
        [scroll addSubview:imageView];
   }];

    //4.设置滚动属性   ------------------------------------------------
    //1)允许分页
    [scroll setPagingEnabled:YES];

    //2)关闭弹簧效果
    [scroll setBounces:NO];

    //3)关闭水平滑动条
    [scroll setShowsHorizontalScrollIndicator:NO];

    //4)设置滑动区域大小
    [scroll setContentSize:CGSizeMake(scroll.bounds.size.width*imageArray.count, scroll.bounds.size.height)];

    //5)设置代理
    [scroll setDelegate:self];

    //5.添加分页控件    ------------------------------------------------
    UIPageControl * pageControl = [[UIPageControl alloc]init];

    //1)根据指定的页数,返回分页控件适合的大小
    CGSize size = [pageControl sizeForNumberOfPages:imageArray.count];
    [pageControl setFrame:CGRectMake(0, 0, size.width, size.height)];

    //2)设置分页控件的位置
    [pageControl setCenter:CGPointMake(width/2, 600)];

    //3)设置分页控件的页数
    [pageControl setNumberOfPages:imageArray.count];
    [pageControl setCurrentPage:0];

    //4)设置分页控件指示的颜色
    [pageControl setCurrentPageIndicatorTintColor:[UIColor redColor]];
    [pageControl setPageIndicatorTintColor:[UIColor blueColor]];

    //5)添加分页控件的监听方法
    [pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];

    //6)把分页控件添加到视图中
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
}

#pragma mark - 分页控件页码变化监听方法
-(void)pageChanged:(UIPageControl*)sender {
    NSLog(@"分页了----%ld", sender.currentPage);
    //根据变化的页数,计算scrollView中的contentOffset
    CGFloat offsetX = sender.currentPage*self.scrollView.bounds.size.width;
    [self.scrollView setContentOffset:CGPointMake(offsetX, 0)];
}

#pragma mark - UIScrollView代理方法
#pragma mark 滚动视图减速事件
-(void)scrollViewDidEndDecelerating:(nonnull UIScrollView *)scrollView{
    NSLog(@"页面停止滚动");
    //根据scrollView的contentOffset属性,判断当前的页码
    NSInteger pageNo = scrollView.contentOffset.x / scrollView.bounds.size.width;
    //设置页码
    [self.pageControl setCurrentPage:pageNo];
}
@end

二、图示

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值