0

サイズが(0,0,320,280)のベースビューAが1つあり、サイズが(100,100,50,50)の別のビューBが含まれ、ビューBには1つのボタンと1つの画像ビュー(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;
        }
    }
}
4

1 に答える 1

0

サブビュー C によって吸収されるため、ビュー B はおそらくタッチ イベントを受け取りません。タッチ メソッドを B に実装すると仮定すると、B を firstResponder として設定することも確認する必要があります。これを実装します:

-(BOOL)canBecomeFirstResponder
{
return YES;
}

そして[self becomeFirstResponder];、サブビューとして C を追加した後に呼び出します。touchesBegan/Moved 内のコードは、呼び出されていない場合 (タッチ イベントが受信されていない場合) は関係ありません。

于 2012-09-21T15:16:31.420 に答える