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

来源:互联网 发布:什么软件贷款最快 编辑:程序博客网 时间:2024/05/21 14:55

  • 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
原创粉丝点击