UIScrollView基本用法详解

18 篇文章 0 订阅

1、UIScrollView可以提供在屏幕尺寸不够的情况下滑动浏览大区域画面

基本用法如下:

    UIScrollView * scrollView = [[UIScrollView alloc] init];
    scrollView.frame = CGRectMake(0, 100, width, height - 300);//UIScrollView大小
    [self.view addSubview:scrollView];
    scrollView.backgroundColor = [UIColor orangeColor];
    
    UIImage * image = [UIImage imageNamed:@"yuan.png"];
    UIImageView * imageView = [[UIImageView alloc] initWithImage:image];
    [scrollView addSubview:imageView];
    
    //自动调整宽高
    scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    scrollView.contentSize = imageView.bounds.size;  //重要,这个是设置UIScrollView滚动区域的大小,否则不能滚动

2、滚动条样式:

默认:灰色线包围黑色条

    scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;

UIScrollViewIndicatorStyleBlack : 黑色条

UIScrollViewIndicatorStyleWhite : 白色条

样式枚举:

typedef NS_ENUM(NSInteger, UIScrollViewIndicatorStyle) {
    UIScrollViewIndicatorStyleDefault,     // black with white border. good against any background
    UIScrollViewIndicatorStyleBlack,       // black only. smaller. good against a white background
    UIScrollViewIndicatorStyleWhite        // white only. smaller. good against a black background
};

3、放大缩小:

须遵守UIScrollViewDelegate协议

    scrollView.delegate = self;
    scrollView.minimumZoomScale = 0.5;
    scrollView.maximumZoomScale = 2.0;
还需要实现如下方法:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    for (id subView in scrollView.subviews) {
        if ([subView isKindOfClass:[UIImageView class]]) {
            return subView;
        }
    }
    return nil;
}

最大放大两倍,最小缩小一倍

4、以页为单位滚动:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    
    CGFloat width = [[UIScreen mainScreen] bounds].size.width;
    CGFloat height = [[UIScreen mainScreen] bounds].size.height;
    
    
    UIScrollView * scrollView = [[UIScrollView alloc] init];
    scrollView.frame = CGRectMake(0, 100, width, height - 300);//UIScrollView大小
    [self.view addSubview:scrollView];
    scrollView.backgroundColor = [UIColor orangeColor];
    

    UIView * view1 = [[UIView alloc] init];
    view1.frame = scrollView.bounds;
    view1.backgroundColor = [UIColor purpleColor];
    [scrollView addSubview:view1];
    
    UIView * view2 = [[UIView alloc] init];
    view2.frame = CGRectMake(width, 0, width, scrollView.frame.size.height);
    view2.backgroundColor = [UIColor redColor];
    [scrollView addSubview:view2];
    
    UIView * view3 = [[UIView alloc] init];
    view3.frame = CGRectMake(width * 2, 0, width, scrollView.frame.size.height);
    view3.backgroundColor = [UIColor greenColor];
    [scrollView addSubview:view3];
    
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO; //隐藏横向滚动条
    scrollView.showsVerticalScrollIndicator = NO; //隐藏竖向滚动条
    
    scrollView.contentSize = CGSizeMake(width * 3, scrollView.frame.size.height);
    
        
}

5、UIScrollViewDelegate

@protocol UIScrollViewDelegate<NSObject>

@optional

//滚动时调用,可以实时监测滚动变化
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;                                               // any offset changes

//实时监测缩放
- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2); // any zoom scale changes

//开始拖动的时候调用
// 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;

//开始减速的时候调用
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;   // called on finger up as we are moving

//结束减速的时候调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;      // called when scroll view grinds to a halt

//设置scrollview动画,动画结束后调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView; // called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating


//返回缩放的可以view,如上例UIImageView
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;     // return a view that will be scaled. if delegate returns nil, nothing happens

//开始缩放的时候调用
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view NS_AVAILABLE_IOS(3_2); // called before the scroll view begins zooming its content

//结束缩放的时候调用
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale; // scale between minimum and maximum. called after any 'bounce' animations


//是否可以拖动到顶部,yes可以否则不可以
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;   // return a yes if you want to scroll to the top. if not defined, assumes YES

//可以拖动到顶部时,拖动结束后调用
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;      // called when scrolling animation finished. may be called immediately if already at top

@end







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值