Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

This is my Objective C code:

- (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:

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

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

like image 282
Jadekin Avatar asked Jul 06 '15 20:07

Jadekin


People also ask

How does UIView animate work?

it queries the views objects for changed state and gets back the initial and target values and hence knows what properties to change and in what range to perform the changes. it calculates the intermediate frames, based on the duration and initial/target values, and fires the animation.

How do you animate a view in Swift?

To be exact, whenever you want to animate the view, you actually call layoutIfNeeded on the superview of that view. Try this instead: UIView. animate(withDuration: 0.1, delay: 0.1, options: UIViewAnimationOptions.


1 Answers

You are passing enum of type UIViewAnimationTransition to an argument which requires type UIViewAnimationOptions (options argument)

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
    })
}
like image 183
imas145 Avatar answered Nov 13 '22 11:11

imas145