iOS--UIScrollView用法

UIScrollView继承于UIView,是苹果封装的一个很常用的框架。UIScrollView主要是用来展示超过固定尺寸View的内容,可以再UIScrollView中操作上下左右滑动,两个手指放大和缩小功能。使用范围包括轮播图等,例如微信聊天列表用的是UITableview继承于UIScrollview,在固定的手机屏幕区域内滑动查看更多内容。下面就介绍一下UIScrollView具体的使用方法:

创建UIScrollView的方法:

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

UIScrollView自定义设置部分属性(重要):

    //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;

UIScrollView自定义实现的部分代理方法(重要):

首先要遵守协议,此协议内的方法都是@optional类型。


// 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{
    
}

你可能感兴趣的:(iOS,UI基础小知识,ios,iphone)