0

i create a UIScrollView and i add UIPanGestureRecognizer and then in the scroll view i add some of UIImageView and then i add this DragAndDrop2 on the images the problem is when i drag the image it is work but only one image i create an NSArray as in code but still the same problem the UIPanGestureRecognizer *DragAndDrop2 is accepted on the last object on the array so how i can apply this UIPanGestureRecognizer *DragAndDrop2 on all the UIIMageView in the array so whatever i select from those UIImages will accepted the UIPanGestureRecognizer here is the code in the

UIImage+Stuff.h

#import <UIKit/UIKit.h>

@interface UIImage (Stuff)

//
// return an UIImage from a CALayer
//
+ ( UIImage* ) grabImage:(CALayer*)layer;

@end

UIImage+Stuff.m

#import "UIImage+Stuff.h"

@implementation UIImage (Stuff)

+ ( UIImage* ) grabImage:(CALayer*)layer
{
UIGraphicsBeginImageContext ( layer.frame.size );
[ layer renderInContext:UIGraphicsGetCurrentContext() ];

    UIImage *grab = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return grab;
}
@end

MainGameVC.h

@class APChartObject;

//
// drag status
//
typedef enum {
tDragStatusBegin = 0,
tDragStatusEnd,
tDragStatusIntersectIn,
tDragStatusIntersectOut
} tDragStatus;

@interface MainGameVC : UIViewController
//top menu scroll views
@property (nonatomic, retain) IBOutlet UIScrollView *TopMenuViewer;
@property (nonatomic, retain) IBOutlet UIScrollView *scrollview;

//drag and drop
@property (nonatomic, strong) IBOutlet UIView *DropView;
@property (nonatomic, strong) UIImageView *dragObject;
@property (nonatomic, strong) IBOutlet UIScrollView *cart;
@property (nonatomic, assign) tDragStatus dragging;
@property (nonatomic, strong) APChartObject *selectedModel;
@property (weak, nonatomic) IBOutlet UIImageView *DragedObjectWheat;
@property (weak, nonatomic) IBOutlet UIImageView *DragedObjectCorn;
@property (weak, nonatomic) IBOutlet UIImageView *DragedObjectOnion;
@end

MainGameVC.m

#import "UIImage+Stuff.h"
@interface MainGameVC ()
{
UIImageView *_selectedView;
CGPoint _startPoint;
}

@end

