PDA

View Full Version : Swapping UI images by drag n drop




sachin.singh
Dec 2, 2008, 08:08 AM
hello everyone...:apple::apple:

As i am a newbie, i am developing an application which has a UIImageView containing 4 UI images....What i want is to just drag and drop an UI image to another UI image so that their position gets swapped and the UI image shld be displayed whre it is DRAGGED....

SOMEONE PLZ PLZ HELP ME SINCE I AM STRUGGLING :( AND STUCK ON THE SAME SINCE 5 Days....:(


I am using the touch began n touch moved function and swapping the positions in TOUCH MOVED function....


ANYONE plz help me with some ideas or ANY SAMPLE CODE...:(

ANY HELP WLD B APPRECIATED....

THANKING IN ADVANCE...
Sachin...:)



dejo
Dec 2, 2008, 10:26 AM
We can probably better help you in you post the code you are using and an explanation of what it is / is not doing.

sachin.singh
Dec 2, 2008, 10:53 PM
hey dejo thanks for ur reply...:)
will surely show u my code on which i am struggling...

The foll. is my UIView class from which m Adding a UIImageView...

@implementation dragNdrop

@synthesize image1,image2,image3,pointImage1,pointImage2,pointImage3;

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
CGRect rectngle=CGRectMake(0.0, 0.0,self.bounds.size.width,self.bounds.size.height);

image1=[UIImage imageWithContentsOfFile:@"/Users/macpc4/Desktop/flowers_1.jpg"];
image2=[UIImage imageWithContentsOfFile:@"Users/macpc4/Desktop/wild_daffodil_flowers_durham_england.jpg"];
image3=[UIImage imageWithContentsOfFile:@"/Users/macpc4/Desktop/dictionary-plants-flowers.jpg"];
UIImage *img[3] = {image1,image2,image3};

[self setBackgroundColor:[UIColor grayColor]];
pointImage1=CGRectMake(30.0, 30.0, self.bounds.size.width/4, self.bounds.size.height/4);
pointImage2=CGRectMake(120.0, 30.0, self.bounds.size.width/4, self.bounds.size.height/4);
pointImage3=CGRectMake(30.0, 180.0, self.bounds.size.width/4, self.bounds.size.height/4);

CGRect pos[3]={pointImage1,pointImage2,pointImage3};
int i = 0;
for(int i=0;i<3;i++)
{
imageView[i]=[[myImage alloc] initWithFrame:pos[i] withImage:img[i] withArray:i withSuperview:self];
viewImage[i]=[[myImage alloc] setView:imageView[i] withInt:i];

[self addSubview:imageView[i]];

}

}
return self;
}
:confused:


Now this is my UIImageView class in which im handling the TOUCH events..

CGRect array[3];
myImage *finalView[3],*drawView[3];
int i,j;
int imgPos;

@implementation myImage
@synthesize thisController,loc1,loc2,image1,image2,image3,inImage,thisRect;


- (id)initWithFrame:(CGRect)frame withImage:(UIImage*)img withArray:(int)arr withSuperview:(dragNdrop *)inView
{
if (self = [super initWithFrame:frame])
{
// Initialization code
mySuperview = inView;
array[arr]=frame;
self.image=inImage;
[self isMultipleTouchEnabled];
self.userInteractionEnabled=YES;
}
return self;
}

-(id)setView:(myImage *)inView withInt:(int)arr
{
finalView[arr]=inView;
return self;

}
:confused:
- (void) touchesBegan: (NSSet*)touches withEvent: (UIEvent*)event
{
UITouch *touch = [touches anyObject];
NSArray *touchCount=[touches allObjects];
if ([touch view] != self) {
// In case of a double tap outside the placard view, update the placard's display string
if ([touch tapCount] == 2) {

}
return;
}
loc1 = [touch locationInView:self];
NSLog(@"start--%f %f",loc1.x,loc1.y);
for(i=0;i<3;i++)
{
if(self==finalView[i])
{
imgPos=i;
}
}

[self animateFirstTouchAtPoint:loc1];
}

