02-UIScrollView底层实现

 
UIScrollView底层实现:修改bounds,就等于UIScrollView的contentOffset偏移量

 
#import "ViewController.h"
 
@interface ViewController ()<UIScrollViewDelegate>
 
@property (nonatomic, weak) UIView *scrollView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    UIView *scorllView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:scorllView];
    _scrollView = scorllView;
   
    // 添加pan手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [scorllView addGestureRecognizer:pan];
   
    UISwitch *switchView = [[UISwitch alloc] init];
    [scorllView addSubview:switchView];
   
    // 1.UIView 添加一个pan手势
    // 2.手指往上移动,内容往上走,想看下面的内容,bounds.y++
   
}

- (void)pan:(UIPanGestureRecognizer *)pan
{
    // 获取手指偏移量
    CGPoint transP = [pan translationInView:pan.view];
    CGFloat offsetY = -transP.y;
   
    // 修改bounds
    CGRect bounds = _scrollView.bounds;
    bounds.origin.y += offsetY;
    _scrollView.bounds = bounds;
   
    // 复位
    [pan setTranslation:CGPointZero inView:pan.view];
   
   
//    NSLog(@"%f",offsetY);
}

// 只要一滚动就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"%@ %@",NSStringFromCGPoint(scrollView.contentOffset),NSStringFromCGRect(scrollView.bounds));
}
//
 
 
 
@end
posted @ 2016-01-15 01:36  子轩~  阅读(351)  评论(0编辑  收藏  举报