UI-UIScrollView
来源:互联网 发布:租用网络服务器多少钱 编辑:程序博客网 时间:2024/05/28 11:47
UIScrollView控制内容显⽰
contentSize //定义内容区域⼤大⼩小,决定是否能够滑动 contentOffset //内容左上⾓角距离坐标原点的偏移量 contentInset//内容区域相对frame上下左右从哪⼉儿显⽰示
UIScrollView管理滚动
scrollEnabled //判断控件是否能够滚动 directionalLockEnabled//限定滚动⽅方向 showsHorizontalScrollIndicator//控制是否显⽰示⽔水平⽅方向的滚动 showVerticalScrollIndicator //控制是否显⽰示垂直⽅方向的滚动条 scrollsToTop //滑动到顶部(点状态条的时候) pagingEnabled//是否整屏翻动 bounces//边界是否回弹
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated; // scroll so rect is just visible (nearest edges). nothing if rect completely visible
#define KCount 8 // 图片个数
#define kWidth self.view.bounds.size.width // 根视图的宽度
#define kHeight self.view.bounds.size.height // 根视图的高度
@interface RootViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSMutableArray *array; // 数组来放image
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// // 定时器循环
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(runTimePage) userInfo:nil repeats:YES];
#pragma mark - scrollView的定制
_scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_scrollView];
// 取消显示水平滑动条
_scrollView.showsHorizontalScrollIndicator = NO;
// 设置分页效果
_scrollView.pagingEnabled = YES;
// 开启用户视图交互
_scrollView.userInteractionEnabled = YES;
// 设置scrollView的代理
_scrollView.delegate = self;
#pragma mark - 设置图片
_array = [NSMutableArray arrayWithCapacity:1];
for (int i = 0; i < KCount; i++) {
NSString *imageName = [NSString stringWithFormat:@"0%d.jpg",i+1];
UIImage *image = [UIImage imageNamed:imageName];
[_array addObject:image];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake((i+1) * kWidth, 0, kWidth, kHeight);
[_scrollView addSubview:imageView];
}
// 取出最后一张图片放第一张
UIImageView *ImageView = [[UIImageView alloc] initWithImage:[_array lastObject]];
ImageView.frame = CGRectMake(0, 0, kWidth, kHeight);
[_scrollView addSubview:ImageView];
// 取出最后一张放到第一张
ImageView = [[UIImageView alloc] initWithImage:[_array firstObject]];
ImageView.frame = CGRectMake((KCount+1) * kWidth, 0, kWidth, kHeight);
[_scrollView addSubview:ImageView];
// 设置scrollView的可视范围
_scrollView.contentSize = CGSizeMake((KCount+2) * kWidth, kHeight);
// scrollView显示到 设置可视范围内的区域
[_scrollView scrollRectToVisible:CGRectMake(kWidth, 0, kWidth, kHeight) animated:NO];
#pragma mark - PageControl的设置
_pageControl = [[UIPageControl alloc] init];
// 设置中点
_pageControl.center = CGPointMake(kWidth / 2, kHeight - 20);
// 设置页数
_pageControl.numberOfPages = KCount;
// 没被选中原点的颜色
_pageControl.pageIndicatorTintColor = [UIColor blackColor];
// 被选中原点的颜色
_pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
// 绑定pageControl方法
[_pageControl addTarget:self action:@selector(turnPage) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_pageControl];
}
#pragma mark - scrollView拖动时调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// int page = floor((self.scrollView.contentOffset.x - kWidth/(KCount+2))/kWidth); // 向下取整
int page = (int)(self.scrollView.contentOffset.x/320) - 1;
_pageControl.currentPage = page;
}
#pragma mark - scrollview滚动减速结束时调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// int currentPage = floor((self.scrollView.contentOffset.x - kWidth/(KCount+2))/kWidth) + 1; // 得到屏幕显示图片的页数
int currentPage = (int)self.scrollView.contentOffset.x/320; // 和上面两行效果一样
if (currentPage==0)
{
[self.scrollView scrollRectToVisible:CGRectMake(kWidth * KCount,0,kWidth,kHeight) animated:NO]; // 序号0 最后1页
}
else if (currentPage == (KCount+1))
{
[self.scrollView scrollRectToVisible:CGRectMake(kWidth,0,kWidth,kHeight) animated:NO]; // 最后+1,循环第1页
}
}
#pragma mark - 实现定时器绑定方法
- (void)runTimePage
{
int page = _pageControl.currentPage; // 获取当前的图片
page++;
page = page > KCount-1 ? 0 : page; // 页数循环
_pageControl.currentPage = page;
[self turnPage];
}
#pragma mark - pageControl绑定的方法
- (void)turnPage
{
int page = _pageControl.currentPage; // 获取当前页数
[_scrollView scrollRectToVisible:CGRectMake((page + 1) * kWidth, 0, kWidth, kHeight) animated:NO];
}
contentSize //定义内容区域⼤大⼩小,决定是否能够滑动 contentOffset //内容左上⾓角距离坐标原点的偏移量 contentInset//内容区域相对frame上下左右从哪⼉儿显⽰示
UIScrollView管理滚动
scrollEnabled //判断控件是否能够滚动 directionalLockEnabled//限定滚动⽅方向 showsHorizontalScrollIndicator//控制是否显⽰示⽔水平⽅方向的滚动 showVerticalScrollIndicator //控制是否显⽰示垂直⽅方向的滚动条 scrollsToTop //滑动到顶部(点状态条的时候) pagingEnabled//是否整屏翻动 bounces//边界是否回弹
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated; // scroll so rect is just visible (nearest edges). nothing if rect completely visible
设置可视化区域
滚动相册的部分代码
#import "RootViewController.h"
#define KCount 8 // 图片个数
#define kWidth self.view.bounds.size.width // 根视图的宽度
#define kHeight self.view.bounds.size.height // 根视图的高度
@interface RootViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSMutableArray *array; // 数组来放image
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// // 定时器循环
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(runTimePage) userInfo:nil repeats:YES];
#pragma mark - scrollView的定制
_scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_scrollView];
// 取消显示水平滑动条
_scrollView.showsHorizontalScrollIndicator = NO;
// 设置分页效果
_scrollView.pagingEnabled = YES;
// 开启用户视图交互
_scrollView.userInteractionEnabled = YES;
// 设置scrollView的代理
_scrollView.delegate = self;
#pragma mark - 设置图片
_array = [NSMutableArray arrayWithCapacity:1];
for (int i = 0; i < KCount; i++) {
NSString *imageName = [NSString stringWithFormat:@"0%d.jpg",i+1];
UIImage *image = [UIImage imageNamed:imageName];
[_array addObject:image];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake((i+1) * kWidth, 0, kWidth, kHeight);
[_scrollView addSubview:imageView];
}
// 取出最后一张图片放第一张
UIImageView *ImageView = [[UIImageView alloc] initWithImage:[_array lastObject]];
ImageView.frame = CGRectMake(0, 0, kWidth, kHeight);
[_scrollView addSubview:ImageView];
// 取出最后一张放到第一张
ImageView = [[UIImageView alloc] initWithImage:[_array firstObject]];
ImageView.frame = CGRectMake((KCount+1) * kWidth, 0, kWidth, kHeight);
[_scrollView addSubview:ImageView];
// 设置scrollView的可视范围
_scrollView.contentSize = CGSizeMake((KCount+2) * kWidth, kHeight);
// scrollView显示到 设置可视范围内的区域
[_scrollView scrollRectToVisible:CGRectMake(kWidth, 0, kWidth, kHeight) animated:NO];
#pragma mark - PageControl的设置
_pageControl = [[UIPageControl alloc] init];
// 设置中点
_pageControl.center = CGPointMake(kWidth / 2, kHeight - 20);
// 设置页数
_pageControl.numberOfPages = KCount;
// 没被选中原点的颜色
_pageControl.pageIndicatorTintColor = [UIColor blackColor];
// 被选中原点的颜色
_pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
// 绑定pageControl方法
[_pageControl addTarget:self action:@selector(turnPage) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_pageControl];
}
#pragma mark - scrollView拖动时调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// int page = floor((self.scrollView.contentOffset.x - kWidth/(KCount+2))/kWidth); // 向下取整
int page = (int)(self.scrollView.contentOffset.x/320) - 1;
_pageControl.currentPage = page;
}
#pragma mark - scrollview滚动减速结束时调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// int currentPage = floor((self.scrollView.contentOffset.x - kWidth/(KCount+2))/kWidth) + 1; // 得到屏幕显示图片的页数
int currentPage = (int)self.scrollView.contentOffset.x/320; // 和上面两行效果一样
if (currentPage==0)
{
[self.scrollView scrollRectToVisible:CGRectMake(kWidth * KCount,0,kWidth,kHeight) animated:NO]; // 序号0 最后1页
}
else if (currentPage == (KCount+1))
{
[self.scrollView scrollRectToVisible:CGRectMake(kWidth,0,kWidth,kHeight) animated:NO]; // 最后+1,循环第1页
}
}
#pragma mark - 实现定时器绑定方法
- (void)runTimePage
{
int page = _pageControl.currentPage; // 获取当前的图片
page++;
page = page > KCount-1 ? 0 : page; // 页数循环
_pageControl.currentPage = page;
[self turnPage];
}
#pragma mark - pageControl绑定的方法
- (void)turnPage
{
int page = _pageControl.currentPage; // 获取当前页数
[_scrollView scrollRectToVisible:CGRectMake((page + 1) * kWidth, 0, kWidth, kHeight) animated:NO];
}
0 0
- UI-UIScrollView
- UI - UIScrollView
- UI -- UIScrollView用法
- UIScrollView 基础学习 - UI
- UI:UIScrollView滚动视图
- UI—MVC,UIScrollView
- UI 06 UIScrollView基础
- UI - UIScrollView和UIPageControl
- IOS UI控件-UIScrollView
- UI基础知识 -- UIScrollView UIPageControl
- UI基础-UIScrollView、UIPageControl
- UI-7-UIScrollView
- UI day 7 UIScrollView
- UI - UIScrollView、UIPageControl
- UI控件--UIScrollView
- iOS-UI控件-UIScrollView
- ios ui控件-UIScrollView封装
- iOS开发UI篇 -- UIScrollView
- 硅藻土的主要优点
- 设置文件大小
- 指针
- css页面通用样式初始化代码
- 模板元的简单学习
- UI-UIScrollView
- 关于代码实现activity的启动模式
- 管道式无土栽培育苗时应注意些什么?
- IOS应用开发08——巧用emoji表情代替图片
- 蛭石于水泥的使用
- ios--IB介绍(Interface Builder)
- 编程之美--3.10 分层遍历二叉树
- WR703 uboot/带Web的Mod版本/pepe2k 再次Mod版本/修改mac地址
- 进制转换,指针前奏,奇偶互换,左移,右移