如果这看起来很模糊,我深表歉意,但我不知道还能怎么说。
我正在尝试构建一个 iPad 应用程序,让用户可以使用他们需要的工具填充工作区。我需要一个让用户将组件拖放到应用程序界面中的示例。例如,如果我有一个应用程序,用户可以制作表单,并且我希望用户能够拖动文本框、列表、单选按钮等来制作表单,我将如何去做呢?我正在使用的项目将位于弹出菜单中,以便用户向上拖动,然后将在该弹出菜单中看到我需要的所有项目。我只是不确定如何让它们从弹出窗口拖动到画布,并且能够重用相同的项目(因此示例中有 3 或 4 个文本框)。这可能吗?谁能指导我有关此的教程?我已经搜索过但找不到任何东西。
如果这是错误的或者是一个愚蠢的任务,请让我知道该怎么做,这是我第一次在这里发帖。
谢谢
已经有很多答案解释了这个问题。
基本上你可以制作自定义 UIView,触摸后它将跟随触摸移动。
像这样的东西:
-(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) {}];
}
类似问题
iPhone App:在UIView中拖放图像的实现
iOS 中的基本拖放
iPhone 拖/放
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)