views:

57

answers:

2

I would like to have objects in my view which can be dragged and dropped.

When they are dropped in a certain area they should disappear and some code should execute.

How are objects made draggable in an iphone app? Is it just a button which can be drag enabled?

How would one detect the position of the draggable object? Would it be as simple as (Psuedo)

if (draggableButton1.xPosition > e.g. && draggableButton1.xPosition < e,g 
       && draggableButton1.yPosition > e.g. && draggableButton1.yPosition < e,g) {
     do something
} 

?

+1  A: 

If said object is an UIView you can get its frame origin and size.

Check Apples MoveMe example: http://developer.apple.com/iphone/library/samplecode/MoveMe/Introduction/Intro.html

pabloruiz55
+1  A: 

When user touches the object that time following method is called.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

When moved with touched, following method will be called.

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

When user end with touch, the following method will be used

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

The NSSet object contain the UITouch object which contain all information for touches on view, which view, position on screen , previous position and so more. You can check documentation for that.

Start your logic from touchesBegan and change the position of your object in touchesMoved event. then when user drop object, touchesEnded event will be called. Check for position in touchesEnded event and excute your code as you have said.

May this three methods will help...

Hiren Gujarati