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

时间:2016-06-03 09:26:05

标签: ios objective-c uiscrollview uipangesturerecognizer cliptobounds

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

The 3rd property is: 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;
}

关于如何改善这一点的任何想法? 提前谢谢。

2 个答案:

答案 0 :(得分:1)

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

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

以下是代码:

- (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;
    }
}

答案 1 :(得分:1)

我建议你看一下ray wenderlich Moving Table View Cells with a Long Press Gesture

这篇文章

它解释了如何创建快照