美文网首页
利用bounds简单实现scrollView的滑动

利用bounds简单实现scrollView的滑动

作者: yyc223 | 来源:发表于2016-03-30 14:16 被阅读286次
    对bounds的理解

    先贴代码:(只是简单的实现上下滑动)

    #import "ViewController.h"
    
    @interface ViewController ()
    @property (weak,nonatomic) UIView *scrollView;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        UIView *scrollView = [[UIView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:scrollView];
        //添加滑动手势
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        self.scrollView = scrollView;
        [scrollView addGestureRecognizer:pan];
        //添加子控件,便于观察
        UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
        redView.backgroundColor = [UIColor redColor];
        [scrollView addSubview:redView];
    }
    - (void)pan:(UIPanGestureRecognizer *)pan
    {
        //获取偏移的点
        CGPoint transP = [pan translationInView:pan.view];
        //偏移量等于下一个点减去上一个点
        CGFloat y = self.scrollView.bounds.origin.y - transP.y;
        //子视图位置的变化相当于其bounds坐标系的变化
        self.scrollView.bounds = CGRectMake(0, y, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
        //复位,表示相对于上一次
        [pan setTranslation:CGPointZero inView:pan.view];
       
    }
    
    • frame: 以父控件的内容的左上角为坐标原点,看一下自己的矩形框 在哪
      bounds: 以自己的内容的左上角为坐标原点,和frame一样都是代表一块区域

    相关文章

      网友评论

          本文标题:利用bounds简单实现scrollView的滑动

          本文链接:https://www.haomeiwen.com/subject/riuslttx.html