[iOS AutoLayout动画 坑] AutoLayout动画平移坑总结 => Swift/OC

来源:互联网 发布:985软件工程硕士 编辑:程序博客网 时间:2024/06/06 03:55

参考帖子:自动布局 Autolayout 报错:Unable to simultaneously satisfy constraints.

参考帖子: Auto Layout 进阶

参考帖子:谈StoryBoard上AutoLayout的约束动画

学习尝试使用Swift做约束动画 实现这样的效果

===>> 往下刷 <<===

⬇️⬇️⬇️

结果 我在学习的时候出现的效果是

↘️↙️
↗️↖️

这样的缩进方式 我瞬间蛋疼了

解决方法是

1.在确保设置好约束

2.一定要在 -(void)viewDidAppear:(BOOL)animated 方法中实现动画 不然要么不执行 要么执行奇怪的动画 (都可以 关键是第三条)

3. 在动画代码写好,强制更新约束的时候

[self.chongjiboView layoutIfNeeded]; 确保强制更新约束的对象 这个是❌ 的

[self.view layoutIfNeeded]; 是正确的 ✅ (这个要看你动画对象, 这里也作为一个坑写出来,反正我是被教学视频的坑了一下)

4. 如果出现

“`Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don’t want. Try this: (1) look at each constraint and try to figure out which you don’t expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you’re seeing NSAutoresizingMaskLayoutConstraints that you don’t understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(

这种类似的 估计是你约束设置的不对 (肯能冲突/可能约束超出 好像不影响反正)

5.如果要在一开始修改约束 一定要在 - (void)viewDidLoad 方法里写,如果写在viewDidApper方法 好像不起作用

%%%%%%以上就是坑总结,下面是swift / oc 的代码 %%%%%%%

swift

override func viewDidLoad() {    super.viewDidLoad()    self.scanlineCons.constant = -300}override func viewDidAppear(_ animated: Bool) {    self.scanlineCons.constant = 300    UIView.animate(withDuration: 1.0) {        UIView.setAnimationRepeatCount(MAXFLOAT)        self.view.layoutIfNeeded()    }}

oc

  • (void)viewDidLoad {
    [super viewDidLoad];

    self.topCons.constant = -300;
    // [self.chongjiboView setTranslatesAutoresizingMaskIntoConstraints:NO];
    }

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[UIView animateWithDuration:2.0 animations:^{
self.topCons.constant = 300;
[UIView setAnimationRepeatCount:88];
[self.view layoutIfNeeded];
}];
}

0 0