Question

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) {
                         //
                     }];


}
Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top