【问题标题】:Trying to limit movement of a UIImage when using UIPanGestureRecognizer in iOS在 iOS 中使用 UIPanGestureRecognizer 时尝试限制 UIImage 的移动
【发布时间】:2013-09-23 20:39:33
【问题描述】:

我正在开发一个允许用户移动标尺以滚动 UITableView 的应用程序。目前,标尺移动得很好,除了我想将它的移动限制为仅 UITableView 的行,没有别的。不幸的是,目前用户能够从顶部滚动整个表格,并超出表格底部到屏幕底部。我附上了一张图片来展示这一点:

这是我拥有的相关代码:

    - (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】:

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

    float calculatedY = self.table.frame.origin.y + _imageView.frame.size.height / 2;
    
    calculatedY = MAX(calculatedY, MINIMUM_VALUE_HERE);
    calculatedY = MIN(calculatedY, MAXIMUM_VALUE_HERE);
    
    newCenter.y = calculatedY;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多