学习笔记:UIPanGestureRecognizer

2016-11-02  本文已影响0人  _sun1993

UIPanGestureRecognizer实现左右滑动
1.添加手势和观察者
// 添加pan手势
[self.view addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]];

// 添加观察者监听oneView的frame的改变
[self.oneView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionOld context:nil];

2.监听手势和观察者
// 监听观察者

// 判断拖动结束的时候
if (pan.state == UIGestureRecognizerStateEnded) {
    CGFloat target = 0;
    if (self.oneView.frame.origin.x > [UIScreen mainScreen].bounds.size.width * 0.5) {
        target = self.view.bounds.size.width * 0.2;
    }
    // 获取x偏移量
    CGFloat constant = self.view.bounds.size.width - target;
    // 便宜
    self.oneView.frame = target == 0 ? self.view.bounds : CGRectMake(constant, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    
}

}

上一篇 下一篇

猜你喜欢

热点阅读