Superview没有收到触摸动作

时间:2012-09-21 10:37:12

标签: ios uiview uiimageview touch superview

我有一个大小为(0,0,320,280)的基本视图A,它包含另一个大小为的视图B. (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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

-(BOOL)canBecomeFirstResponder
{
return YES;
}

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