UIView.animateWithDuration 不动画 Swift(再次) [英] UIView.animateWithDuration Not Animating Swift (again)

查看:46
本文介绍了UIView.animateWithDuration 不动画 Swift(再次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我已经检查了以下堆栈溢出问题:

27907570322292522611814131604300>

当被附加到按钮的 IBAction 调用时,我想要做的就是在视图中淡入淡出动画(通过 alpha).然后在点击视图上的按钮时反转.

我的问题可能是我正在使用故事板视图中 ViewDock 上的辅助视图.视图在 viewDidLoad 时添加到子视图,其中框架/边界设置为与超级视图相同(用于完整停留)

这是作为一个叠加视图完成的原因,因为它是一个教程指示器.

结果(与列出此问题的许多其他人一样)是视图(和包含的控件)只是立即出现并立即消失.不褪色.

我尝试过带延迟、带完成和不带完成、带过渡的 animationWithDuration,甚至从旧的 UIView.beginAnimations 开始.

没有任何效果.欢迎提出建议.

代码尽可能直接:
将代码扩展到所有相关内容
Edit2: TL;DR 除了 UIViewAnimateWithDuration 之外,一切都可以正常工作,它似乎忽略了块和持续时间,而只是直接运行内联代码作为 UI 更改.解决这个问题将获得奖励

@IBOutlet var infoDetailView:UIView!//连接到SceneDock中的视图覆盖 func viewDidLoad() {super.viewDidLoad()//剪切其他不相关的 vDL 代码设置信息视图()}功能设置信息视图(){infoDetailView.alpha = 0.0view.addSubview(infoDetailView)updateInfoViewRect(infoDetailView.superview!.bounds.size)}func updateInfoViewRect(size:CGSize) {让 viewRect = CGRect(原点:CGPointZero,大小:大小)infoDetailView.frame = viewRectinfoDetailView.bounds = viewRectinfoDetailView.layoutIfNeeded()infoDetailView.setNeedsDisplay()}覆盖 func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)更新信息视图矩形(大小)}功能隐藏信息视图(){AFLog.enter(thisClass)UIView.animateWithDuration(2.0,动画:{self.infoDetailView.alpha = 0.0},完成:{(完成)在返回真})AFLog.exit(thisClass)}func showInfoView() {AFLog.enter(thisClass)UIView.animateWithDuration(2.0,动画:{self.infoDetailView.alpha = 0.75},完成:{(完成)在返回真})AFLog.exit(thisClass)}//标记:- IBActions@IBAction func openInfoView(sender: UIButton) {显示信息视图()}@IBAction func closeInfoView(sender: UIButton) {隐藏信息视图()}

请注意,我从以下几点开始:

func showInfoView() {UIView.animateWithDuration(2.0, animations: { () -> Void inself.infoDetailView.alpha = 0.75})}功能隐藏信息视图(){UIView.animateWithDuration(2.0, animations: { () -> Void inself.infoDetailView.alpha = 0.00})}

解决方案

如果 infoDetailView 处于自动布局约束下,则需要在 parent 视图上调用 layoutIfNeeded inside animateWithDuration:

func showInfoView() {self.view.layoutIfNeeded()//在这里也调用它来完成挂起的布局操作UIView.animate(withDuration: 2.0, 动画: {self.infoDetailView.alpha = 0.75self.view.layoutIfNeeded()})}

理论上,如果您只是更改 .alpha 值,则不需要这样做,但 也许在这种情况下可能是问题所在.

Note: I’ve already checked the following stack overflow issues:

27907570, 32229252, 26118141, 31604300

All I am trying to do is fade animate in a view (by alpha) when called by an IBAction attached to a button. Then reverse when a button on the view is hit.

My wrinkle may be that I'm using a secondary view that is on the ViewDock in the storyboard View. The view is added to the subview at the time of viewDidLoad where the frame/bounds are set to the same as the superview (for a full layover)

The reason this is done as an overlay view since it is a tutorial indicator.

The result (like many others who've listed this problem) is that the view (and contained controls) simply appears instantly and disappears as instantly. No fade.

I have tried animationWithDuration with delay, with and without completion, with transition, and even started with the old UIView.beginAnimations.

Nothing is working. Suggestions warmly welcomed.

The code is about as straight forward as I can make it:
Edit: Expanded the code to everything relevant
Edit2: TL;DR Everything works with the exception of UIViewAnimateWithDuration which seems to ignore the block and duration and just run the code inline as an immediate UI change. Solving this gets the bounty

@IBOutlet var infoDetailView: UIView! // Connected to the view in the SceneDock

override func viewDidLoad() {
    super.viewDidLoad()

    // Cut other vDL code that isn't relevant

    setupInfoView()
}

func setupInfoView() {
    infoDetailView.alpha = 0.0
    view.addSubview(infoDetailView)
    updateInfoViewRect(infoDetailView.superview!.bounds.size)
}

func updateInfoViewRect(size:CGSize) {
    let viewRect = CGRect(origin: CGPointZero, size: size)

    infoDetailView.frame = viewRect
    infoDetailView.bounds = viewRect

    infoDetailView.layoutIfNeeded()
    infoDetailView.setNeedsDisplay()
}

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
    updateInfoViewRect(size)
}

func hideInfoView() {
    AFLog.enter(thisClass)
    UIView.animateWithDuration(
        2.0,
        animations:
        {
            self.infoDetailView.alpha = 0.0
        },
        completion:
        { (finished) in
            return true
        }
    )
    AFLog.exit(thisClass)
}

func showInfoView() {
    AFLog.enter(thisClass)
    UIView.animateWithDuration(
        2.0,
        animations:
        {
            self.infoDetailView.alpha = 0.75
        },
        completion:
        { (finished) in
            return true
        }
    )
    AFLog.exit(thisClass)
}

// MARK: - IBActions

@IBAction func openInfoView(sender: UIButton) {
    showInfoView()
}

@IBAction func closeInfoView(sender: UIButton) {
    hideInfoView()
}

Please note, I started with the following:

func showInfoView() {
    UIView.animateWithDuration(2.0, animations: { () -> Void in
        self.infoDetailView.alpha = 0.75
    })
}

func hideInfoView() {
    UIView.animateWithDuration(2.0, animations: { () -> Void in
        self.infoDetailView.alpha = 0.00
    })
}

解决方案

If you infoDetailView is under auto layout constraints you need to call layoutIfNeeded on the parent view inside animateWithDuration:

func showInfoView() {
    self.view.layoutIfNeeded() // call it also here to finish pending layout operations
    UIView.animate(withDuration: 2.0, animations: {
        self.infoDetailView.alpha = 0.75
        self.view.layoutIfNeeded()
    })
}

Theoretically this should not be needed if you just change the .alpha value, but maybe this could be the problem in this case.

这篇关于UIView.animateWithDuration 不动画 Swift(再次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