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

参考帖子:自动布局 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];
}];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值