ios view控件手势拖动

@property (weak, nonatomic) IBOutlet UIView *dragView;//可以是UIView的子类
@property (nonatomic, strong) UIPanGestureRecognizer *panGestureRecognizer;//添加在视图上的拖动手势
@property (weak, nonatomic) IBOutlet UIButton *btnUnfold;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.dragView addGestureRecognizer:self.panGestureRecognizer];
    _dragView.layer.cornerRadius = 15;
    _dragView.layer.masksToBounds = YES;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_btnUnfold.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(8.0, 8.0)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = _btnUnfold.bounds;
    maskLayer.path = maskPath.CGPath;
    _btnUnfold.layer.mask = maskLayer;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIPanGestureRecognizer *)panGestureRecognizer {
    if (!_panGestureRecognizer) {
        _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragViewMoved:)];
    }
    return _panGestureRecognizer;
}

- (void)dragViewMoved:(UIPanGestureRecognizer *)panGestureRecognizer {
    
    if (panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [panGestureRecognizer translationInView:self.dragView];
        CGFloat heightDiff = self.tabBarController.tabBar.frame.origin.y - self.dragView.frame.origin.y;
        NSLog(@"heightDiff:%f",heightDiff);
        NSLog(@"dragViewHight:%f",self.dragView.frame.origin.y);
        NSLog(@"tabBarItemHight:%f",self.tabBarController.tabBar.frame.origin.y); // tabBar 高度

        if (self.dragView.frame.origin.y + translation.y + 30 <= self.tabBarController.tabBar.frame.origin.y && self.dragView.frame.origin.y + translation.y + 330 >= self.tabBarController.tabBar.frame.origin.y) {
            self.dragView.center = CGPointMake(self.dragView.center.x, self.dragView.center.y + translation.y);
            NSLog(@"location X:%f, Y:%f",self.dragView.center.x + translation.x, self.dragView.center.y + translation.y);
        }

        //关键,不设为零会不断递增,视图会突然不见
        [panGestureRecognizer setTranslation:CGPointZero inView:self.view];
    }
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值