ios Swift 2.0 中的 UIView.animateWithDuration?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31255374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIView.animateWithDuration in Swift 2.0?
提问by Jadekin
Before explain my problem, it is important to say that I already implemented the suggestion made in this questionand I think my doubts about this animateWithDuration
method are quite different, despite both questions having a very similar title.
在解释我的问题之前,重要的是要说我已经实施了这个问题中提出的建议,我认为我对这种animateWithDuration
方法的怀疑是完全不同的,尽管两个问题的标题非常相似。
So, I am a Swift newbie and I am doing some small projects in Swift, based on previous Objective C demos that I did before.
所以,我是一个 Swift 新手,我正在 Swift 中做一些小项目,基于我之前做过的 Objective C 演示。
This is my Objective C code:
这是我的目标 C 代码:
- (void)moveSideBarToXposition: (int) iXposition{
[UIView animateWithDuration:0.5f
delay:0.1
options: UIViewAnimationOptionTransitionNone
animations:^{ self.mainView.frame = CGRectMake(iXposition, 20, self.mainView.frame.size.width, self.mainView.frame.size.height); }
completion:^(BOOL finished){
if (self.isSidebarHidden==YES) {
self.isSidebarHidden = NO;
}
else{
self.isSidebarHidden = YES;
}
}];
}
And this is my Swift version:
这是我的 Swift 版本:
func moveSideBarToXposition(iXposition: Float) {
UIView.animateWithDuration(0.5, delay: 1.0, options: UIViewAnimationTransition.None, animations: { () -> Void in
self.contentView.frame = CGRectMake(iXposition, 20, self.contentView.frame.size.width, self.contentView.frame.size.height)
}, completion: { (finished: Bool) -> Void in
if isMenuHidden == true {
isMenuHidden = false
} else {
isMenuHidden = true
}
})
}
And I get this error.
我收到这个错误。
Cannot invoke 'animateWithDuration' with an argument list of type '(Double, delay: Double, options: UIViewAnimationTransition, animations: () -> Void, completion: (Bool) -> Void)'
无法使用类型为 '(Double, delay: Double, options: UIViewAnimationTransition, animations: () -> Void, completion: (Bool) -> Void)' 的参数列表调用 'animateWithDuration'
I read the documentation but actually I am not sure what the problem is.
我阅读了文档,但实际上我不确定问题是什么。
Btw, i am working on Xcode 7 and Swift 2.0
顺便说一句,我正在使用 Xcode 7 和 Swift 2.0
回答by imas145
You are passing enum of type UIViewAnimationTransition
to an argument which requires type UIViewAnimationOptions
(options
argument)
您正在将类型的枚举传递UIViewAnimationTransition
给需要类型UIViewAnimationOptions
(options
参数)的参数
Here is the correct syntax with the correct enum value:
这是具有正确枚举值的正确语法:
func moveSideBarToXposition(iXposition: Float) {
let convertedXposition = CGFloat(iXposition)
UIView.animateWithDuration(0.5, delay: 1.0, options: UIViewAnimationOptions.TransitionNone, animations: { () -> Void in
self.contentView.frame = CGRectMake(convertedXposition, 20, self.contentView.frame.size.width, self.contentView.frame.size.height)
}, completion: { (finished: Bool) -> Void in
// you can do this in a shorter, more concise way by setting the value to its opposite, NOT value
isMenuHidden = !isMenuHidden
})
}