iOS基础知识

UIScorllView

2016-04-15  本文已影响279人  letaibai

基本属性

// 创建UIScrollView
UIScrollView *scrollView = [UIScrollView alloc] init];
// 设置UIScrollView的frame
 self.scrollView.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height);

以下是UIScrollView的常用属性:

self.scrollView.contentSize = CGSizeMake(CGFloat width, CGFloat height);
self.scrollView.contentOffset =  CGPointMake(CGFloat x, CGFloat y);
self.scrollView.contentInset = UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);
// YES为显示,NO为不显示
self.scrollView.showsHorizontalScrollIndicator = YES/NO;
// YES为显示,NO为不显示
self.scrollView.showsVerticalScrollIndicator = YES/NO;
// 最小缩放比例
@property(nonatomic) CGFloat minimumZoomScale;  // default is 1.0
// 最大缩放比例
@property(nonatomic) CGFloat maximumZoomScale; 

代理模式

// 可通过以下代码设置代理,也可通过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的代理,并实现上述代理方法中对应的方法.

上一篇下一篇

猜你喜欢

热点阅读