Skip to content

Instantly share code, notes, and snippets.

@taktamur
Created April 3, 2018 14:22
Show Gist options
  • Save taktamur/94759b6330cef1e1f5f322626bb8ea65 to your computer and use it in GitHub Desktop.
Save taktamur/94759b6330cef1e1f5f322626bb8ea65 to your computer and use it in GitHub Desktop.
class SelectedCell:UITableViewCell {
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
// ↓を呼び出すと、highlightedがfalseの場合に、
// UITableViewCellSelectionStyleで指定した色(.grayとか)になるみたい。
// super.setHighlighted(highlighted, animated: animated)
if( highlighted ) {
// こっちは指で押した時に呼び出される
// → 押しっぱなしにしていると赤が見える
self.contentView.backgroundColor = UIColor.red
}else{
// 指を離した時に呼び出される
self.contentView.backgroundColor = UIColor.green
// 元の色に戻るようなアニメーションを仕込める
UIView.animate(withDuration: 0.5, animations: {
self.contentView.backgroundColor = UIColor.white
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment