如何在iOS中使用拖动物品

3
我不太确定如何开始解释,但我会尝试。
我一段时间前开始读iOS的书籍,并且正在努力掌握Objective-C。我认为我对它有一个不错的理解。
在iOS上,我正在制作一个简单的游戏,它涉及将数字拖到框中,然后应用程序会获取被拖动到框中的数字。然而,我不知道如何做到这一点。
我在想你们是否有关于这方面的链接/文章,如果我应该使用某种框架进行此操作,或者甚至是一些示例代码。
感谢任何回答。

你使用的图形框架是什么? - PTBG
@PTGB 我没有使用任何“特殊”的框架。我正在使用与Xcode配置的标准框架。游戏不会是图形密集型的,因为拖动是最复杂的操作之一。 - Austin
4个回答

2
这是一个范围广泛的问题,但简单的方法是为每个可拖动数字创建一个自定义的UIView子类来处理绘制。在视图控制器中,您可以创建所有这些自定义UIView,并为每个创建一个UILongPressGestureRecognizer,将allowableMovement属性设置为CGFLOAT_MAX或某个较大的数值。使用视图控制器中的回调方法将识别器附加到每个可拖动的视图上。
然后,在视图控制器手势识别器操作方法中,您只需将视图中心属性更新为手势识别器在视图控制器视图中的位置即可。例如:
- (void)handleLongPressGesture:(UIGestureRecognizer *)recognizer
{
    if ([recognizer state] == UIGestureRecognizerStateChanged)
    {
         [[recognizer view] setCenter:[recognizer locationInView:self.view]];
    }
}

2

在“cocos2d拖放”上进行搜索...此外,这里有一篇文章可能会帮助您入门。


+1 我只想分享教程链接,但你比我更快了 :) - Nick Weaver
@NickWeaver 我其实也刚开始学习cocos2d,所以我正在寻找好的教程 :D - PTBG
@PTBG 谢谢,我现在正在做第一个教程 :) 编辑:所有的coco2d都有启动画面吗? - Austin
@PTBG 建议你看一下我在回答中提供的书籍链接,我已经阅读过了,它可以很全面地介绍cocos2d。 - Nick Weaver

1

Cocos2D很好,我喜欢它,但你并不需要到那个级别才能处理一些基本的UITouch。如果你只想处理一些触摸事件,这里有一个例子:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    // Only move the placard view if the touch was in the placard view
    if ([touch view] != placardView) {
        // On double tap outside placard view, update placard's display string
        if ([touch tapCount] == 2) {
            [placardView setupNextDisplayString];
        }
        return;
    }
    // "Pulse" the placard view by scaling up then down
    // Use UIView's built-in animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    CGAffineTransform transform = CGAffineTransformMakeScale(1.2, 1.2);
    placardView.transform = transform;
    [UIView commitAnimations];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    transform = CGAffineTransformMakeScale(1.1, 1.1);
    placardView.transform = transform;
    [UIView commitAnimations];

    // Move the placardView to under the touch
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25];
    placardView.center = [self convertPoint:[touch locationInView:self] fromView:placardView];
    [UIView commitAnimations];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    // If the touch was in the placardView, move the placardView to its location
    if ([touch view] == placardView) {
        CGPoint location = [touch locationInView:self];
        location = [self convertPoint:location fromView:placardView];
        placardView.center = location;
        return;
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    // If the touch was in the placardView, bounce it back to the center
    if ([touch view] == placardView) {

        CGPoint location = [touch locationInView:self];
        location = [self convertPoint:location fromView:placardView];

        UIView* dropZone; // assume this exists
        CGRect dz = [dropZone frame];
        if (CGRectContainsPoint(dz,location)) {
          [self doDropMagic];
        }
    }
}

更多信息,请查看事件处理指南,这是我从中获取这个混乱代码的地方。


1

我建议你使用Cocos2d。它是一个很好的基于OpenGL的框架,速度相当快。在Cocos2d中创建对象(称为CCSprite)并将它们放置在视图(CCLayer)上并与之交互非常容易。

拖放实际上包含三个阶段(伪代码),使用触摸处理方法(类似于UIKit的触摸方法):

  1. 在ccTouchesBegan中选择一个数字
  2. 在ccTouchesMoves中移动数字:使用触摸位置更新数字位置
  3. 使用ccTouchesEnded放下数字

  • 代码库中的TouchesTest目标展示了一个很好的拖放示例。它实现了一个简单的类乒乓球游戏:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接