Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用Swift连续设置UIView数组的动画_Ios_Animation_Recursion_Uiview_Map Function - Fatal编程技术网

Ios 使用Swift连续设置UIView数组的动画

Ios 使用Swift连续设置UIView数组的动画,ios,animation,recursion,uiview,map-function,Ios,Animation,Recursion,Uiview,Map Function,我有一个视图数组(UIStackView.arrangedSubviews),我想用UIView.animate()对其进行动画制作。但我只想在上一个元素完成后开始为下一个元素设置动画 我想找到一个有点“优雅”的解决方案。我试过两种不同的方法,但我都坚持。我怎样才能做到这一点 第一次尝试:递归闭包 第二次尝试:使用数组映射 只需枚举它们,不使用completion,而是使用delay参数设置它们的动画效果如何?您可以将ith视图的延迟计算为i*animDuration: let animDura

我有一个视图数组(
UIStackView.arrangedSubviews
),我想用
UIView.animate()
对其进行动画制作。但我只想在上一个元素完成后开始为下一个元素设置动画

我想找到一个有点“优雅”的解决方案。我试过两种不同的方法,但我都坚持。我怎样才能做到这一点

第一次尝试:递归闭包 第二次尝试:使用数组映射
只需枚举它们,不使用
completion
,而是使用
delay
参数设置它们的动画效果如何?您可以将
i
th视图的延迟计算为
i*animDuration

let animDuration = 0.5
for (index, view) in stackView.arrangedSubviews.enumerated() {
    view.frame.origin.y += 30.0
    UIView.animate(withDuration: animDuration, delay: animDuration * Double(index), options: [.curveLinear], animations: {
        view.alpha = 1
        view.frame.origin.y -= 30.0
    }, completion: nil)
}
注意:
UIStackView
使用自动布局来布局其排列的子视图。直接设置帧时要小心

编辑

虽然我认为您可以非常确定使用
延迟
将如您所期望的那样工作,但您可以尝试使用
UIViewPropertyAnimator
对象链接动画:

var animators: [UIViewPropertyAnimator] = []
stackView.arrangedSubviews.forEach({ (view) in
    let animator = UIViewPropertyAnimator(duration: 0.5, timingParameters: UICubicTimingParameters(animationCurve: .linear))
    view.frame.origin.y += 30.0
    animator.addAnimations {
        view.alpha = 1
        view.frame.origin.y -= 30.0
    }
    // start this animator in completion of the previous one (if there is previous one)
    let previousAnimator = animators.last
    previousAnimator?.addCompletion({ (_) in
        animator.startAnimation()
    })

    // append new animator to animators
    animators.append(animator)
})

// now you should be able to run the chain by starting the first animator
animators.first?.startAnimation()

只需枚举它们,不使用
completion
,而是使用
delay
参数设置它们的动画效果如何?您可以将
i
th视图的延迟计算为
i*animDuration

let animDuration = 0.5
for (index, view) in stackView.arrangedSubviews.enumerated() {
    view.frame.origin.y += 30.0
    UIView.animate(withDuration: animDuration, delay: animDuration * Double(index), options: [.curveLinear], animations: {
        view.alpha = 1
        view.frame.origin.y -= 30.0
    }, completion: nil)
}
注意:
UIStackView
使用自动布局来布局其排列的子视图。直接设置帧时要小心

编辑

虽然我认为您可以非常确定使用
延迟
将如您所期望的那样工作,但您可以尝试使用
UIViewPropertyAnimator
对象链接动画:

var animators: [UIViewPropertyAnimator] = []
stackView.arrangedSubviews.forEach({ (view) in
    let animator = UIViewPropertyAnimator(duration: 0.5, timingParameters: UICubicTimingParameters(animationCurve: .linear))
    view.frame.origin.y += 30.0
    animator.addAnimations {
        view.alpha = 1
        view.frame.origin.y -= 30.0
    }
    // start this animator in completion of the previous one (if there is previous one)
    let previousAnimator = animators.last
    previousAnimator?.addCompletion({ (_) in
        animator.startAnimation()
    })

    // append new animator to animators
    animators.append(animator)
})

// now you should be able to run the chain by starting the first animator
animators.first?.startAnimation()

谢谢你。关于你的PS便笺,你对我应该如何做有什么建议吗?不确定你到底想要实现什么,但我通常只是将
view.ishiden
设置为
true
以使其隐藏,然后在设置动画时设置为
false
。但这可能不是你想要达到的效果。视图正在逐渐变暗,同时也在向上滑动。但很高兴知道这是一个值得研究的问题。@eivindml看看更新的答案。还没有测试过,所以这将是您的任务:)谢谢。关于你的PS便笺,你对我应该如何做有什么建议吗?不确定你到底想要实现什么,但我通常只是将
view.ishiden
设置为
true
以使其隐藏,然后在设置动画时设置为
false
。但这可能不是你想要达到的效果。视图正在逐渐变暗,同时也在向上滑动。但很高兴知道这是一个值得研究的问题。@eivindml看看更新的答案。还没有测试,所以这将是您的任务:)我的更新答案有效吗?我的更新答案有效吗?