drag images in & out of UIScrollview

Discussion in 'iOS Programming' started by BarryK88, Mar 10, 2011.

  1. BarryK88 macrumors member

    Joined:
    Mar 1, 2011
    #1
    In my project I'm having a scrollview with a view images inside. I'd like to drag them from my scroller to my View and counterwise.

    Is there someone who has some experience with this type of function.

    Help is greatly appreciated!
     
  2. BarryK88 thread starter macrumors member

    Joined:
    Mar 1, 2011
    #2
    TapRecognizer in View and Scrollview (to drag and drop images in and out of Scrollvie

    I created a view and a scrollview. They both have images inside their view. I'd like to implement a method which makes it possible for the user to drag images in and out of the scroller.

    I implemented a UITaprecognizer within my project however I'm not sure how to implement the next steps in my code.

    like:

    * how to see which image is doubletapped.
    * how to make sure that only the doubletapped image is draggable.
    * how to drag my image inside or outside a scrollview. (by means of a CGIntersectRect) wich replaces my image in the scroller to the image within my view.

    Here's a snippit from my code

    Code:
    - (void)viewDidLoad{
        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
        tap.numberOfTapsRequired = 2;
        [image1 addGestureRecognizer:tap];
        [image2 addGestureRecognizer:tap];
        [image3 addGestureRecognizer:tap];
    [scroller addGestureRecognizer:tap];
        [self.view addGestureRecognizer:tap];
        [tap release];
    
    [super viewDidLoad];
    }
    
    -(IBAction)handleDoubleTap:(UITapGestureRecognizer *)recognizer {
    
        NSLog(@"an image is doubletapped");
    }
    - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *) event {
    
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:self.view];
    
    if ([touch view] == image1) {
            image1.center = location;
        }
    if ([touch view] == image2) {
            image2.center = location;
        }
    if ([touch view] == image3) {
            image3.center = location;
        }
    [self checkcollision];
    }
    
    -(void)checkcollision {
    
    //check collision here
    }
    Help is greatly appreciated!
     
  3. BarryK88 thread starter macrumors member

    Joined:
    Mar 1, 2011
    #3
    Gesture recognizer problem (within a UIScrollview)

    I've got a scroller within my view and got several images inside this scroller.

    I'd like to drag and drop these images out (and into) my scroller. I implemented a LongPressGestureRecognizer.

    I also would like to know (by code) which image got longpressed. I thought I fixed this last one by using CGRectContainsPoint. However my code is not reacting to the CGRectContainsPoint. It still Logs "fail".

    Code:
    - (void)viewDidLoad{
    
        //scroller properties
        scroller.contentSize = CGSizeMake(1300, 130);
        scroller.scrollEnabled = YES;
        scroller.directionalLockEnabled =YES;
        scroller.frame = CGRectMake(0, 874, 768, 130);
    
        UILongPressGestureRecognizer *longPress =
        [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
    
        [image1 addGestureRecognizer:longPress];
        [image2 addGestureRecognizer:longPress];
        [image3 addGestureRecognizer:longPress];
        [longPress release];
    
    }
    
    -(void)longPressed:(UILongPressGestureRecognizer *)sender {
    
    
        CGPoint location = [sender locationInView:self.view];
    
        if(CGRectContainsPoint(image1.frame,  location)) {
            NSLog(@"image1 has been longpressed");
            // do something
        }
    
        if(CGRectContainsPoint(image2.frame,  location)) {
            NSLog(@"image2 has been longpressed");
            // do something
        }
    
        if(CGRectContainsPoint(image3.frame,  location)) {
            NSLog(@"image3 has been longpressed");
            // do something
    
    
        }
    
             else {
                     NSLog(@"fail");
    }
    Has someone got experience with this LongPressGestureRecognizer and the number of images that can respond to this function?

    Thanks in advance!
     
  4. BarryK88 thread starter macrumors member

    Joined:
    Mar 1, 2011
    #4
    Use a gesture inside a scrollview (bring content out of the scrollview with gestures)

    Inside my UIScrollView I've got a view. Within this view there are several images. I'd like to drag the images out of my scroller (and view) to the mainview. However getting the images in front of the scroller and view is driving me bonkers.

    This is a piece of my code:
    Code:
    - (void)viewDidLoad{
    
        //scroller properties
        scroller.contentSize = CGSizeMake(1300, 130);
        scroller.scrollEnabled = YES;
        scroller.directionalLockEnabled =YES;
        scroller.frame = CGRectMake(0, 874, 768, 130);
        [scroller setDelegate:self];
    
        UIView *contentContainer = [[UIView alloc] init];
    
        [scroller addSubview:contentContainer];
        [scroller addSubview:image1];
        [scroller bringSubviewToFront:image1];
        [speler1 release];
    
        UILongPressGestureRecognizer *longPress1 =
        [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
        [image1 addGestureRecognizer:longPress1];
        [longPress1 release];
    
        UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]  initWithTarget:self action:@selector(move:)];
        [panRecognizer setMinimumNumberOfTouches:1];
        [panRecognizer setMaximumNumberOfTouches:1];
        [panRecognizer setDelegate:self];
        [image1 addGestureRecognizer:panRecognizer];
    
    
    }
    
    -(void)longPressed:(UILongPressGestureRecognizer *)sender {
    
        [[self.view superview] bringSubviewToFront:image1];
    
    }
    
    -(void)move:(id)sender {
    
        //move the image
    
    }
    I'd like to bring the images in front of the views by using a LongPressGestureRecognizer. The PanGestureRecognizer takes care of moving the image. The dragging works only inside the scrollview. Anyone knows how to bring my images in front of the views?

    Help is greatly appreciated
     

Share This Page