UIScorllView
2016-04-15 本文已影响279人
letaibai
基本属性
- UIScrollView是一个可以滚动的控件,功能强大.在显示分页图片,商品列表,APP欢迎页等地方会经常用到.首先想使用uiscrollview必须先要创建一个,并且设置frame.
// 创建UIScrollView
UIScrollView *scrollView = [UIScrollView alloc] init];
// 设置UIScrollView的frame
self.scrollView.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height);
以下是UIScrollView的常用属性:
- contenSize:用来设置scrollview的滚动范围
self.scrollView.contentSize = CGSizeMake(CGFloat width, CGFloat height);
- contenOffset:用来设置scrollView内控件的偏移量
self.scrollView.contentOffset = CGPointMake(CGFloat x, CGFloat y);
- contentInset:用来设置scrollView控件的外偏移量,防止scrollview的控件被其他控件挡住
self.scrollView.contentInset = UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);
- showsHorizontalScrollIndicator:是否显示水平方向滚动条
// YES为显示,NO为不显示
self.scrollView.showsHorizontalScrollIndicator = YES/NO;
- :showsVerticalScrollIndicator:是否显示竖直方向滚动条
// YES为显示,NO为不显示
self.scrollView.showsVerticalScrollIndicator = YES/NO;
- 设置scrollView能否缩放
// 最小缩放比例
@property(nonatomic) CGFloat minimumZoomScale; // default is 1.0
// 最大缩放比例
@property(nonatomic) CGFloat maximumZoomScale;
代理模式
- 如果想要监听UIScrollView的滚动事件,需要成为scrollView的代理.并且实现
<UIScrollViewDelegate>
中的方法即可.
// 可通过以下代码设置代理,也可通过storyboard拖线的方式设置
self.scrolView.delegate = self;
注意:成为scrollView的代理后,需要遵守<UIScrollViewDelegate>
协议.
以下是<UIScrollViewDelegate>
协议中的常用的代理方法
// any offset changes
// scrollView滚动时
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
// any zoom scale changes
// scrollView完成缩放时
- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2);
// called on start of dragging (may require some time and or distance to move)
// 开始拖拽时
- (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 NS_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;
// called on finger up as we are moving
// 开始减速时(停止拖拽,scrollview惯性减速时)
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;
// called when scroll view grinds to a halt
// 停止减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
// called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
//停止滚动动画
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;
// return a view that will be scaled. if delegate returns nil, nothing happens
// 缩放scrollview里面的view时
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
// called before the scroll view begins zooming its content
// 开始缩放
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view NS_AVAILABLE_IOS(3_2);
// scale between minimum and maximum. called after any 'bounce' animations
// 停止缩放
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale;
// return a yes if you want to scroll to the top. if not defined, assumes YES
//是否允许scrollview滚动到顶部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;
// called when scrolling animation finished. may be called immediately if already at top
// 滚动到顶部时
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;
注意:想监听scrollView内部的事件,必须成为scrollView的代理,并实现上述代理方法中对应的方法.