将拖放组件添加到iOS应用 [英] Adding drag and drop component to iOS app
问题描述
如果这看起来很模糊,我深表歉意,但是我不确定该怎么写.
I apologise if this seems very vague but I'm not sure how else to word it.
我正在尝试构建一个ipad应用程序,该应用程序将允许用户使用他们所需的工具填充工作区.我需要一个让用户将组件拖放到应用程序界面中的示例.因此,例如,如果我有一个用户可以制作表格的应用程序,并且希望用户能够拖动文本框,列表,单选按钮等来制作表格,那么我该怎么做呢?我正在使用的项目将在弹出菜单中,以便用户向上拖动,然后将在该弹出窗口中看到我需要的所有项目.我只是不确定如何让他们从弹出窗口拖到画布上,还可以重用同一项目(因此在示例中有3或4个文本框).这可能吗?谁能指导我有关此的教程?我已经搜索过但找不到任何东西.
I am trying to build an ipad app that will let users populate a workspace with tools they need. I need an example of letting a user drag and drop components into the app interface. So for example if i had an app that the user can make a form and I want the user to be able to drag text boxes, lists, radio buttons etc to make the form, how would i go about doing that? The items that i am using will be in a popup menu so that the user drags up and then will see all the items that i need inside that popup. Im just not sure how to let them drag from the popup to the canvas and also be able to reuse the same item (so have 3 or 4 text boxes in the example). Is this possible? Can anyone direct me to a tutorial on this? I have searched but cant find anything.
如果这被认为是错误的或者是一个愚蠢的任务,请让我知道该怎么做,这是我第一次在这里发布.
If this is laid out wrong or is a stupid quest please let me know how to do it, this is my first time posting on here.
谢谢
推荐答案
已经有很多答案解释了这个问题. 基本上,您可以创建自定义UIView,在触摸后它将跟随触摸运动. 像这样:
There are already many answers explaining this question. Basically you can make custom UIView, which after touching will be following the touch movement. Something like this:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
_originalPosition = self.view.center;
_touchOffset = CGPointMake(self.view.center.x-position.x,self.view.center.y-position.y);
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint position = [touch locationInView: self.view.superview];
[UIView animateWithDuration:.001
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^ {
self.view.center = CGPointMake(position.x+_touchOffset.x, position.y+_touchOffset.y);
}
completion:^(BOOL finished) {}];
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint positionInView = [touch locationInView:self.view];
CGPoint newPosition;
if (CGRectContainsPoint(_desiredView.frame, positionInView)) {
newPosition = positionInView;
// _desiredView is view where the user can drag the view
} else {
newPosition = _originalPosition;
// its outside the desired view so lets move the view back to start position
}
[UIView animateWithDuration:0.4
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^ {
self.view.center = newPosition
// to
}
completion:^(BOOL finished) {}];
}
类似的问题
这篇关于将拖放组件添加到iOS应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!