简体   繁体   English

Swift 2.0中的UIView.animateWithDuration?

[英]UIView.animateWithDuration in Swift 2.0?

Before explain my problem, it is important to say that I already implemented the suggestion made in this question and 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: 这是我的Objective 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)' 无法使用类型'的参数列表调用'animateWithDuration'(Double,delay:Double,options:UIViewAnimationTransition,animations :() - > Void,completion:(Bool) - > Void)'

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

You are passing enum of type UIViewAnimationTransition to an argument which requires type UIViewAnimationOptions ( options argument) 您正在将类型为UIViewAnimationTransition枚举传递给需要类型为UIViewAnimationOptionsoptions参数)的参数

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
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM