How to select UIImgeView from NSArray

I have UIImageView

that are being used as draggable objects and they are in NSArray

, so they work great when dragging, but I want when I drag them and end up with the drag method instead of dropping the image in UIImageView

I want to replace it with a custom image only at the dragged end. so my problem is that if I delete NSArray

and make it only an IF statement it works fine but only takes one image and if I do it in NSArray

like in the code below it only takes the last image (Close2) and this will not be added

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

    @property (nonatomic, strong) IBOutletCollection(UIImageView) NSArray *TopMenuImages;
    //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;

    // all tools
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectBox;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectSand;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectSoil;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectWater;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectWheat;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectCorn;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectGarlic;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectLettuse;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectOnion;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectSugercane;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectTomato;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectCucumber;
    @property (weak, nonatomic) IBOutlet UIImageView *DragedObjectBeans;
    @end

      

MainGameVC.m

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

@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 ImageTag:(int)ToolTag
    {
    if ( _selectedView ) [ _selectedView removeFromSuperview ];
    selectedViewTag = ToolTag;
    _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 *Tool;

    UIImage *ReplacedPhoto;
    UIImageView *imageView;
    switch (selectedViewTag)
    {

        case 1:

        {
            logic for image tag 1
                break;
        }
        case 2:

        {
            logic for image tag 2
                break;
        }

            Tool.userInteractionEnabled = YES;
            [self appendView:Tool];
    }

    //
    // 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;
            UIImage *grab = [UIImage grabImage: gesture.view.layer];
            ToolTag = gesture.view.tag ;
            for (int i = 0; i>ToolTag; i++)
            {
                i = i+1;
                ToolTag = i;
            }
            //
            // centering view
            //
            //pInView.x = pInView.x - pSize.width/2;
            //pInView.y = pInView.y - pSize.height/2;

            [ self cloneViewWithCenter:pInView image:grab ImageTag:ToolTag ];

            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
    {

        [ _cart addSubview:view ];

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

      

+3


source to share


1 answer


In your header file, declare a property to hold your image and then wire it up in your interface constructor.

@interface YourViewController : UIViewController

@property (nonatomic, strong) IBOutletCollection(UIImageView) NSArray *imageViews;
//...

@end

      

Now in the interface builder, you need to connect the entire image to this new property.



enter image description hereenter image description here

Now you need to work with the collection imageViews

. You can even manually tag an image, which can later be used to check which image is being dragged.

for (UIImageView *imageView in self.imageViews) {
  if (imageView.tag == 1){ 
  // .. Logic for image with tag 1
  }
  elseif (imageView.tag == 2){ 
  // .. Logic for image with tag 2
  }
}

      

+4


source







All Articles