如何在iPhone应用程序中拖动对象?

时间:2021-01-03 22:46:01

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?

如何在iPhone应用程序中拖动对象?它只是一个可以拖动的按钮吗?

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

如何检测可拖动物体的位置?会不会像(Psuedo)那样简单

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

?

2 个解决方案

#1


4  

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.

NSSet对象包含UITouch对象,其中包含视图上的触摸的所有信息,视图,屏幕上的位置,先前的位置等等。您可以检查文档。

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.

从touchesBegan开始你的逻辑,并在touchesMoved事件中更改对象的位置。然后当用户删除对象时,将调用touchesEnded事件。检查touchesEnded事件中的位置并按照您的说法执行代码。

May this three methods will help...

愿这三种方法有所帮助......

#2


2  

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

如果所述对象是UIView,则可以获得其框架原点和大小。

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

检查Apples MoveMe示例:http://developer.apple.com/iphone/library/samplecode/MoveMe/Introduction/Intro.html

#1


4  

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.

NSSet对象包含UITouch对象,其中包含视图上的触摸的所有信息,视图,屏幕上的位置,先前的位置等等。您可以检查文档。

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.

从touchesBegan开始你的逻辑,并在touchesMoved事件中更改对象的位置。然后当用户删除对象时,将调用touchesEnded事件。检查touchesEnded事件中的位置并按照您的说法执行代码。

May this three methods will help...

愿这三种方法有所帮助......

#2


2  

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

如果所述对象是UIView,则可以获得其框架原点和大小。

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

检查Apples MoveMe示例:http://developer.apple.com/iphone/library/samplecode/MoveMe/Introduction/Intro.html