//.h文件
@interface ViewController : UIViewController<UIScrollViewDelegate>{
UIScrollView *scrollView;
UIImageView *imageView;
}
//.m文件
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// CGRect *screenBounds = [[UIScreen mainScreen] bounds]; //返回整个屏幕的边界,包括状态栏所显示的空间
CGRect bounds = [[UIScreen mainScreen] applicationFrame]; //返回屏幕的可显示区域
scrollView = [[UIScrollView alloc] initWithFrame:bounds];
scrollView.delegate = self; //设置委托
// scrollView.backgroundColor = [UIColor redColor];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
[scrollView addSubview:imageView];//添加图片
scrollView.contentSize = imageView.frame.size; //设置内容的尺寸大小
//设置是否显示滑动条
scrollView.showsHorizontalScrollIndicator=NO;
scrollView.showsVerticalScrollIndicator=NO;
//pagingEnabled属性,页面翻转
scrollView.pagingEnabled = NO;
//缩放的最大最小值
scrollView.minimumZoomScale = 0.25;
scrollView.maximumZoomScale = 2.5;
//indicatorStyle指定滚动指示条类型
scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault; //滚动指示器类型,默认白边界上绘制黑色滚动条 3种
//directionalLockEnabled属性
scrollView.directionalLockEnabled = YES; //设置为YES将滚动锁定在一个方向上进行。
//bounces属性,YES时滚动到边缘可超为超出界面外一点然后弹回原位
scrollView.bounces = NO; //NO时滚动不会超出可见范围
//bouncesZoom属性,类似bounds 指的用户的缩放
// scrollView.bouncesZoom = NO; //NO时缩放不可超出最大最小缩放范围
//pagingEnabled属性,页面翻转
scrollView.pagingEnabled = NO;
// scrollView.contentOffset = YES;
self.view = scrollView;
}
//响应缩放方法
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return imageView;
}
/*********代理方法*********/
//在视图滚动时接到通知,包括一个指向被滚动视图的指针,从中可读取contentOffset属性已确定其滚动到的位置,常用
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
}
//拖动之前收到通知,可读取contentOffset
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
}
//用户抬起手指时得到通知,还会得到一个布尔值指明在报告滚动视图最后位置之前,手否需要进行减速
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
}
//当用户抬起手指为滚动视图需要继续滚动时收到通知,可读取contentOffset属性,可判断用户抬起手指前最后一次滚动到的位置,但不是最终位置
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
}
//当前一个提到的减速完毕、滚动视图停止移动时会得到通知,收到这个通知的时刻,滚动视图contentOffset属性会反映出滚动条最终停止位置
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
}
//用户进行缩放时会得到通知,缩放比例表示为一个浮点数,作为参数传递
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{
}
//当用户点触iPhone的状态条时滚动视图代理可以决定视图是否应滚定回到开头
-(BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
}
-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
}