Cell selection and animation

This commit is contained in:
Charles Magahern
2015-12-31 16:57:57 -08:00
parent df0f72af23
commit 303c620372
2 changed files with 71 additions and 15 deletions
@@ -57,6 +57,11 @@ class SwitchesViewController: UIViewController,
{ {
let reuseID = SwitchesViewController.collectionViewCellReuseIdentifier let reuseID = SwitchesViewController.collectionViewCellReuseIdentifier
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseID, forIndexPath: indexPath) as! WemoDeviceCellView let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseID, forIndexPath: indexPath) as! WemoDeviceCellView
var device = WemoDevice()
device.name = "beatmania IIDX"
cell.device = device
cell.ordinal = indexPath.row + 1 cell.ordinal = indexPath.row + 1
return cell return cell
@@ -71,4 +76,10 @@ class SwitchesViewController: UIViewController,
let dimensions = rint(collectionView.bounds.size.width / cellsPerRow) - spacing let dimensions = rint(collectionView.bounds.size.width / cellsPerRow) - spacing
return CGSize(width: dimensions, height: dimensions) return CGSize(width: dimensions, height: dimensions)
} }
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! WemoDeviceCellView
cell.toggled = !cell.toggled
}
} }
+55 -10
View File
@@ -11,30 +11,37 @@ import UIKit
class WemoDeviceCellView: UICollectionViewCell { class WemoDeviceCellView: UICollectionViewCell {
private var _device: WemoDevice? private var _device: WemoDevice?
private var _ordinal: Int = 0 private var _ordinal: Int = 0
private var _selectionOverlayView: UIView = UIView()
private var _nameLabel: UILabel = UILabel() private var _nameLabel: UILabel = UILabel()
private var _ordinalLabel: UILabel = UILabel() private var _ordinalLabel: UILabel = UILabel()
private var _indicator: SwitchIndicatorView = SwitchIndicatorView() private var _indicator: SwitchIndicatorView = SwitchIndicatorView()
static private var disabledBackgroundColor = UIColor(white: 0.2, alpha: 1.0)
static private var enabledBackgroundColor = UIColor(red: 0.0, green: 0.8, blue: 0.0, alpha: 1.0)
static private var disabledAnnotationsColor = UIColor.darkGrayColor()
static private var enabledAnnotationsColor = UIColor.blackColor()
override init(frame: CGRect) override init(frame: CGRect)
{ {
super.init(frame: frame) super.init(frame: frame)
let annotationsColor = UIColor.darkGrayColor() self.contentView.backgroundColor = WemoDeviceCellView.disabledBackgroundColor
self.backgroundColor = UIColor(white: 0.2, alpha: 1.0)
_selectionOverlayView.backgroundColor = UIColor.clearColor()
self.contentView.addSubview(_selectionOverlayView)
_nameLabel.font = UIFont(name: "Orbitron-Medium", size: 16.0) _nameLabel.font = UIFont(name: "Orbitron-Medium", size: 16.0)
_nameLabel.numberOfLines = 3 _nameLabel.numberOfLines = 3
_nameLabel.textColor = UIColor.whiteColor() _nameLabel.textColor = UIColor.whiteColor()
_nameLabel.text = "beatmania IIDX".uppercaseString self.contentView.addSubview(_nameLabel)
self.addSubview(_nameLabel)
_ordinalLabel.font = UIFont(name: "Orbitron-Medium", size: 21.0) _ordinalLabel.font = UIFont(name: "Orbitron-Medium", size: 21.0)
_ordinalLabel.textColor = annotationsColor _ordinalLabel.textColor = WemoDeviceCellView.disabledAnnotationsColor
_ordinalLabel.text = "00" _ordinalLabel.text = "00"
self.addSubview(_ordinalLabel) self.contentView.addSubview(_ordinalLabel)
_indicator.foregroundColor = annotationsColor _indicator.foregroundColor = WemoDeviceCellView.disabledAnnotationsColor
self.addSubview(_indicator) self.contentView.addSubview(_indicator)
} }
required init?(coder aDecoder: NSCoder) required init?(coder aDecoder: NSCoder)
@@ -48,9 +55,11 @@ class WemoDeviceCellView: UICollectionViewCell {
{ {
super.layoutSubviews() super.layoutSubviews()
let bounds = self.bounds let bounds = self.contentView.bounds
let padding: CGFloat = 10.0 let padding: CGFloat = 10.0
_selectionOverlayView.frame = bounds
_indicator.frame = CGRect( _indicator.frame = CGRect(
x: padding, x: padding,
y: padding, y: padding,
@@ -75,6 +84,19 @@ class WemoDeviceCellView: UICollectionViewCell {
) )
} }
override var highlighted: Bool {
didSet
{
if (self.highlighted) {
_selectionOverlayView.backgroundColor = UIColor(white: 0.8, alpha: 1.0)
} else {
UIView.animateWithDuration(1.0, animations: { () -> Void in
self._selectionOverlayView.backgroundColor = UIColor.clearColor()
})
}
}
}
// MARK: API // MARK: API
var device: WemoDevice? { var device: WemoDevice? {
@@ -86,7 +108,7 @@ class WemoDeviceCellView: UICollectionViewCell {
set(device) set(device)
{ {
_device = device _device = device
_nameLabel.text = _device?.name _nameLabel.text = _device?.name.uppercaseString
} }
} }
@@ -102,4 +124,27 @@ class WemoDeviceCellView: UICollectionViewCell {
_ordinalLabel.text = String(format: "%.2d", _ordinal) _ordinalLabel.text = String(format: "%.2d", _ordinal)
} }
} }
var toggled: Bool {
get
{
return (_device?.state == .On)
}
set(toggled)
{
_device?.state = (toggled ? .On : .Off)
_indicator.status = toggled
if (toggled) {
self.contentView.backgroundColor = WemoDeviceCellView.enabledBackgroundColor
_indicator.foregroundColor = WemoDeviceCellView.enabledAnnotationsColor
_ordinalLabel.textColor = WemoDeviceCellView.enabledAnnotationsColor
} else {
self.contentView.backgroundColor = WemoDeviceCellView.disabledBackgroundColor
_indicator.foregroundColor = WemoDeviceCellView.disabledAnnotationsColor
_ordinalLabel.textColor = WemoDeviceCellView.disabledAnnotationsColor
}
}
}
} }