1
votes

I have many uiimageviews in a uiscrollview. I am trying to drag an uiimageview out of the uiscrollview. I have subclasssed uiscollview and have written custon touchbegan,toucmoved and touchended methods to pass the touch to scrollview superclass on scroll-drag event and to my main viewcontroller class touchmethods on events other than scroll -events. To drag uiimageview out of uiscrollview I am removing that from its superview(scrollview) and adding it to mainview on touchbegan method. my uiimageview is removed from scrollview and is added to mainview succesfully . Now the problem here is : on the first touch ,my touchbegan method is called but touchmoved method is not called when i move the uiimageview. on the second touch onwards everything works n the uiimageview is dragged out the scrollview

Update: I subclassed UIImageView class and wrote the touchbegan,touchmoved and touchended methods inside the uiimageview subclass and now it is working.Previously I was using touch methods of the mainview ,So touch-methods were also considering touch on the UIScollview .But now only touches on uiimageviews are considered. I also added one more change: I disabled scrolling at touchbegan and enabled it at touchend . Thanx

1

1 Answers

1
votes

What works best for me is to not move the uiImageView that is in the scrollview but rather create a duplicate view (in the controlling view controller) and move that with the touches, then add that to the main view and then subsequently remove the original.

This approach also looks better if the user changes their mind halfway through and decides to not drag it out after all.

EDIT: I checked my app to refresh myself what I do and I too don't get touchesMoved or ended on the original item. I do have a gesture recognizer on each of the views in the slideview and when this is fired it tells the delegate (the viewcontroller that is the parent of the scrollview) that the drag is in process. From the object passed I can get the CGPoint and act accordingly:

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

}