iOS--UIScrollView usage

UIScrollView inherits from UIView and is a very commonly used framework encapsulated by Apple. UIScrollView is mainly used to display content that exceeds the fixed size of the View. You can use UIScrollView to slide up, down, left, and right, and use two fingers to zoom in and out. The scope of use includes carousel images, etc. For example, the WeChat chat list uses UITableview, which is inherited from UIScrollview, and slides within a fixed mobile phone screen area to view more content. Here is an introduction to the specific usage of UIScrollView:

Method to create UIScrollView:

    UIScrollView *scrollview = [[UIScrollView alloc] init];
    scrollview.frame = CGRectMake(0, 0, 414, 280);
    [self.view addSubview:scrollview];

UIScrollView custom settings some properties (important):

    //contentSize属性是设置UIScrollView内可以装载多大的空间
    scrollview.contentSize = CGSizeMake(414*3, 600);
    
    //contentOffset属性是设置UIScrollView内加载视图的偏移量,例如想让轮播图向右播放第二张,就设置这个属性
    scrollview.contentOffset = CGPointMake(0, 0);
    
    //contentInset属性是设置UIScrollView内视图与scrollview内边界的距离,设置顺序是top-left-bottom-right
    scrollview.contentInset = UIEdgeInsetsMake(5, 10, 15, 20);
    
    //获取属性是设置UIScrollView.contentInset的值,就是上面偏移的值,返回一个结构体类型
    UIEdgeInsets edgeInsets = scrollview.adjustedContentInset;
    
    //UIScrollView是否按照页(默认以scrollview的宽度或高度为一页)
    scrollview.pagingEnabled= YES;
    
    //UIScrollView是否可以滑动
    scrollview.scrollEnabled = YES;
    
    //UIScrollView是否显示右侧边界显示的竖向指示条
    scrollview.showsVerticalScrollIndicator = YES;
    
    //UIScrollView是否显示底侧边界显示的横向指示条
    scrollview.showsHorizontalScrollIndicator = YES;
    
    //UIScrollView是否显示侧边界显示的指示条的颜色样式
    scrollview.indicatorStyle =UIScrollViewIndicatorStyleWhite;
    
    //UIScrollView最小缩小的倍数
    scrollview.minimumZoomScale = 0.5;

    //UIScrollView最大放大的倍数
    scrollview.maximumZoomScale = 3;

Some proxy methods of UIScrollView custom implementation (important):

First of all, you must comply with the <UIScrollViewDelegate> protocol. The methods in this protocol are all @optional types.


// any offset changes:UIScrollView内视图移动时调用此方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
}

// any zoom scale changes:UIScrollView内视图放大或缩小时调用此方法
- (void)scrollViewDidZoom:(UIScrollView *)scrollView API_AVAILABLE(ios(3.2)){
    
}

// called on start of dragging:UIScrollView内视图即将拖动时调用此方法
- (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
//UIScrollView内视图即将停止拖动时调用此方法,带有两个参数:CGPoint移动的点和CGPoint offset点
- (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
//UIScrollView内视图停止拖动后调用此方法,带有一个参数(BOOL)decelerate:如果后续继续移动,返回YES
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
}

// called on finger up as we are moving
// 手指抬起时,UIScrollView内视图移动速度开始减弱时调用
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    
}

// called when scroll view grinds to a halt
//UIScrollView内视图移动停止移动时调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
}

// called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
// 当UIScrollView内视图移动画结束时调用,如果没有设置动画,则不会调用。
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
}

// called before the scroll view begins zooming its content
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view API_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
//UIScrollView是否回到顶部(最初的位置),返回YES会执行返回至顶部动作。
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
    return  YES;
}

//UIScrollView是返回至顶部动作结束时调用。
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
    
}

Guess you like

Origin blog.csdn.net/JustinZYP/article/details/124313286