Reputation: 2777
I have a custom UICollectionView Cell
. The layout of the cell looks like this
- ContentView
- UILabel
- UIView --> buttonView
- UIButton
ButtonViews
hidden property is standard set to true.
When the user taps the cell. ButtonView
becomes visible.
What my problem now is, is that I don't get the focus to the uibutton
inside the buttonView
This are pieces of my code:
In my viewController:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! ShoppingCartCell
cell.buttonView.hidden = false
cell.buttonView.updateFocusIfNeeded()
}
func collectionView(collectionView: UICollectionView, canFocusItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
In my custom cell:
public override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
if (self.focused)
{
self.transform = CGAffineTransformMakeScale(1.1, 1.1)
}
else
{
self.transform = CGAffineTransformIdentity
buttonView.hidden = true
}
super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)
guard let nextFocusedView = context.nextFocusedView else { return }
switch nextFocusedView {
case self.buttonView:
self.focusGuide.preferredFocusedView = self.deleteButton
default:
self.focusGuide.preferredFocusedView = nil
}
}
Upvotes: 0
Views: 1087
Reputation: 4107
After trying different things I subclassed UIButton for your button you use in a cell, and noticed that calling
self.focusGuide.preferredFocusedView = cell.aButton
that is not making the button in focus, self.focused on that button is false as well as didUpdateFocusInContext in that button class is not getting called.
As a temporary fix and/or future exploration/experimenting you can "fake" focus on your button. For that, as I mentioned before, subclass from UIButton and make your own button class.
In that class add something like this:
func makeFocused()
{
UIView.animateWithDuration(0.2, animations:
{
self.transform = CGAffineTransformMakeScale(1.1, 1.1)
},
completion:
{
finished in
UIView.animateWithDuration(0.2, animations:
{
self.transform = CGAffineTransformIdentity
},
completion: nil)
})
self.layer.shadowOffset = CGSizeMake(10, 10);
self.layer.shadowOpacity = 0.6;
self.layer.shadowRadius = 15;
self.layer.shadowColor = UIColor.blackColor().CGColor;
}
Similarly add unfocus method. Then call focus/unfocus on your button inside of the:
class YourCustomCell: UICollectionViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var aButton: CustomButton!
private var focusGuide = UIFocusGuide()
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)
{
super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)
guard let nextFocusedView = context.nextFocusedView else { return }
guard let prevFocusedView = context.previouslyFocusedView else { return }
if let cell = nextFocusedView as? YourCustomCell
{
cell.aButton.makeFocused()
self.setNeedsFocusUpdate()
return
}
}
Hope this helps :)
Upvotes: 0
Reputation: 1266
You hide buttonview for particular condition but you not show again for another condition. Try this code,
public override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
if (self.focused)
{
self.transform = CGAffineTransformMakeScale(1.1, 1.1)
buttonView.hidden = false
}
else
{
self.transform = CGAffineTransformIdentity
buttonView.hidden = true
}
Upvotes: 1