【问题标题】:iOS Swift - Cannot Change Animation Options with Array [UIViewAnimationOptions]iOS Swift - 无法使用数组更改动画选项 [UIViewAnimationOptions]
【发布时间】:2017-01-31 19:38:10
【问题描述】:

我在 Swift 2 中使用 array 的动画选项有一些问题。我可以像这样在 array 添加多个选项:[.Repeat, UIViewAnimationOptions.Autoreverse] 这是如果我添加动画功能(第一个示例):

UIView.animateWithDuration(1, delay: 0, options: [.Repeat, UIViewAnimationOptions.Autoreverse], animations: {

    }) { (true) in

    }

但我不能像这样在array 中添加动画选项(第二个示例):

var animationOptions = [UIViewAnimationOptions]()
animationOptions.append(UIViewAnimationOptions.Repeat)
animationOptions.append(UIViewAnimationOptions.Autoreverse)
UIView.animateWithDuration(1, delay: 0, options: animationOptions, animations: {

    }) { (true) in

    }

有人可以帮我像第二个例子那样在数组中制作动画选项吗?

【问题讨论】:

    标签: ios arrays swift animation swift2


    【解决方案1】:

    你可以像这样使用动画选项:

    斯威夫特 2

    UIView.animateWithDuration(0.2, delay: 0.0, options: [.Repeat, 
    .Autoreverse, .CurveLinear, .CurveEaseOut, .CurveEaseInOut,
    .TransitionCurlUp, .TransitionCurlDown, 
    .TransitionFlipFromBottom,.TransitionFlipFromLeft,.TransitionFlipFromRight, 
    .BeginFromCurrentState, .CurveEaseIn], animations: {}, completion: nil)
    

    Swift 3、4、5

    UIView.animate(withDuration: 0.2, delay: 0.0, options: [.repeat, 
    .autoreverse, .curveLinear, .curveEaseOut, .curveEaseInOut,
    .transitionCurlUp, .transitionCurlDown, 
    .transitionFlipFromBottom,.transitionFlipFromLeft,.transitionFlipFromRight, 
    .beginFromCurrentState, .curveEaseIn], animations: {}, completion: nil)
    

    【讨论】:

    • 感谢您的回答,但我需要数组中的多个动画选项,以便添加无限动画选项。请查看我的问题中的第二个示例。
    • UIView.animateWithDuration(0.2, delay: 0.0, options: [.Repeat, .Autoreverse, .CurveLinear, .CurveEaseOut, .CurveEaseInOut, .TransitionCurlUp, .TransitionCurlDown, .TransitionFlipFromBottom,.TransitionFlipFromLeft,.TransitionFlipFromRight , .BeginFromCurrentState, .CurveEaseIn],动画:{},完成:nil)
    • 你能在数组变量中添加这个值吗? “[.Repeat, .Autoreverse, .CurveLinear, .CurveEaseOut, .CurveEaseInOut, .TransitionCurlUp, .TransitionCurlDown, .TransitionFlipFromBottom,.TransitionFlipFromLeft,.Transitio‌​nFlipFromRight, .BeginFromCurrentState, .CurveEaseIn]”
    【解决方案2】:
        var animationOptions:UIViewAnimationOptions = .repeat
        animationOptions.insert(.autoreverse)
        UIView.animate(withDuration: 0.1, delay: 0.1, options: animationOptions, animations: {
    
        }) { (success:Bool) in
    
        }
    

    您需要使用来自 SetAlgebra 协议的插入,OptionSet 符合该协议。在问题中,您使用的是数组对象而不是 UIViewAnimationOptions。

    【讨论】:

    • 哇哦,非常感谢@New16。这回答了我的问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 2019-09-03
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多