Search code examples
iphoneiosscale

UIView not detecting touch when scaled


I have a view and its subview is UIScrollView and having an UIImageView which is a subview of UIScrollView. When I try to scale UIView,UIScrollView and UIImageView is also getting scaled. But the problem is I'm getting touches on half portion of view.

Here is my code

[UIView animateWithDuration:0.5 delay:0.0 options:0 animations:^{ 
CGFloat DesiredWidth = 250.0;
  CGFloat DesiredHeight = 300.0;
    CGAffineTransform scalingTransform1;
                scalingTransform1 = CGAffineTransformMakeScale(DesiredWidth/self.view1.bounds.size.width,
    DesiredHeight/self.view1.bounds.size.height);
                self.view1.transform = scalingTransform1;
                self.view1.center=CGPointMake(268, 250); }completion:^(BOOL finished) 
             {   }];

Please anyone can tell me how can I get touch.I searched a lot but could not find

Thanks


Solution

  • I think your problem is the size of the very root UIView(i.e. the superview of the UIView you talk about) of your views. When a certain subview is larger than its superview it will not receive touch events in this portions of it's frame that go outside of the superview frame.

    If you have read about the responder chain in iOS you should know that UIView calls -[hitTest: withEvent:] on it's subviews in the order they have been laid out. This method checks if a certain touch points inside the UIView frame then starts to check if a certain subview can handle the touch event in the order they are positioned in the responder chain(the view hierarchy in this case).

    So when the touch event is first received a certain UIView first hit tests itself and when the location of the touch is outside it's frame it does not check it's subviews. So check very carefully all your views' frames and if some root view (it could be the view property of the UIViewController you use) is actually smaller than the subview you touch.

    Also mind the transforms. In the documentation is stated that when some transforms are applied the frames of the views become irrelevant.

    To summarize my reply let's have this situation:

    some_superview.frame = CGRectMake(0, 0, 200, 200);
    some_subview_of_the_superview.frame = CGRectMake(0, 0, 200, 300);
    

    in the region of the subview that is actually (0, 200, 200, 100) which is the bottom 100 points the touches will be ignored by the superview because they are outside it's frame