您的位置:首页 > 产品设计 > UI/UE

iOS-UI-滚动视图2—分页查看功能

2015-07-29 14:02 375 查看
//

// ViewController.m

// UI-滚动视图2—分页查看

//

// Created by jzq_mac on 15/7/29.

// Copyright (c) 2015年 jzq_mac. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

NSArray *imageList = @[@"星星.png",@"n63img.jpeg",@"u=827694700,2201960268&fm=21&gp=0.jpg"];

//分页查看图片

UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];

scrollView .delegate = self;

//分页效果

scrollView.pagingEnabled = YES;

//设置滚动条的样式

scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;

//设置滚动视图的偏移量
可以达到滚动视图默认在第几屏 还可以通过contentOffset来判断滚动到第几屏

scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.view.frame), 0);

//设置是否有反弹的效果(默认值是YES
允许看到地图,并有反弹效果)

scrollView.bounces = NO;

//隐藏横向滚动条

scrollView.showsHorizontalScrollIndicator = NO;

for (int i = 0; i < imageList.count; i++) {

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*i , 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];

imageView.image = [UIImage imageNamed:imageList[i]];

[scrollView addSubview:imageView];

}

scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)*imageList.count, 0);

[self.view addSubview:scrollView];

// UIPageControl(页面控制器)

UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-40, CGRectGetWidth(self.view.frame), 20)];

pageControl.tag = 119;

//设置pageControl总共多少页面

pageControl.numberOfPages = imageList.count;

//设置指示的当前页面

pageControl.currentPage = 1;

//当只有一个页面的时候隐藏pageControl

pageControl.hidesForSinglePage = YES;

//设置轨道的颜色,小圆点

pageControl.pageIndicatorTintColor = [UIColor yellowColor];

//设置当前的圆点颜色

pageControl.currentPageIndicatorTintColor = [UIColor redColor];

[self.view addSubview:pageControl];

}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView

//{

// NSLog(@"%f",scrollView.contentOffset.x);

// //在哪一个页面,通过偏移量的x的位置/屏幕的宽度
可以的到当前的页数

// }

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

//得到一个当前页数;滚动视图的偏移量,屏幕的宽(持有的数据)

// x的偏移量

CGFloat x = scrollView.contentOffset.x;

// 屏幕的宽

CGFloat w = CGRectGetWidth(self.view.frame);

NSLog(@"%f",x);

//通过偏移量的x的位置/屏幕的宽度
可以的到当前的页数

NSInteger currentPage1 = x/w;

UIPageControl *pageControll = (UIPageControl *)[self.view viewWithTag:119];

// 设置当前页面

pageControll.currentPage = currentPage1;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: