0

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;
        }
    }
}
thavasidurai
  • 1,972
  • 1
  • 26
  • 51
  • Is your `UIImageView` (i.e. **C**) has `userInteractionEnabled = YES` ? – Hemang Sep 21 '12 at 11:20
  • Yes i set userInteractionEnabled = YES – thavasidurai Sep 21 '12 at 11:27
  • So if you add log statements, the 'B' view never gets touches ever. Why not make the button a subview of 'B' not the UIImageView - it would seem to be cleaner. I also assume that 'B' is larger than the button and the image view. 'B' has userInteractionEnabled. – David H Sep 21 '12 at 11:48
  • if you see this i will get to know why i added the button in B. http://stackoverflow.com/questions/12525512/how-to-create-uibutton-in-uiimageviews-top-left-corner-and-bottom-right-corner – thavasidurai Sep 21 '12 at 12:02

1 Answers1

0

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).

Igor N
  • 381
  • 2
  • 6
  • Can you check whether your image view (C) receives the touch events? If that is the case you can set view B as a delegate for view C and pass on all the touch events to it. – Igor N Sep 27 '12 at 11:37
  • Yes image view receives touch event. can u provide sample code to set delegate – thavasidurai Sep 27 '12 at 12:08
  • You can try setting `userInteractionEnabled = NO;` for view C and the opposite for view B. This should let the touches pass through the imageView and on to the required view. If that doesn't help, just create a property of class B in class C and pass the methods to it. – Igor N Sep 27 '12 at 12:30