相关疑难解决方法(0)

如何为约束更改设置动画?

我正在更新旧的应用程序,AdBannerView当没有广告时,它会从屏幕上滑落.当有广告时,它会在屏幕上滑动.基本的东西.

旧样式,我在动画块中设置框架.新风格,我有一个IBOutlet确定Y位置的约束,在这种情况下,它与superview底部的距离,并修改常量.

- (void)moveBannerOffScreen {
    [UIView animateWithDuration:5 animations:^{
        _addBannerDistanceFromBottomConstraint.constant = -32;
    }];
    bannerIsVisible = FALSE;
}

- (void)moveBannerOnScreen {
    [UIView animateWithDuration:5 animations:^{
        _addBannerDistanceFromBottomConstraint.constant = 0;
    }];
    bannerIsVisible = TRUE;
}
Run Code Online (Sandbox Code Playgroud)

横幅移动,完全符合预期,但没有动画.

更新:我重新观看了WWDC12视频" 掌握自动布局的最佳实践 ",其中包括动画.它讨论了如何使用更新约束AdBannerView.

在此输入图像描述 在此输入图像描述

我尝试使用以下代码,但得到完全相同的结果.

- (void)moveBannerOffScreen {
    _addBannerDistanceFromBottomConstraint.constant = -32;
    [UIView animateWithDuration:2 animations:^{
        [self.view setNeedsLayout];
    }];
    bannerIsVisible = FALSE;
}

- (void)moveBannerOnScreen {
    _addBannerDistanceFromBottomConstraint.constant = 0;
    [UIView animateWithDuration:2 animations:^{
        [self.view setNeedsLayout];
    }];
    bannerIsVisible = TRUE;
}
Run Code Online (Sandbox Code Playgroud)

在旁注中,我已经多次检查过,这是在主线程上执行的.

animation objective-c ios autolayout ios6

934
推荐指数
9
解决办法
27万
查看次数

更改约束时`self.view.layoutIfNeeded()`做什么

所以我正在学习如何从屏幕外制作视图动画,以用作滑块菜单.

class ViewController: UIViewController {
    @IBOutlet weak var red: UIView!
    @IBOutlet weak var redHConstraint: NSLayoutConstraint!

    @IBAction func buttonShift(sender: AnyObject) {
        self.view.layoutIfNeeded()        // **HERE**
        UIView.animateWithDuration(0.5) {
            self.redHConstraint.constant = 0
            self.view.layoutIfNeeded()    // And **HERE**
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我已经改编了如何使用Swift中的约束来动画UIView的代码?

1. self.view.layoutIfNeeded()部分代码有什么作用?

2.为什么在动画之前和期间编码2x?

注意:如果我注释掉第1个self.view.layoutIfNeeded()没什么变化,但是如果我注释掉第2个self.view.layoutIfNeeded()动作不再是动画并且只是出现在新坐标中.

ios swift

14
推荐指数
1
解决办法
2万
查看次数

标签 统计

ios ×2

animation ×1

autolayout ×1

ios6 ×1

objective-c ×1

swift ×1