示例代码
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *scrollView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(40, 40, 320, 460)];
self.scrollView.backgroundColor = [UIColor redColor];
// 是否支持滑动最顶端
// self.scrollView.scrollsToTop = NO;
self.scrollView.delegate = self;
// 设置内容大小,即滚动范围
self.scrollView.contentSize = CGSizeMake(320, 700);
// 是否反弹
// self.scrollView.bounces = NO;
// 是否分页
// self.scrollView.pagingEnabled = YES;
// 是否滚动
// self.scrollView.scrollEnabled = NO;
// self.scrollView.showsHorizontalScrollIndicator = NO;
// 设置indicator风格
// self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
// 设置内容的边缘和Indicators边缘
// self.scrollView.contentInset = UIEdgeInsetsMake(0, 50, 50, 0);
// self.scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 50, 0, 0);
// 提示用户,Indicators flash
[self.scrollView flashScrollIndicators];
// 是否同时运动,lock
self.scrollView.directionalLockEnabled = YES;
[self.view addSubview:self.scrollView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 320, 40)];
label.backgroundColor = [UIColor yellowColor];
label.text = @"学习UIScrolleview";
// 文字居中
label.textAlignment = NSTextAlignmentCenter;
[self.scrollView addSubview:label];
}
#pragma mark -
/*
// 返回一个放大或者缩小的视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
}
// 开始放大或者缩小
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:
(UIView *)view
{
}
// 缩放结束时
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
}
// 视图已经放大或缩小
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
NSLog(@"scrollViewDidScrollToTop");
}
*/
// 是否支持滑动至顶部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
return YES;
}
// 滑动到顶部时调用该方法
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
NSLog(@"scrollViewDidScrollToTop");
}
/*************************************缓慢拖动、快速拖动都会调用************************************/
// scrollView 开始拖动(刚有拖动的迹象就调用,即在调用scrollViewDidScroll之前就会调用scrollViewWillBeginDragging方法)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"scrollViewWillBeginDragging");
}
// scrollView 已经滑动(拖动就调用,只要你不停的拖动,这个方法就会调用无数次)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"scrollViewDidScroll");
}
// scrollView 结束拖动(松开鼠标停止拖动的那一瞬间调用(水平滚动ScrollView也调用,垂直滚动TableView也调用))
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(@"scrollViewDidEndDragging");
}
/************************************快速拖动才会调用*********************************************/
// scrollView 开始减速(必须得有快速拖动的动作,立马停止拖动就会调用。如果是缓慢拖动,停止拖动时不会调用这个方法)
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
NSLog(@"scrollViewWillBeginDecelerating");
}
// scrollview 减速停止(必须得有快速拖动的动作,scrollView滚动完毕(速度减为0)并且手已经松开的时候调用)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"scrollViewDidEndDecelerating");
}
@end
效果
-
缓慢拖动,快速拖动都会调用
-
快速拖动才会调用,因为只有快速拖动,减速的效果才明显
关键代码
/****************缓慢拖动、快速拖动都会调用*************/
// scrollView 开始拖动(刚有拖动的迹象就调用,即在调用scrollViewDidScroll之前就会调用scrollViewWillBeginDragging方法)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"scrollViewWillBeginDragging");
}
// scrollView 已经滑动(拖动就调用,只要你不停的拖动,这个方法就会调用无数次)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"scrollViewDidScroll");
}
// scrollView 结束拖动(松开鼠标停止拖动的那一瞬间调用(水平滚动ScrollView也调用,垂直滚动TableView也调用))
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(@"scrollViewDidEndDragging");
}
/********************快速拖动才会调用********************/
// scrollView 开始减速(必须得有快速拖动的动作,立马停止拖动就会调用。如果是缓慢拖动,停止拖动时不会调用这个方法)
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
NSLog(@"scrollViewWillBeginDecelerating");
}
// scrollview 减速停止(必须得有快速拖动的动作,scrollView滚动完毕(速度减为0)并且手已经松开的时候调用)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"scrollViewDidEndDecelerating");
}