objective-c - ios 使用约束布局后动画失败

 mobiledu2502868793 发布于 2022-10-25 17:58

运行这段代码后,动画效果失败

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

点击按钮,动画效果会在最后恢复到最初始的位置,是否有人能够简单解答一下,为什么[self.view addSubview:[[UIView alloc] init]]; 会造成这个问题

1 个回答
  • 当你用autolayout布局后,相应的view如果需要做动画,也要改变布局,而不是frame。

    2022-10-27 00:17 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有