UIScrollView基本用法详解

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滚动区域的大小,否则不能滚动

UIScrollView基本用法详解_第1张图片

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

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

UIScrollView基本用法详解_第2张图片UIScrollView基本用法详解_第3张图片UIScrollView基本用法详解_第4张图片

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);
    
        
}

UIScrollView基本用法详解_第5张图片 UIScrollView基本用法详解_第6张图片 UIScrollView基本用法详解_第7张图片

5、UIScrollViewDelegate

@protocol UIScrollViewDelegate

@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







你可能感兴趣的:(iOS,iOS-UI)