UIPageControl, UIScrollView属性
#import "RootView.h"
@implementation RootView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self createSubviews];
}
return self;
}
- (void)createSubviews
{
self.backgroundColor = [UIColor blackColor];
// UIPageControl
UIPageControl *pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(50, 500, 200, 30)];
pageC.backgroundColor = [UIColor orangeColor];
// 需要设置个数才会生效
pageC.numberOfPages = 10;
// 赋值时 - 默认初始位置
// 读取时 - 当前所在位置
pageC.currentPage = 5;
// 未选中时点的颜色
pageC.pageIndicatorTintColor = [UIColor redColor];
// 选中时点的颜色
pageC.currentPageIndicatorTintColor = [UIColor blueColor];
[pageC addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
[self addSubview:pageC];
[pageC release];
// 2, UIScrollView - 重点!!!!!!!!
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.frame];
#pragma 重要, 核心属性
// 横向设置19的图片位
scroll.contentSize = CGSizeMake(self.frame.size.width * 19, 0);
// 使能按页翻动
scroll.pagingEnabled = YES;
// // 关闭边缘反弹
// scroll.bounces = NO;
#pragma 重要, 实用属性
// 偏移 - 当前显示的图片槽
scroll.contentOffset = CGPointMake(self.frame.size.width * 3, 0);
// 使能调到最顶端的功能
scroll.scrollsToTop = YES;
// 是否允许滑动(默认允许)
scroll.scrollEnabled = NO;
// 水平/垂直跟随条
scroll.showsHorizontalScrollIndicator = NO;
scroll.showsVerticalScrollIndicator = NO;
/****************************** 缩放部分**************************/
// 最小/最大缩放倍数
scroll.minimumZoomScale = 0.5;
scroll.maximumZoomScale = 10;
// 注意! 要实现缩放功能需要签订协议
scroll.delegate = self;
/********************************************************************/
[self addSubview:scroll];
[scroll release];
for (NSInteger i = 0; i < 19; i++) {
// 注意: 1. 先看图片序号的命名格式1 or 01; 2. 再看图片的起始序号0 or 1
NSString *fileName = [NSString stringWithFormat:@"image%ld", i + 1];
NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"jpg"];
UIImage *image = [UIImage imageNamed:filePath];
// 注意: imageV的x坐标
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height)];
imageV.image = image;
// for循环的最终目的 - 创建19个imageView, 分别添加到scroll的槽里
[scroll addSubview:imageV];
}
pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(50, 600, self.frame.size.width - 50 * 2, 30)];
pageC.numberOfPages = 19;
// 注意, page的起点与scroll的初始偏移量一样
pageC.currentPage = scroll.contentOffset.x / self.frame.size.width;
[pageC addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
[self addSubview:pageC];
[pageC release];
}
#pragma 重要, 一般结束状态的处理可在此方法中进行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"结束减速时触发");
NSLog(@"%f", scrollView.contentOffset.x);
}
/***************************** 缩放相关方法 ***************************/
// 允许哪个页面缩放
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView.subviews objectAtIndex:0];
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
[[scrollView.subviews objectAtIndex:0] setCenter:CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)];
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
NSLog(@"缩放操作时触发");
// 将缩放时图片的中心点设置为屏幕的中心点
[[scrollView.subviews objectAtIndex:0] setCenter:self.center];
}
/********************************************************************/
- (void)pageAction:(UIPageControl *)sender
{
}
UIScrollView中添加可用于缩放的subScrollView
#import "RootView.h"
@implementation RootView
- (void)dealloc
{
[_scrollV release];
[super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self createSubvuews];
}
return self;
}
- (void)createSubvuews
{
self.scrollV = [[UIScrollView alloc] initWithFrame:self.frame];
self.scrollV.contentSize = CGSizeMake(self.frame.size.width *19, 0);
for (NSInteger i = 0; i < 19; i++) {
// 创建subScrollV 为了缩放
UIScrollView *subScrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height)];
subScrollV.minimumZoomScale = 0.5;
subScrollV.maximumZoomScale = 5;
// 缩放比例
// subScrollV.zoomScale = 1.0;
subScrollV.delegate = self;
[self.scrollV addSubview:subScrollV];
[subScrollV release];
NSString *fileName = [NSString stringWithFormat:@"image%ld", i + 1];
NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"jpg"];
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
imageV.image = [UIImage imageWithContentsOfFile:filePath];
[subScrollV addSubview:imageV];
[imageV release];
}
// scrollV负责移动拖拽, subScrollV负责缩放
self.scrollV.delegate = self;
self.scrollV.pagingEnabled = YES;
[self addSubview:self.scrollV];
[_scrollV release];
}
#pragma 触摸的过程 - 开始拖拽(手指触摸) -> 结束拖拽(松开手指)-> 减速开始(回弹的过程) -> 减速结束(图片最终稳定下来)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
self.preIndex = scrollView.contentOffset.x / self.frame.size.width;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSInteger curIndex = self.scrollV.contentOffset.x / self.frame.size.width;
if (self.preIndex != curIndex) {
[[self.scrollV.subviews objectAtIndex:self.preIndex] setZoomScale:1.0];
}
// [[scrollView.subviews objectAtIndex:self.preIndex] setZoomScale:1.0];
// NSLog(@"所在页码%f", scrollView.contentOffset.x / self.frame.size.width);
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
// 保证缩放在中心位置
[scrollView.subviews.firstObject setCenter:self.center];
}
// 允许哪个视图缩放
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
// 当前的scrollView是subScrollView
return scrollView.subviews.firstObject;
}
@end
UIScrollViewDelegate中的各种方法
//scrollView滚动时,就调用该方法。任何offset值改变都调用该方法。即滚动过程中,调用多次
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"scrollViewDidScroll");
CGPoint point=scrollView.contentOffset;
NSLog(@"%f,%f",point.x,point.y);
// 从中可以读取contentOffset属性以确定其滚动到的位置。
// 注意:当ContentSize属性小于Frame时,将不会出发滚动
}
// 当scrollView缩放时,调用该方法。在缩放过程中,回多次调用
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
NSLog(@"scrollViewDidScroll");
float value=scrollView.zoomScale;
NSLog(@"%f",value);
}
// 当开始滚动视图时,执行该方法。一次有效滑动(开始滑动,滑动一小段距离,只要手指不松开,只算一次滑动),只执行一次。
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"scrollViewWillBeginDragging");
}
// 滑动scrollView,并且手指离开时执行。一次有效滑动,只执行一次。
// 当pagingEnabled属性为YES时,不调用,该方法
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
NSLog(@"scrollViewWillEndDragging");
}
// 滑动视图,当手指离开屏幕那一霎那,调用该方法。一次有效滑动,只执行一次。
// decelerate,指代,当我们手指离开那一瞬后,视图是否还将继续向前滚动(一段距离),经过测试,decelerate=YES
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"scrollViewDidEndDragging");
if (decelerate) {
NSLog(@"decelerate");
}else{
NSLog(@"no decelerate");
}
CGPoint point=scrollView.contentOffset;
NSLog(@"%f,%f",point.x,point.y);
}
// 滑动减速时调用该方法。
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"scrollViewWillBeginDecelerating");
// 该方法在scrollViewDidEndDragging方法之后。
}
// 滚动视图减速完成,滚动将停止时,调用该方法。一次有效滑动,只执行一次。
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"scrollViewDidEndDecelerating");
[_scrollView setContentOffset:CGPointMake(0, 500) animated:YES];
}
// 当滚动视图动画完成后,调用该方法,如果没有动画,那么该方法将不被调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
NSLog(@"scrollViewDidEndScrollingAnimation");
// 有效的动画方法为:
// - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated 方法
// - (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated 方法
}
// 返回将要缩放的UIView对象。要执行多次
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
NSLog(@"viewForZoomingInScrollView");
return self.imgView;
}
// 当将要开始缩放时,执行该方法。一次有效缩放,就只执行一次。
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{
NSLog(@"scrollViewWillBeginZooming");
}
// 当缩放结束后,并且缩放大小回到minimumZoomScale与maximumZoomScale之间后(我们也许会超出缩放范围),调用该方法。
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{
NSLog(@"scrollViewDidEndZooming");
}
// 指示当用户点击状态栏后,滚动视图是否能够滚动到顶部。需要设置滚动视图的属性:_scrollView.scrollsToTop=YES;
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
return YES;
}
// 当滚动视图滚动到最顶端后,执行该方法
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
NSLog(@"scrollViewDidScrollToTop");
}
判断uiscrollview是向上滚动还是向下滚动
int _lastPosition; //A variable define in headfile
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
int currentPostion = scrollView.contentOffset.y;
if (currentPostion - _lastPosition > 25) {
_lastPosition = currentPostion;
NSLog(@"ScrollUp now");
}
else if (_lastPosition - currentPostion > 25)
{
_lastPosition = currentPostion;
NSLog(@"ScrollDown now");
}
}
// 25 可以是任意数字,可根据自己的需要来设定。
// 升级版:到达顶部或底部时不会反弹
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int currentPostion = scrollView.contentOffset.y;
if (currentPostion - _lastPosition > 20 && currentPostion > 0) { //这个地方加上 currentPostion > 0 即可)
_lastPosition = currentPostion;
NSLog(@"ScrollUp now");
}
else if ((_lastPosition - currentPostion > 20) && (currentPostion <= scrollView.contentSize.height-scrollView.bounds.size.height-20) ){
_lastPosition = currentPostion;
NSLog(@"ScrollDown now");
}
}