【问题标题】:Superview does not receive the touch actionSuperview没有收到触摸动作
【发布时间】:2012-09-21 10:37:12
【问题描述】:

我有一个大小为 (0,0,320,280) 的基本视图 A,它包含另一个大小为 (100,100,50,50) 和视图 B 有一个按钮和一个图像视图 (C) 作为子视图。图像视图框架与视图 B 相同,在 B 的左上角添加了按钮。

我的要求是当我们拖动B的右下角时,它的大小必须增加或减少。

如果我们从除右下角以外的任何其他位置拖动 B,它必须移动。不应修改视图大小。

我的问题是视图 B 没有收到触摸动作。

我添加了下面的代码。请指导我。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];

    //baseVw-------> view B//

    if ([touch view]==baseVw)
    {
        touchStart = [[touches anyObject] locationInView:baseVw];
        isResizingLR = (baseVw.bounds.size.width - touchStart.x < kResizeThumbSize && baseVw.bounds.size.height - touchStart.y < kResizeThumbSize);
        isResizingUL = (touchStart.x <kResizeThumbSize && touchStart.y <kResizeThumbSize);
        isResizingUR = (baseVw.bounds.size.width-touchStart.x < kResizeThumbSize && touchStart.y<kResizeThumbSize);
        isResizingLL = (touchStart.x <kResizeThumbSize && baseVw.bounds.size.height -touchStart.y <kResizeThumbSize);
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchPoint = [[touches anyObject] locationInView:baseVw];
    CGPoint previous=[[touches anyObject]previousLocationInView:baseVw];
    float  deltaWidth = touchPoint.x-previous.x;
    float  deltaHeight = touchPoint.y-previous.y;

    if ([touch view]==baseVw)
    {
        if (isResizingLR)
        {
            baseVw.frame = CGRectMake(baseVw.frame.origin.x, baseVw.frame.origin.y,touchPoint.x + deltaWidth, touchPoint.y + deltaWidth);
        }
        else
        {
            CGPoint activePoint = [[touches anyObject] locationInView:baseVw];

            // Determine new point based on where the touch is now located
            CGPoint newPoint = CGPointMake(baseVw.center.x + (activePoint.x - touchStart.x),
                                       baseVw.center.y + (activePoint.y - touchStart.y));

            //--------------------------------------------------------
            // Make sure we stay within the bounds of the parent view
            //--------------------------------------------------------
            float midPointX = CGRectGetMidX(baseVw.bounds);

            // If too far right...
            if (newPoint.x > baseVw.superview.bounds.size.width  - midPointX)
                newPoint.x = baseVw.superview.bounds.size.width - midPointX;
            else if (newPoint.x < midPointX)    // If too far left...
                newPoint.x = midPointX;

            float midPointY = CGRectGetMidY(baseVw.bounds);

            // If too far down...
            if (newPoint.y > baseVw.superview.bounds.size.height  - midPointY)
                newPoint.y = baseVw.superview.bounds.size.height - midPointY;
            else if (newPoint.y < midPointY)    // If too far up...
                newPoint.y = midPointY;

            // Set new center location
            baseVw.center = newPoint;
        }
    }
}

【问题讨论】:

  • 你的UIImageView(即C)有userInteractionEnabled = YES吗?
  • 是的,我设置 userInteractionEnabled = YES
  • 因此,如果您添加日志语句,则“B”视图永远不会被触及。为什么不让按钮成为“B”的子视图而不是 UIImageView - 它似乎更干净。我还假设“B”大于按钮和图像视图。 “B”有 userInteractionEnabled。
  • 如果你看到这个我就会知道为什么我在 B 中添加了按钮。stackoverflow.com/questions/12525512/…

标签: ios uiview uiimageview touch superview


【解决方案1】:

视图 B 可能不会收到任何触摸事件,因为这些事件被子视图 C 吸收。假设您在 B 中实现了触摸方法,您还应该确保将 B 设置为 firstResponder。实现这个:

-(BOOL)canBecomeFirstResponder
{
return YES;
}

然后在添加 C 作为子视图后调用[self becomeFirstResponder];。如果不调用 touchesBegan/Moved 中的代码(未接收到触摸事件),则它们无关紧要。

【讨论】:

  • 你能检查你的图像视图(C)是否接收到触摸事件吗?如果是这种情况,您可以将视图 B 设置为视图 C 的委托,并将所有触摸事件传递给它。
  • 是的图像视图接收触摸事件。你能提供示例代码来设置委托吗
  • 您可以尝试为视图 C 设置 userInteractionEnabled = NO;,为视图 B 设置相反的值。这应该让触摸通过 imageView 并到达所需的视图。如果这没有帮助,只需在 C 类中创建 B 类的属性并将方法传递给它。
猜你喜欢
  • 2018-01-10
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-08
  • 1970-01-01
相关资源
最近更新 更多