UIScrollView
contentOffset 滚动区域左上角与视图左上角的距离
frame 视图在屏幕中展示的大小
contentSize 视图内部,内容可以滚动的区域
其他常用属性和方法
scrollEnabled
pagingEnabled
showHorizontalScrollIndicator
setContentOffset:animated
-(instancetype)init{
self = [super init];
if(self){
self.view.backgroundColor = [UIColor lightGrayColor];
self.tabBarItem.title = @"推荐";
self.tabBarItem.image = [UIImage imageNamed:@"icon.bundle/like@2x.png"];
self.tabBarItem.selectedImage = [UIImage imageNamed:@"icon.bundle/like_selected@2x.png"];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.backgroundColor = [UIColor lightGrayColor];
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 5, self.view.bounds.size.height);
scrollView.showsHorizontalScrollIndicator = NO; //是否显示横向滚动条
scrollView.pagingEnabled = YES; // 是否支持翻页效果
NSArray *colorArray = @[[UIColor redColor], [UIColor blueColor], [UIColor grayColor],[UIColor greenColor], [UIColor yellowColor] ];
for(int i = 0;i < 5;i++){
UIView *view = [[UIView alloc] initWithFrame:(CGRectMake(scrollView.bounds.size.width * i,
0, scrollView.bounds.size.width,
scrollView.bounds.size.height))];
view.backgroundColor = [colorArray objectAtIndex:i];
[scrollView addSubview:view];
}
[self.view addSubview:scrollView];
}
常用UIScrollViewDelegate:
// 用于监听页面滚动,根据Offset做业务逻辑
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
// 页面拖拽,用于中断业务逻辑
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
// called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset API_AVAILABLE(ios(5.0));
// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
// 页面减速,页面停止时开始逻辑,如视频自动播放
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView; // called on finger up as we are moving
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView; // called when scroll view grinds to a halt
UIKit中的滚动视图
UIView(渲染绘制/子视图管理)——> UIScrollView(滚动功能)——> UITableView/UICollectionView(子视图布局/复用)
UIScrollViewDelegate(滚动相关回调)——> UICollectionViewDelegate/UITableVieDelegate(基于滚动,视图相关回调)