#import "RootViewController.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@interface RootViewController () <UIScrollViewDelegate>
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
/**
UIScorllView 滑动控件的基类,可以支持滚动,用于当内容页区域大于可视区域大小时,可以通过滚动来查看整个内容区域,他的UITableView,UITextView的父类
*/
//1.创建控件
UIScrollView *scrolView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 30, kScreenWidth - 20 *2, kScreenHeight - 30 * 2)];
//2.配置属性
scrolView.backgroundColor = [UIColor redColor];
//1设置内容页的大小
scrolView.contentSize = CGSizeMake(scrolView.bounds.size.width, scrolView.bounds.size.height);
//2设置偏移量
scrolView.contentOffset = CGPointMake(400, 0);
//3隐藏欢动指示器
// scrolView.showsHorizontalScrollIndicator = NO; //水平
// scrolView.showsVerticalScrollIndicator = NO; // 竖直
//4控制scrollView能否滑动
scrolView.scrollEnabled = YES;
//5方向锁,控制滑动时只能单方向滑动
scrolView.directionalLockEnabled = NO;
//6控制点击状态条时,scroll是否可以回到顶部,y的偏移量为0;
scrolView.scrollsToTop = YES;
//7控制当前内容页到边界是,是否具有弹跳效果.
scrolView.bounces = YES;
//8控制即使内容页区域大小 小于等于 scrollView大小时,依然有反弹效果
scrolView.alwaysBounceHorizontal = YES;
//9代理
/**
* 对于协议和代理的使用步骤:(一对多 --- 一个代理对象可以做多个任务(方法))
1.针对自定义协议:
1>自定义协议(存放让代理实现的任务,分为可选和必须实现)
2>定义代理属性(用于存储代理对象,id类型 --要服从协议,语义特性为assign)
3>在其他文件中为该类设置代理(指定任务由谁完成)
4>让代理对象所属的类服从协议(答应干活)
5>代理对象实现协议中的方法(制定任务完成的细节)
6>委托方通知代理去执行协议中的方法,(让代理干活)
2.无需自定义协议:
1>在其他类中为该类指定代理(指定任务由谁来做)
2>让代理对象所属的类服从协议(代理答应任务由他来做)
3>代理对象实现协议中的方法(制定任务完成的细节)
*/
scrolView.delegate = self;
//10和缩放视图有关的的属性
scrolView.minimumZoomScale = 1.0;
scrolView.maximumZoomScale = 4.0;
//3.添加到父视图
[self.view addSubview:scrolView];
//4.释放
[scrolView release];
//添加到UIImageView
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"3.jpg"]];
imageView.tag = 200;
imageView.userInteractionEnabled = YES;
imageView.frame = CGRectMake(0 , 0, scrolView.contentSize.width, scrolView.contentSize.height);
[scrolView addSubview:imageView];
[imageView release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
*/
//只要拖拽就会触发,拖拽改变偏移量
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//打印偏移量
// NSLog(@"offst = %@",NSStringFromCGPoint(scrollView.contentOffset));
}
// any offset changes Zoom缩放
//只要缩放就会触发,改变大小
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
//打印内容页的大小
NSLog(@"contentSize = %@",NSStringFromCGSize(scrollView.contentSize));
}
//当scrollView将要拖拽时,此时手指刚刚接触到屏幕
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//滑动结束 回到最左边
[scrollView setContentOffset:CGPointZero animated:YES];
}
//当scrollView 开始加速时触发,此时手指已经离开视图(不一定触发,有加速过程才会触发该方法)
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
}
// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
//当scrollView减速结束是触发,已经停止滑动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
}
//当scrollView将要结束拖拽是接触
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
}// called on finger up as we are moving
//当scrollView计数拖拽时触发,此时手指已经离开视图
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
}// called when scroll view grinds to a halt
//当滑动scrollView ,并且动画结束时触发(前提是必须要有动画效果)
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
}// called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
//返回需要缩放的视图,但是只能是scrollView的子视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
NSLog(@"%s,%d",__FUNCTION__ , __LINE__);
return [scrollView viewWithTag:200];
}
//当将要开始缩放时触发
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{
}
//缩放结束时触发
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
}
//当scrollsToTop属性设置为YES时,通过该方法进一步询问,点击状态条是否可以回到顶部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
NSLog(@"%s,%d",__FUNCTION__ , __LINE__);
return YES;
}// return a yes if you want to scroll to the top. if not defined, assumes YES
//当点击状态条,scrollView回到顶部时触发
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
NSLog(@"%s,%d",__FUNCTION__ , __LINE__);
}// called when scrolling animation finished. may be called immediately if already at top
@end