문제

I have one base view A with size of (0,0,320,280), it contain another view B with size of (100,100,50,50) and View B has one button and one image view(C) as sub view. image view frame is same as view B, button has added in B's top left corner.

My requirement is when we drag the B's bottom right corner its size has to increased or decreased.

if we drag the B from any other place except bottom right corner it has to move. view size should not be modified.

My problem is view B does not receive the touch action.

i added the code below. please guide me.

- (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;
        }
    }
}
도움이 되었습니까?

해결책

View B probably does not receive any touch events because these are absorbed by the subview C. Assuming you implemented the touch methods in B you should also make sure that you set B as the firstResponder. Implement this:

-(BOOL)canBecomeFirstResponder
{
return YES;
}

And then call [self becomeFirstResponder]; after you add C as the subview. The code inside the touchesBegan/Moved doesn't matter if they are not being called (touch events not received).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top