0
votes

I have a scroll view with an image. User can touch the scrollview and place rectangle views on it. I want that when user pinch zoom a scroll view then the child subviews should automatically resize according to the ratio. Also user should be able to move or resize the child views with touch events. This is working fine when scroll view is not zoomed but when scroll view is zoomed then the child view cant be resized or dragged as the whole scroll view moves. I tried to change the frame size of child view according to the scroll view size but it is not giving proper results .

Is there any way to recieve touch events when scroll view is zoomed

Thanks in advance

1

1 Answers

0
votes
@interface UIZoomingViewController : UIViewController <UIScrollViewDelegate>
    @property (strong, nonatomic) UIView* substrate;
@end

@implementation UIZoomingViewController

-(id)init /*your init method*/
{
    if(self = [super init])
    {
        UIScrollView* scrollView = ((UIScrollView*)self.view); /*controller view - UIScrollView*/
        [scrollView setDelegate:self];
        /*set contentsize, min max zoom scale*/

        _substrate = [UIView alloc] initWithFrame:scrollView.bounds];
        [scrollView addSubview:_substrate];

        /*
            if you need zoom view, add to substrate
            [_substrate addSubview:...];
        */
    }
    return self;
}

#pragma mark - UIScrollView delegate implementation

-(UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView
{
    return _substrate;
}

@end

and little trick, if you need to receive touches after UIScrollView, create subclass...

@interface UINewScrollView : UIScrollView
@end

@implementation UINewScrollView

-(BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
    return YES;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[self nextResponder] touchesBegan:touches withEvent:event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[self nextResponder] touchesMoved:touches withEvent:event];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[self nextResponder] touchesEnded:touches withEvent:event];
}

@end