Applying AffineTransform scaling to a UIView changes its center

I run this code to compress UIView when a user clicks on his supervisor. However, using CGAffineTransformScale (), it also changes the center. Is this expected behavior?

-(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) { // }]; } 
+4
source share
2 answers

I finally found out what was going on: Autolayout repositioned the subview and changed the center.

+8
source

I tested this code in 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 started the 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 challenge (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} 

Therefore, I would say no, this is not the expected behavior.

+2
source

Source: https://habr.com/ru/post/1435270/


All Articles