1

我正在尝试开发一个分析应用程序来确定你是否“聪明”这涉及到给自己拍照并将点拖到你的脸上,鼻子、嘴巴和眼睛所在的位置。但是,我尝试过的代码不起作用:

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

    if ([touch view] == eye1) 
    {
        eye1.center = location;
    } 
    else if ([touch view] == eye2) 
    {
        eye2.center = location;
    } 
    else if ([touch view] == nose) 
    {
        nose.center = location;
    } 
    else if ([touch view] == chin)  
    {
       chin.center = location;
    }
    else if ([touch view] == lip1) 
    {
        lip1.center = location;
    }
    else if ([touch view] ==lip2) 
    {
        lip2.center = location;
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [self touchesBegan:touches withEvent:event];
}

发生了什么,因为当我只有一个图像时,它可以工作,但对我没有帮助。我该怎么做才能让它发挥作用?这些点从屏幕底部的“工具栏”开始,然后用户将它们拖到脸上。我有点希望完成的结果看起来像:

http://gyazo.com/0ea444a0edea972a86a46ebb99580b2e

4

1 回答 1

2

有两种基本方法:

  1. 您可以在控制器或主视图中使用各种触摸方法(例如touchesBegantouchesMoved等),或者您可以在主视图中使用单个手势识别器。在这种情况下,您将使用touchesBegan或者,如果使用手势识别器, a stateof UIGestureRecognizerStateBegan,确定locationInView超级视图,然后通过测试来测试触摸是否在您的一个视图上CGRectContainsPoint,使用frame各种视图的作为第一个参数,并使用location作为第二个参数。

    识别出手势开始的视图,然后在手势识别器中touchesMoved,或者,如果在手势识别器中, a stateof UIGestureRecognizerStateChanged,并根据translationInView.

  2. 或者(更简单的恕我直言),您可以创建附加到每个子视图的单独手势识别器。后一种方法可能如下所示。例如,您首先添加手势识别器:

    NSArray *views = @[eye1, eye2, lip1, lip2, chin, nose];
    
    for (UIView *view in views)
    {
        view.userInteractionEnabled = YES;
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
        [view addGestureRecognizer:pan];
    }
    

    然后你实现一个handlePanGesture方法:

    - (void)handlePanGesture:(UIPanGestureRecognizer *)gesture
    {
        CGPoint translation = [gesture translationInView:gesture.view];
        if (gesture.state == UIGestureRecognizerStateChanged)
        {
            gesture.view.transform = CGAffineTransformMakeTranslation(translation.x, translation.y);
            [gesture.view.superview bringSubviewToFront:gesture.view];
        }
        else if (gesture.state == UIGestureRecognizerStateEnded)
        {
            gesture.view.transform = CGAffineTransformIdentity;
            gesture.view.center = CGPointMake(gesture.view.center.x + translation.x, gesture.view.center.y + translation.y);
        }
    }
    
于 2013-06-23T01:55:29.533 回答