【问题标题】:UITapGestureRecognizer and UIPanGestureRecognizerUITapGestureRecognizer 和 UIPanGestureRecognizer
【发布时间】:2013-12-19 21:24:34
【问题描述】:

我想在点击屏幕时创建并查看 uiimageview。 在我抬起手指之前,我想在屏幕上移动 uiimageview,并且只有在我松开手指时才设置图像。这是我所做的:

- (IBAction)tap:(UITapGestureRecognizer *)recognizer {

    CGPoint location = [recognizer locationInView:self.view];

    UIImageView *circle = [[UIImageView alloc] initWithFrame:CGRectMake(location.x, location.y, 50, 50)];
    circle.userInteractionEnabled = YES;
    [circle setImage:[UIImage imageNamed:@"monkey_1.png"]];
    [self.view addSubview:circle];

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:circle action:nil];
    [recognizer requireGestureRecognizerToFail:pan];

    CGPoint translation = [pan translationInView:circle];
    pan.view.center = CGPointMake(pan.view.center.x + translation.x, pan.view.center.y + translation.y);
    [pan setTranslation:CGPointMake(0, 0) inView:self.view];
}

【问题讨论】:

  • 这可能会更容易用一个手势来完成。做一个长按手势,并在它的动作中检测状态。开始时您将显示图像,更改时您将移动图像,然后在结束时进行清理。

标签: ios uiimageview uigesturerecognizer uipangesturerecognizer uitapgesturerecognizer


【解决方案1】:

您只需使用UIPanGestureRecognizerUILongPressGestureRecognizer 即可完成此操作。在手势处理方法中,检查识别器的state 属性,并在UIGestureRecognizerStateEnded 时显示您的图像(即当用户从屏幕上抬起手指时)。例如:

- (void)handleGesture:(UILongPressGestureRecognizer *)recognizer {
    if(recognizer.state == UIGestureRecognizerStateEnded) {
       // gesture ended: show the image
    }
    else if(recognizerState == UIGestureRecognizerStateBegan) {
        // this code runs when the gesture is started. 
    }
    else if(recognizerState == UIGestureRecognizerStateChanged) {
        // gesture is in progress
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2012-02-11
    • 2012-02-18
    相关资源
    最近更新 更多