UIScrollView

自动布局 masonry

可以多加一个 contentView,也可以不要。

    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
        make.width.mas_equalTo(self.view);
    }];
    
    UIView *tempView;
    for (NSInteger index = 0; index < 100; index ++) {
        UIView *view = [UIView new];
        view.backgroundColor = [UIColor randomColor];
        [self.scrollView addSubview:view];
        
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(tempView ? tempView.mas_bottom : self.scrollView);
            make.left.and.right.mas_equalTo(self.scrollView);
            make.width.mas_equalTo(self.scrollView);
            make.height.mas_equalTo(arc4random() % 66);
        }];
        tempView = view;
    }
    
    [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.scrollView);
    }];

storyboard

注:最好在scrollView 上手动添加一个contenView 方便拉约束。

常用属性

        _scrollView.contentInset = UIEdgeInsetsMake(10, 20, 30, 40);// 内容调整
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = YES;// 触边弹回


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {    
// scrollView.contentOffset
}
  • 其他属性
        _scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;// 键盘管理
        _scrollView.directionalLockEnabled = YES;// 滚动方向锁定  
        _scrollView.decelerationRate = 50;// 手指滑动时,滚动速度
        _scrollView.pagingEnabled = YES;
        _scrollView.scrollEnabled = YES;
        _scrollView.scrollsToTop = YES;// 点击状态栏 滚动到顶部
        
        _scrollView.bounces = YES;// 触边弹回
        _scrollView.alwaysBounceVertical = YES;// 内容 小于时,已然可以弹回
        _scrollView.alwaysBounceHorizontal = YES;
        
        [_scrollView flashScrollIndicators];// 显示指示条,一小会
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(100, 0, 100, 20);
        _scrollView.indicatorStyle = 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
         };
         */

        _scrollView.minimumZoomScale = 0.2;
        _scrollView.maximumZoomScale = 2.;
        
        [_scrollView.panGestureRecognizer addTarget:self action:@selector(shishi2:)];
        [_scrollView.pinchGestureRecognizer addTarget:self action:@selector(shishi3:)];


代理

#pragma mark - delegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    
};

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
};

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    
};

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    
}

//- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
//    
//};

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view {
    
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale {
    
}

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {

    return YES;
}

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
    
}

你可能感兴趣的:(UIScrollView)