Ashok
Ashok

Reputation: 5655

issue with animate with duration in swift

I got an error while using blocks in animateWithDuration:animations:completion: method

below is my code:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var cell=tableView.cellForRowAtIndexPath(indexPath)
    var backGrndView:UIView?=cell?.contentView.viewWithTag(2) as UIView?
    UIView.animateWithDuration(0.2,
        animations: {
            backGrndView?.backgroundColor=UIColor.redColor()
        },
        completion: { finished in
            backGrndView?.backgroundColor=UIColor.redColor()
    })
}

I tried the solutions at link.

But my problem is not solved.

below is screenshot:

enter image description here

Please help me out.

Thanks in advance

Upvotes: 0

Views: 712

Answers (1)

Kirsteins
Kirsteins

Reputation: 27335

Seems that problem is the fact that swift auto returns if there is only one statement in closure. You can work around this by adding explicit returns:

UIView.animate(withDuration: 0.2,
                           animations: {
                            backGrndView?.backgroundColor = UIColor.red
                            return
},
                           completion: { finished in
                            backGrndView?.backgroundColor = UIColor.red
                            return
})

This statement:

 backGrndView?.backgroundColor = UIColor.red

returns ()? that is same as Void?, but closure return type is Void.

Upvotes: 3

Related Questions