0

I am building a uicollectionview that will have rows of cells. Each cell will contain a UIButton. I want to be able to 'zoom into' the UIButton on tap. I have discovered this code for zooming in on self.view, but I am not sure how to make it properly zoom in on the center of the tapped UIButton. Right now, it zooms in towards the bottom right corner of the screen.

Any suggestions on properly centering in on the tapped button?

-(void)didTapButton:(UIButton*)sender{
    CGFloat s = 3;
    CGAffineTransform tr = CGAffineTransformScale(self.view.transform, s, s);
    CGFloat h = sender.frame.size.height;
    CGFloat w = sender.frame.size.width;
    [UIView animateWithDuration:2.5 delay:0 options:0 animations:^{
        self.view.transform = tr;
        self.view.center = CGPointMake(w*s/2,h-h*s/2);
    } completion:^(BOOL finished) {}];
}
JimmyJammed
  • 9,598
  • 19
  • 79
  • 146

1 Answers1

0

If you're using auto layout, you won't be able to use view animation in the way you're doing, because layout is triggered by the transform and keeps resetting the frame. This could explain why the zoom seems to expand the view down and to the right rather than from the center. (You might like to see my essay on the struggle between auto layout and view transforms.)

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Yeah I just tried disabling auto layout and it still zooms into the same spot. I am guessing most likely it is just the way I am calculating the position. Must need a bit of different logic. – JimmyJammed May 11 '14 at 18:16
  • Yes, well, I was a bit concerned that you are doing the scale transform _and_ moving the center simultaneously, which seems kind of iffy. – matt May 11 '14 at 18:21
  • And your center calculation makes no sense, because the sender's frame is in terms of its superview's bounds, but `self.view.center` is in terms of _its_ superview's bounds, so you are using two completely different coordinate systems simultaneously without converting. – matt May 11 '14 at 18:24