:confused:
- (void) touchesMoved: (NSSet*)touches withEvent: (UIEvent*)event
{

UITouch *touch = [touches anyObject];
NSArray *touchCount=[touches allObjects];
NSLog(@"%d",[touchCount count]);

// If the touch was in the placardView, move the placardView to its location
if ([touch view] == self) {
loc2 = [touch locationInView:self];
NSLog(@"moved-- %f %f",loc2.x,loc2.y);
//thisController=[[dragNdrop alloc]init];
NSPoint pnt1=NSPointFromCGPoint(loc1);
NSPoint pnt2=NSPointFromCGPoint(loc2);

NSRect rct=NSRectFromCGRect(array[0]);
NSRect rct1=NSRectFromCGRect(array[1]);
NSRect rct2=NSRectFromCGRect(array[2]);

NSPoint pntArr[3]={pnt1,pnt2};
NSRect rctArr[3]={rct,rct1,rct2};
myImage *temp;
:confused:
for(i=0;i<3;i++,i!=imgPos)
{
if(NSPointInRect(pnt2, rctArr[i]))
{
j=i;
temp=finalView[j]; finalView[j]=finalView[imgPos];
finalView[imgPos]=temp;
[self drawRect:array[j] withIndex:j];
}
}
return;
}
}

- (void)drawRect:(CGRect)rect withIndex:(int)inIndex
{
drawView[inIndex]=finalView[j];
finalView[j].frame=array[inIndex];
[self.superview addSubview:drawView[inIndex]];
}

I Hope this will give u an Overall overview...i knw i hav displayed my code in a vry Shabby way...bt kindly bear me for this...

Plz HELP ME OUT...
Thanking YOU
sachin..:apple:

dejo
Dec 3, 2008, 12:46 AM
So, what problems are you experiencing with this code?

Also, please repost (or edit the post) and use the CODE tags.

sachin.singh
Dec 3, 2008, 04:42 AM
Thanks 4 ur soon reply dejo...:)
Actually the problem i am facing is dat, I want to swap the position of three UIImageview to the position whre i am dragging a particular UIImageView...
Hope now u got d problem...SO juz wanted to confirm tht wat wrong i am doing in applying FOR loop or any other condition in TOUCHES MOVED function.......

- (void) touchesBegan: (NSSet*)touches withEvent: (UIEvent*)event
{
UITouch *touch = [touches anyObject];
NSArray *touchCount=[touches allObjects];
if ([touch view] != self) {
// In case of a double tap outside the placard view, update the placard's display string
if ([touch tapCount] == 2) {

}
return;
}
loc1 = [touch locationInView:self];
NSLog(@"start--%f %f",loc1.x,loc1.y);
for(i=0;i<3;i++)
{
if(self==finalView[i])
{
imgPos=i;
}
}

[self animateFirstTouchAtPoint:loc1];
}


- (void) touchesMoved: (NSSet*)touches withEvent: (UIEvent*)event
{

UITouch *touch = [touches anyObject];
NSArray *touchCount=[touches allObjects];
NSLog(@"%d",[touchCount count]);

// If the touch was in the placardView, move the placardView to its location
if ([touch view] == self) {
loc2 = [touch locationInView:self];
NSLog(@"moved-- %f %f",loc2.x,loc2.y);
//thisController=[[dragNdrop alloc]init];
NSPoint pnt1=NSPointFromCGPoint(loc1);
NSPoint pnt2=NSPointFromCGPoint(loc2);

NSRect rct=NSRectFromCGRect(array[0]);
NSRect rct1=NSRectFromCGRect(array[1]);
NSRect rct2=NSRectFromCGRect(array[2]);

NSPoint pntArr[3]={pnt1,pnt2};
NSRect rctArr[3]={rct,rct1,rct2};
myImage *temp;

for(i=0;i<3;i++,i!=imgPos)
{
if(NSPointInRect(pnt2, rctArr[i]))
{
j=i;
temp=finalView[j]; finalView[j]=finalView[imgPos];
finalView[imgPos]=temp;
[self drawRect:array[j] withIndex:j];
}
}
return;
}
}
i want to make it work the same way like how in iphone simulator the ICONS of applications whn tapped for 2 seconds can be swapped...
THANK YOU..

rahulvyas
Dec 19, 2009, 06:13 AM
mail me your complete source code so that we can understand your code and remove the bug.here is my mail address rahul.vyas@isol.co.in