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
// 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
相关文章推荐
- 写在Demo战斗系统之前,先用原型工具做套UI第二篇-人物选择界面制作
- IOS GCD queue group
- 无法使用以下搜索标准找到 X.509 证书: StoreName“My”、StoreLocation“LocalMachine”、FindType“FindBySubjectName”、FindValue“MyWebSite”。
- easyui 获取指定列的值,格式化该列的值的实例
- iOS中添加UITapGestureRecognizer手势识别后,UITableView的didSelectRowAtIndexPath失效
- VisualSVN错误 Cannot query proxy blanket解决办法
- UISegmentControl 和UIslider
- QQuickImageProvider在QML设计中的应用
- 【干货】jsMind思维导图整合Easyui的右键菜单
- iOS UITableView删除cell分割线
- mediawiki和confluence
- UITapGestureRecognizer会屏蔽掉Button的点击事件
- 从ValueType讨论c#的值类型系统
- 玩转Android--UI篇--PreferenceActivity(开启wifi和音乐等)
- UIImage的2种加载方式
- label指定位置文字属性设置
- 深入掌握JMS(四):实战Queue
- JSP 获取Request 常用参数
- IOS入门学习笔记(普通UI控件--UISwitch、UITextField、UITextView、UISegmentedControl)
- 应用Druid监控SQL语句的执行情况