将拖放组件添加到iOS应用程序

And*_*rew 11 objective-c ios

如果这看起来很模糊,我很抱歉,但我不确定如何说出来.

我正在尝试构建一个ipad应用程序,让用户使用他们需要的工具填充工作区.我需要一个让用户将组件拖放到app界面的示例.因此,例如,如果我有一个应用程序,用户可以制作一个表单,我希望用户能够拖动文本框,列表,单选按钮等来制作表单,我将如何去做呢?我正在使用的项目将在弹出菜单中,以便用户拖动,然后将在弹出窗口中看到我需要的所有项目.我只是不确定如何让他们从弹出窗口拖动到画布,也能够重复使用相同的项目(因此在示例中有3或4个文本框).这可能吗?任何人都可以指导我这方面的教程吗?我已经搜索但无法找到任何东西.

如果这是错误的或者是一个愚蠢的任务请告诉我怎么做,这是我第一次在这里发帖.

谢谢

Voj*_*bka 8

已经有很多答案解释了这个问题.基本上你可以制作自定义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) {}];

}
Run Code Online (Sandbox Code Playgroud)

类似的问题

iPhone App:在UIView中实现拖放图像

iOS中的基本拖放功能

iPhone拖放


归档时间:

查看次数:

20977 次

最近记录:

9 年,5 月 前