I have UIImageView
s which are used as drag-able objects and they are in NSArray
so and they work fine when drag them but what i want is when i drag them and finish dragging method instead of drop the image in the UIImageView
i want replace it with custom image only in the dragged finish. so my problem is if i remove the NSArray
and make it only IF statement it work fine but only take one image and if i make it in NSArray
as the code below it is take only last image (Close2) and not appended it
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