0

新手 obj-c 问题。

我的任务是为 iPad 进行视觉表现测试。在视图中,我有三个 UIImages(带有测试问题答案的图像)将拖到区域以寻求答案。如果选择了正确的图像,那么它需要留在这个区域,如果没有 - 它会回到起始位置。如果用户停止在区域内拖动而不是回答它也需要回到开始位置。我试图实现这一点:http: //www.cocoacontrols.com/platforms/ios/controls/tkdragview但这对我来说太难了,因为我是一个超级新手。

我通过 PanRecognizer 实现了简单的图像拖动,所以每三个图像拖动

 -(IBAction)controlPan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}

如果我以正确的方式思考,我需要设置 CGPoint 的起点和终点坐标,然后自定义 Pan Gestures 方法?如果不正确,我该怎么做?

4

1 回答 1

2

您可以考虑这三种方法,也许这些对您来说更容易:

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:

详细的演示– touchesBegan:withEvent:是:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    UITouch *touched = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touched.view];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    UITouch *touched = [[event allTouches] anyObject];
    // Here I suppose self.imageView contains the image user is dragging.
    CGPoint location = [touch locationInView:touched.view];
    // Here the check condition is the image user dragging is absolutely in the answer area.
    if (CGRectContainsRect(self.answerArea.frame, self.imageView.frame)) {
        // set transition to another UIViewController
    } else {
        self.imageView.center = location;    // Here I suppose you just move the image with user's finger.
    }
}

这是此UIResponder 类参考的 Apple 文档

于 2012-11-15T11:55:08.243 回答