- (void)viewDidLoad
{
// drag and drop touch
UIPanGestureRecognizer *DragAndDrop = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
[self.TopMenuViewer addGestureRecognizer:DragAndDrop];

NSArray *imageViewArray = [NSArray arrayWithObjects:DragedObjectWheat,DragedObjectCorn,DragedObjectOnion, nil];

for(UIImageView *image in imageViewArray)
{
    UIPanGestureRecognizer *DragAndDrop2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:) ];
    [DragAndDrop2 setMinimumNumberOfTouches:1];

    [image addGestureRecognizer:DragAndDrop2];
    image.userInteractionEnabled = YES;

}

}

    //+---------------------------------------------------------------------------+
    #pragma mark - View exchange
    // +---------------------------------------------------------------------------+


    //
    // make the tricks.
    // Add a subview with the screenshot of selected and move around the screen
    //
    - ( void) cloneViewWithCenter:(CGPoint)point image:(UIImage*)grab
    {
        if ( _selectedView ) [ _selectedView removeFromSuperview ];

        _selectedView = [[ UIImageView alloc ] initWithImage:grab ];
        _selectedView.frame = CGRectMake(point.x, point.y, grab.size.width, grab.size.height);
        _selectedView.userInteractionEnabled = YES;

        [ self.view addSubview:_selectedView ];

        UIPanGestureRecognizer *pan = [[ UIPanGestureRecognizer alloc ] initWithTarget:self action:@selector(moveObject:) ];
        [ pan setMinimumNumberOfTouches:1 ];
        [ _selectedView addGestureRecognizer:pan ];
    }


    // +---------------------------------------------------------------------------+
    #pragma mark - Refresh
    // +---------------------------------------------------------------------------+


    //
    // refresh loop
    //
    - (void) refreshView
    {
        [UIView animateWithDuration:0.2 animations:^
         {
             CGRect r = _selectedView.frame;

             switch ( _dragging ) {
                 case tDragStatusBegin:
                     r.size.width  *= 1;
                     r.size.height *= 1;
                     break;
                 case tDragStatusEnd:
                     r.size.width  /= 1;
                     r.size.height /= 1;
                     break;
                 case tDragStatusIntersectIn:
                     r.size.width  = 1;
                     r.size.height = 1;

                     [ self finishDrag ];
                     break;
                 case tDragStatusIntersectOut:
                     _selectedView.center = _startPoint;
                     break;
             }

             _selectedView.frame = r;

         } completion:^(BOOL finished)
         {

             if ( _dragging == tDragStatusIntersectOut )
                 _selectedView.hidden = YES;

         }];
    }

    //
    // end drag
    //
    - (void) finishDrag
    {
        UIImageView *img = [[UIImageView alloc] initWithImage:_selectedView.image];
        [ self appendView:img ];
    }

    //
    // check for insertion in cart (or not)
    //
    - (void) checkForIntersection
    {
        //
        // ABS coords.
        //
        CGRect childRect = [ self.view convertRect:_selectedView.frame fromView:nil ];
        CGRect cartRect  = [ self.view convertRect:_cart.frame fromView:nil ];

        if ( CGRectIntersectsRect ( childRect, cartRect ))
        {
            self.dragging = tDragStatusIntersectIn;

        }
        else
        {
            self.dragging = tDragStatusIntersectOut;
        }
    }

    - (void) refreshCart
    {
        [ _cart setContentOffset:CGPointMake(_cart.contentOffset.x, 0) animated:YES ];
    }


    // +---------------------------------------------------------------------------+
    #pragma mark - Pan gesture
    // +---------------------------------------------------------------------------+

    - ( void ) panDetected:(UIPanGestureRecognizer*)gesture
    {

        CGPoint pInView = [ gesture locationInView:self.view ];
        //CGSize  pSize   = gesture.view.frame.size;

        if ( gesture.state == UIGestureRecognizerStateBegan )
        {
            _startPoint = pInView;

    //        UIPanGestureRecognizer *DragAndDrop2 = [[ UIPanGestureRecognizer alloc ] initWithTarget:self action:@selector(panDetected:) ];
    //        [ DragAndDrop2 setMinimumNumberOfTouches:1 ];
    //        [DragedObjectCorn addGestureRecognizer:DragAndDrop2];
    //        [DragedObjectOnion addGestureRecognizer:DragAndDrop2];
    //        [DragedObjectWheat addGestureRecognizer:DragAndDrop2];
    //        
    //        NSArray *imageViewArray = [NSArray arrayWithObjects:DragedObjectWheat,DragedObjectCorn,DragedObjectOnion, nil];
    //        for(UIImageView *image in imageViewArray)
    //        {
    //            [image addGestureRecognizer:DragAndDrop2];
    //            image.userInteractionEnabled = YES;
    //        }


    UIGestureRecognizer *DragAndDrop2;
    // grab image
    //
     UIImage *grab = [UIImage grabImage: DragAndDrop2.view.layer];
            //
            // centering view
            //
            //pInView.x = pInView.x - pSize.width/2;
            //pInView.y = pInView.y - pSize.height/2;

            [ self cloneViewWithCenter:pInView image:grab ];

            self.dragging = tDragStatusBegin;
        }
        else if ( gesture.state == UIGestureRecognizerStateChanged )
        {
            [ self moveObject:gesture ];
        }
        else if ( gesture.state == UIGestureRecognizerStateEnded )
        {
            self.dragging = tDragStatusEnd;
            [ self checkForIntersection ];
        }
    }

    //
    // move draggable view around
    //
    - (void) moveObject:(UIPanGestureRecognizer *)pan
    {
        _selectedView.center = [ pan locationInView:_selectedView.superview ];
    }


    // +---------------------------------------------------------------------------+
    #pragma mark - Setter
    // +---------------------------------------------------------------------------+


    - (void)setDragging:(tDragStatus)dragging
    {
        _dragging = dragging;
        [ self refreshView ];
    }


    // +---------------------------------------------------------------------------+
    #pragma mark - Chart view
    // +---------------------------------------------------------------------------+


    //
    // recursively append view to scrollview.
    // If position already contains a view, shift and retry.
    //
    - (void) appendView:(id)view
    {
        //    UIView *v = view;
        //
        //    CGRect vRect = [ self.view convertRect:v.frame fromView:nil ];
        //
        //    for ( UIView *innerView in _cart.subviews )
        //    {
        //        CGRect cartRect = [ self.view convertRect:innerView.frame fromView:nil ];
        //
        //        if ( CGRectIntersectsRect(vRect, cartRect) )
        //        {
        //            CGRect r = v.frame;
        //            r.origin.x += r.size.width;
        //            v.frame = r;
        //
        //           _cart.contentSize = CGSizeMake(r.origin.x + 80, 80);
        //
        //            return [ self appendView:v ];
        //        }
        //    }
        //    
        [ _cart addSubview:view ];

        [ self performSelector:@selector(refreshCart) withObject:nil afterDelay:0 ];
    }
    @end
0

1 Answer 1

0

Two things:

  1. You have to set your UIImageViews to userInteractionEnabled = YES since their user interaction property is disabled by default.

  2. Each UIImageView should have its own UIGestureRecognizer.

I suggest editing your code like so:

NSArray *imageViewArray = [NSArray arrayWithObjects:DragedObjectWheat,DragedObjectCorn,DragedObjectOnion, nil];

for(UIImageView *image in imageViewArray)
{
    UIPanGestureRecognizer *DragAndDrop2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:) ];
    [DragAndDrop2 setMinimumNumberOfTouches:1];

    [image addGestureRecognizer:DragAndDrop2];
    image.userInteractionEnabled = YES;

}

Also, I don't know what you're trying to do with this line, but this code's a bit suspicious:

UIImage *grab = [UIImage grabImage: DragAndDrop2.view.layer];

Edit:

Move that first block of code I wrote to your viewDidLoad then change these lines in panDetected:

UIGestureRecognizer *DragAndDrop2;
UIImage *grab = [UIImage grabImage: DragAndDrop2.view.layer];

to this:

UIImage *grab = [UIImage grabImage: gesture.view.layer];

like in the github code, so that you're grabbing the image from the current gesture.

29
  • UIImage *grab = [UIImage grabImage: DragAndDrop2.view.layer];is taking the image that accepted on it the DragAndDrop2 "GestureRecognizer" and add it in this grap uiimage so that it move around in the screen, the problem all working fine except the GestureRecognizer is applied only on the last object in the array,while what u want is when i select any other uiimages in the array should GestureRecognizer apply on it
    – Mahmoud
    Dec 21, 2014 at 4:30
  • in this case it is work very fine but as u can see in my code the last object in the array is DragedObjectOnion, so it is the only one work so how every u select other images in the scroll view it is run and show only the image of onion
    – Mahmoud
    Dec 21, 2014 at 4:37
  • my project is about drag and drop items from scroll view, so drag it from scroll view and drop it in the uiview or other scroll view, but really it give me headache specially am beginner
    – Mahmoud
    Dec 21, 2014 at 4:38
  • @Mahmoud Did you try editing your code like I suggested in the first part of my answer? Dec 21, 2014 at 4:39
  • yes and when i put the UIPanGestureRecognizer inside the for statment the UIImage *grap .... line give error coz can not see the DragAndDrop2
    – Mahmoud
    Dec 21, 2014 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.