把一个UIImageview拖出UIScrollview



我在一个uiscrollview中有很多uiimageview。我想把uiimageview拖出uisrollview。我已经子类化了uiscollview,并编写了自定义touchbegan, touchmoved和touchended方法,将触摸传递给滚动拖动事件上的滚动视图超类和滚动事件以外的事件上的主视图控制器类touchmethods。为了将uiimageview拖出uiscrollview,我将它从它的父视图(scrollview)中移除,并在touchbegan方法上将它添加到主视图。我的uiimageview从滚动视图中移除,并成功添加到主视图中。这里的问题是,在第一次触摸时,我的touchbegan功能被调用但是当我移动uiimageview时,touchmoved功能没有被调用。在第二次触摸之后uiimageview中的一切都被拖出scrollview

更新:我子类化了UIImageView类,并在UIImageView子类中编写了touchbegan,touchmoved和touchended方法,现在它正在工作。以前我用的是主视图的触摸方法,所以触摸方法也会考虑在uscollview上触摸,但是现在只考虑在uiimageview上触摸。我还增加了一个变化:我在触点开始禁用滚动,在触点结束启用滚动。谢谢

对我来说最有效的是不要移动在滚动视图中的uiImageView,而是创建一个重复的视图(在控制视图控制器中)并使用触摸移动它,然后将其添加到主视图,然后随后删除原始。

如果用户中途改变主意,决定不把它拖出来,这种方法看起来也会更好。

编辑:我检查了我的应用程序来刷新我所做的我也没有得到touchesMoved或ended在原始项目上。我确实在slideview的每个视图上都有一个手势识别器,当这个被触发时它告诉委托(是scrollview的父视图控制器)拖放正在进行中。从传递的对象,我可以得到CGPoint和相应的操作:

UILongPressGestureRecognizer *myRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(drag:)];
[myRecognizer setDelegate:self];
[myItemInsideScrollView addGestureRecognizer:myRecognizer];

-(void)drag:(UILongPressGestureRecognizer *)sender {
    [self.view bringSubviewToFront:[(UILongPressGestureRecognizer*)sender view]];
    CGPoint translatedPoint = [sender locationInView:self.view];
    // if we are not already dragging we create the duplicate view
    // if we are already dragging just update the location of the duplicate view
    // next check if drag has ended and if so call a method to do what we want
    if ( dragging ) {
        if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
            [self checkForDragRelease:(CGPoint)translatedPoint];
        }
    }
}

最新更新