Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell setHighlighted, setSelected "animateAlongsideTransition" or similar

I want to be able to add custom animations to subclasses of UITableViewCell that would override methods such as:

override func setHighlighted(highlighted: Bool, animated: Bool) {

}

override func setSelected(selected: Bool, animated: Bool) {

}

and match the animation curve and animation duration for the default animations those methods perform.

In other words, how can I find the information about the current animations provided by Apple. I need this in order to add my own custom animations that perfectly matches the default ones

like image 393
user2336702 Avatar asked Apr 20 '16 08:04

user2336702


1 Answers

You can subclass your custom cell in your table view.

And here I create a simple example in Swift where I change the value of a label inside the cell:

import UIKit

class userTableViewCell: UITableViewCell {
@IBOutlet weak var userLabel: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    self.highlighted = false
    self.userLabel.alpha = 0.0
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    if selected {
        self.highlighted = true
    } else {
        self.highlighted = false
    }

    // Configure the view for the selected state
}

override var highlighted: Bool {
    get {
        return super.highlighted
    }
    set {
        if newValue {
            // you could put some animations here if you want
            UIView.animateWithDuration(0.7, delay: 1.0, options: .CurveEaseOut, animations: {
                self.userLabel.text = "select"
                self.userLabel.alpha = 1.0

                }, completion: { finished in
                print("select")
            })
        }
        else {
            self.userLabel.text = "unselect"
        }
        super.highlighted = newValue
    }
}

}

And with the storyboard what you must have: screenshot of Xcode storyboard

like image 108
Stéphane Garnier Avatar answered Oct 16 '22 21:10

Stéphane Garnier