ScrollerView的底层实现

2017-01-12  本文已影响20人  CoderLNHui

基本原理:

代码实现


#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>

@property (weak, nonatomic)  UIView *scrollView;

@property (nonatomic, assign) CGPoint offsetX;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    UIView *scrollView = [[UIView alloc] initWithFrame:self.view.bounds];

    

    [self.view addSubview:scrollView];

    

    _scrollView = scrollView;

    

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];

    [scrollView addGestureRecognizer:pan];

    

    UISwitch *switchV = [[UISwitch alloc] init];

    

    [scrollView addSubview:switchV];

    

    

}

// 拖动的时候调用

- (void)pan:(UIPanGestureRecognizer *)pan

{

    // 获取偏移量

    CGPoint transP = [pan translationInView:pan.view];

    

    _offsetX.x += -transP.x;

    _offsetX.y += -transP.y;

    

    _scrollView.bounds = CGRectMake(_offsetX.x, _offsetX.y, self.view.bounds.size.width, self.view.bounds.size.height);

    

    [pan setTranslation:CGPointZero inView:pan.view];

    

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    NSLog(@"%f %f",scrollView.contentOffset.y,scrollView.bounds.origin.y);

}

上一篇 下一篇

猜你喜欢

热点阅读