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

View File

@@ -57,6 +57,11 @@ class SwitchesViewController: UIViewController,
{
let reuseID = SwitchesViewController.collectionViewCellReuseIdentifier
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseID, forIndexPath: indexPath) as! WemoDeviceCellView
var device = WemoDevice()
device.name = "beatmania IIDX"
cell.device = device
cell.ordinal = indexPath.row + 1
return cell
@@ -71,4 +76,10 @@ class SwitchesViewController: UIViewController,
let dimensions = rint(collectionView.bounds.size.width / cellsPerRow) - spacing
return CGSize(width: dimensions, height: dimensions)
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! WemoDeviceCellView
cell.toggled = !cell.toggled
}
}

View File

@@ -9,32 +9,39 @@
import UIKit
class WemoDeviceCellView: UICollectionViewCell {
private var _device: WemoDevice?
private var _ordinal: Int = 0
private var _nameLabel: UILabel = UILabel()
private var _ordinalLabel: UILabel = UILabel()
private var _indicator: SwitchIndicatorView = SwitchIndicatorView()
private var _device: WemoDevice?
private var _ordinal: Int = 0
private var _selectionOverlayView: UIView = UIView()
private var _nameLabel: UILabel = UILabel()
private var _ordinalLabel: UILabel = UILabel()
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)
{
super.init(frame: frame)
let annotationsColor = UIColor.darkGrayColor()
self.backgroundColor = UIColor(white: 0.2, alpha: 1.0)
self.contentView.backgroundColor = WemoDeviceCellView.disabledBackgroundColor
_selectionOverlayView.backgroundColor = UIColor.clearColor()
self.contentView.addSubview(_selectionOverlayView)
_nameLabel.font = UIFont(name: "Orbitron-Medium", size: 16.0)
_nameLabel.numberOfLines = 3
_nameLabel.textColor = UIColor.whiteColor()
_nameLabel.text = "beatmania IIDX".uppercaseString
self.addSubview(_nameLabel)
self.contentView.addSubview(_nameLabel)
_ordinalLabel.font = UIFont(name: "Orbitron-Medium", size: 21.0)
_ordinalLabel.textColor = annotationsColor
_ordinalLabel.textColor = WemoDeviceCellView.disabledAnnotationsColor
_ordinalLabel.text = "00"
self.addSubview(_ordinalLabel)
self.contentView.addSubview(_ordinalLabel)
_indicator.foregroundColor = annotationsColor
self.addSubview(_indicator)
_indicator.foregroundColor = WemoDeviceCellView.disabledAnnotationsColor
self.contentView.addSubview(_indicator)
}
required init?(coder aDecoder: NSCoder)
@@ -48,9 +55,11 @@ class WemoDeviceCellView: UICollectionViewCell {
{
super.layoutSubviews()
let bounds = self.bounds
let bounds = self.contentView.bounds
let padding: CGFloat = 10.0
_selectionOverlayView.frame = bounds
_indicator.frame = CGRect(
x: padding,
y: padding,
@@ -74,6 +83,19 @@ class WemoDeviceCellView: UICollectionViewCell {
height: ordinalLabelSize.height
)
}
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
@@ -86,7 +108,7 @@ class WemoDeviceCellView: UICollectionViewCell {
set(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)
}
}
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
}
}
}
}