After running this code, the animation effect failed
#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()
@property (nonatomic,strong)UIView *a;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *a = [[UIView alloc] init];
a.backgroundColor = [UIColor redColor];
[self.view addSubview:a];
self.a = a;
[a mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view);
make.right.equalTo(self.view);
make.bottom.equalTo(self.view.mas_bottom);
make.height.equalTo(@100);
}];
UIButton *b = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 200 , 200)];
b.backgroundColor = [UIColor greenColor];
[self.view addSubview:b];
[b addTarget:self action:@selector(ad) forControlEvents:UIControlEventTouchUpInside];
}
-(void)ad{
[UIView animateWithDuration:2 delay:1 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.a.center = CGPointMake(self.a.center.x, self.a.center.y - 200);
} completion:^(BOOL finished) {
NSLog(@"11111 %@",self.a);
[self.view addSubview:[[UIView alloc] init]];
NSLog(@"12222222 %@",self.a);
}];
}
Click the button, and the animation effect will return to its original position at the end. Can anyone give a simple answer as to why [self.view addSubview:[[UIView alloc] init]]; causes this problem
When you use autolayout for layout, if the corresponding view needs to be animated, you must also change the layout, not the frame.