4

I'm running this code to shrink a UIView when the user taps on its superview. However, when applying the CGAffineTransformScale(), it also changes the centre. Is that the expected behaviour?

-(void) onTap:(UITapGestureRecognizer *)tap{


    [UIView animateWithDuration:1.2
                          delay:0
                        options:0
                     animations:^{
                         CGAffineTransform transform = self.icon.transform;
                         NSLog(@"Previous center: %@", NSStringFromCGPoint(self.icon.center));

                         self.icon.transform = CGAffineTransformScale(transform, 0.5, 0.5);

                         NSLog(@"Next center: %@", NSStringFromCGPoint(self.icon.center));
                     } completion:^(BOOL finished) {
                         //
                     }];


}
cfischer
  • 24,452
  • 37
  • 131
  • 214
  • 1
    The center of the view is moved down and to the left – cfischer Sep 24 '12 at 03:14
  • Would you mind editing the code to output the Bounds and center before the animateWithDuration call and in the completion:^ block? And add the actual NSLog output? I use MonoTouch and I did not see this behavior. The center stayed in place and the view scaled about the center point. – cod3monk3y Sep 24 '12 at 14:02

2 Answers2

8

I finally found out what was going on: Autolayout was repositioning the subview and changing the center.

cfischer
  • 24,452
  • 37
  • 131
  • 214
  • 1
    I've written a little essay on this problem and some possible solutions: http://stackoverflow.com/questions/12943107/how-do-i-adjust-the-anchor-point-of-a-calayer-when-auto-layout-is-being-used/14105757#14105757 – matt Jan 01 '13 at 16:57
  • I'm not using Autolayout and still have the same outcome. – durazno Jan 31 '16 at 07:17
2

I tested with this code in the ViewController using ObjC:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Create a subview
    CGRect bounds = CGRectMake(10, 10, 100, 100);
    UIView* v = [[UIView alloc] initWithFrame: bounds];
    [v setBackgroundColor:[UIColor yellowColor]];
    [self.view addSubview:v];

    icon = v;
}

And kicked off an animation in viewDidAppear:

- (void)viewDidAppear:(BOOL)animated
{
    NSLog(@"Previous center: %@", NSStringFromCGPoint(self.icon.center));

    [UIView animateWithDuration:1.2 
          delay:1.0 
        options:0 
     animations:^{
         NSLog(@"Before center: %@", NSStringFromCGPoint(self.icon.center));

         CGAffineTransform transform = self.icon.transform;
         self.icon.transform = CGAffineTransformScale(transform, 0.5, 0.5);

         NSLog(@"After center: %@", NSStringFromCGPoint(self.icon.center));
     }
     completion:^(BOOL finished){
         // nothing
         NSLog(@"Completion center: %@", NSStringFromCGPoint(self.icon.center));
     }];        
}

And the center was the same in every call (60,60):

2012-09-24 10:22:11.776 ScaleView[19611:f803] Previous center: {60, 60}
2012-09-24 10:22:11.778 ScaleView[19611:f803] Before center: {60, 60}
2012-09-24 10:22:11.779 ScaleView[19611:f803] After center: {60, 60}
2012-09-24 10:22:13.979 ScaleView[19611:f803] Completion center: {60, 60}

So I would say no, that's not the expected behavior.

cod3monk3y
  • 9,508
  • 6
  • 39
  • 54