[尝试在iOS中使用UIPanGestureRecognizer时限制UIImage的移动

问题描述 投票:0回答:1

我正在开发一个允许用户移动标尺以滚动UITableView的应用程序。此刻,标尺移动得很好,只是我想将其移动限制为仅UITableView的行,而没有别的。不幸的是,此刻,用户能够从顶部滚动整个表格,并超出表格底部到屏幕底部。我已附上图片以显示此内容:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS92RzlhSC5wbmcifQ==” alt =“在此处输入图像描述”>

这里是我拥有的相关代码:

    - (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer {

    CGPoint newCenter  = _imageView.center;

        newCenter.y = [recognizer locationInView:[_imageView superview]].y;

        if (newCenter.y - _imageView.frame.size.height / 2 < self.table.frame.origin.y)
            newCenter.y = self.table.frame.origin.y + _imageView.frame.size.height / 2;

        else if (newCenter.y + _imageView.frame.size.height / 2 > self.table.frame.origin.y + self.table.frame.size.height)
            newCenter.y = self.table.frame.origin.y + self.table.frame.size.height - _imageView.frame.size.height / 2;

        _imageView.center = newCenter;


        if ([recognizer state] == UIGestureRecognizerStateChanged) {

            if (newCenter.y == self.table.frame.origin.y + _imageView.frame.size.height / 2) {

                CGPoint topPoint = [recognizer locationInView:_table];
                NSIndexPath *nextRow = [_table indexPathForRowAtPoint:topPoint];
                int currentRow = nextRow.row;

                if (currentRow != 0) {

                    nextRow = [NSIndexPath indexPathForRow:currentRow-- inSection:0];

                    [UIView animateWithDuration:.3 animations:^{
                        [_table selectRowAtIndexPath:nextRow animated:NO scrollPosition:UITableViewScrollPositionBottom];

                    }];

                }


            }
            //below is the specific section where I believe is the issue
            else if (newCenter.y == self.table.frame.origin.y + self.table.frame.size.height - _imageView.frame.size.height / 2) {

                CGPoint bottomPoint = [recognizer locationInView:_table];
                NSIndexPath *nextRow = [_table indexPathForRowAtPoint:bottomPoint];
                int currentRow = nextRow.row;

                if (currentRow != [self.tireCode count]-1) {

                    nextRow = [NSIndexPath indexPathForRow:currentRow++ inSection:0];

                    [UIView animateWithDuration:.3 animations:^{
                        [_table selectRowAtIndexPath:nextRow animated:NO scrollPosition:UITableViewScrollPositionBottom];

                    }];

                }

            }

        }

正如我说的,我希望标尺仅滚动UITableView的行,而没有其他内容。我做错了什么?

ios uitableview uiimage uipangesturerecognizer
1个回答
0
投票

您应在分配超出范围的值之前检查newCenter.y的值。为它定义一个最小值和最大值。

© www.soinside.com 2019 - 2024. All rights reserved.