如何在 GestureRecognizer 开始时以编程方式关闭 clipsToBounds 属性?

How to dismiss programmatically the clipsToBounds property when a GestureRecognizer begin?

我有一个 UIScrollView 可以包含很多视图。为了允许良好的滚动(滚动时内容不会超出视图),在我的 Main.sotryboard 上,我点击了我的 UIScrollView 然后在属性检查器中我允许 Clip Subviews 属性:

我的问题:UIScrollViews 中的所有视图都是可拖动的(因为它们都有一个 UIPanGestureRecognizer。 因此,当我尝试将它们拖到 UIScrollView 之外时,它们就消失了。 事实上,他们只是落后于所有其他观点

举个例子,我还有其他组件允许从先例中删除视图 UIScrollView。因此,当我开始从中拖放时,它会消失,然后重新出现在我放置视图的第二个组件中。

我尝试了什么: 我有一个特殊的 UIPanGestureRecognizer 用于拖放来自此 UIScrollView 的视图。所以,我实际上有这个(显然,它不起作用,否则我不会在这里):

//Here recognizer is the `UIPanGestureRecognizer`
//selectpostit is the name of the view I want to drag
if(recognizer.state == UIGestureRecognizerStateBegan){
    selectpostit.clipsToBounds = NO;
}

关于如何改进它的任何想法? 提前致谢。

您可以尝试在每次手势开始时将 scrollView.clipsToBounds 重置为 NO,但这会导致副作用,因为在拖动过程中滚动视图之外的其他内容会变得可见。

我建议在平移开始时拍摄可拖动视图的快照,将其放置在滚动视图的父级上,然后移动它。这种方法应该可以解决你的问题。

代码如下:

- (void)onPanGesture:(UIPanGestureRecognizer*)panRecognizer
{
    if(panRecognizer.state == UIGestureRecognizerStateBegan)
    {
        //when gesture recognizer starts, making snapshot of the draggableView and hiding it
        //will move shapshot that's placed on the parent of the scroll view
        //that helps to prevent cutting by the scroll view bounds
        self.draggableViewSnapshot = [self.draggableView snapshotViewAfterScreenUpdates: NO];
        self.draggableView.hidden = YES;
        [self.scrollView.superview addSubview: self.draggableViewSnapshot];
    }

    //your code that updates position of the draggable view

    //updating snapshot center, by converting coordinates from draggable view
    CGPoint snapshotCenter = [self.draggableView.superview convertPoint:self.draggableView.center toView: self.scrollView.superview];
    self.draggableViewSnapshot.center = snapshotCenter;

    if(panRecognizer.state == UIGestureRecognizerStateEnded ||
       panRecognizer.state == UIGestureRecognizerStateCancelled ||
       panRecognizer.state == UIGestureRecognizerStateFailed)
    {
        //when gesture is over, cleaning up the snapshot
        //and showing draggable view back
        [self.draggableViewSnapshot removeFromSuperview];
        self.draggableViewSnapshot = nil;
        self.draggableView.hidden = NO;
    }
}

我建议你看看 ray wenderlich 的这篇文章 Moving Table View Cells with a Long Press Gesture

它解释了如何创建快照