How can I create a button with a background color for tvOS while still showing focus?

前端 未结 8 972
Happy的楠姐
Happy的楠姐 2021-01-02 05:02

All I want to do is add a background color to a button for all states. But I want to maintain the automatic focus shadow that you get \"for free\" when using a system button

8条回答
  •  鱼传尺愫
    2021-01-02 05:15

    The Ultimate Solution with inspiration from SomaMan. Just subclass all your custom buttons and you Get this:

    includes: on tap animation and release and drag away.

    //
    //  CustomFocusButton.swift
    //
    
    import UIKit
    
    class CustomFocusButton: UIButton {
    
    
    let focusedScaleFactor : CGFloat = 1.2
    let focusedShadowRadius : CGFloat = 10
    let focusedShadowOpacity : Float = 0.25
    let shadowColor = UIColor.blackColor().CGColor
    let shadowOffSetFocused = CGSizeMake(0, 27)
    let animationDuration = 0.2
    
    override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)
    {
        coordinator.addCoordinatedAnimations({
                if self.focused{
                    UIView.animateWithDuration(self.animationDuration, animations:{ [weak self] () -> Void in
    
                            guard let weakSelf = self else {return}
                            weakSelf.transform = CGAffineTransformMakeScale(weakSelf.focusedScaleFactor, weakSelf.focusedScaleFactor)
                            weakSelf.clipsToBounds = false
                            weakSelf.layer.shadowOpacity = weakSelf.focusedShadowOpacity
                            weakSelf.layer.shadowRadius = weakSelf.focusedShadowRadius
                            weakSelf.layer.shadowColor = weakSelf.shadowColor
                            weakSelf.layer.shadowOffset = weakSelf.shadowOffSetFocused
    
                        },completion:{ [weak self] finished in
    
                            guard let weakSelf = self else {return}
                            if !finished{
                                weakSelf.transform = CGAffineTransformMakeScale(weakSelf.focusedScaleFactor, weakSelf.focusedScaleFactor)
                                weakSelf.clipsToBounds = false
                                weakSelf.layer.shadowOpacity = weakSelf.focusedShadowOpacity
                                weakSelf.layer.shadowRadius = weakSelf.focusedShadowRadius
                                weakSelf.layer.shadowColor = weakSelf.shadowColor
                                weakSelf.layer.shadowOffset = weakSelf.shadowOffSetFocused
                            }
    
                        })
                } else {
                    UIView.animateWithDuration(self.animationDuration, animations:{  [weak self] () -> Void in
                        guard let weakSelf = self else {return}
    
                        weakSelf.clipsToBounds = true
                        weakSelf.transform = CGAffineTransformIdentity
    
                        }, completion: {[weak self] finished in
                            guard let weakSelf = self else {return}
                            if !finished{
                                weakSelf.clipsToBounds = true
                                weakSelf.transform = CGAffineTransformIdentity
                            }
                    })
                }
            }, completion: nil)
    }
    
    override func pressesBegan(presses: Set, withEvent event: UIPressesEvent?) {
        UIView.animateWithDuration(animationDuration, animations: { [weak self] () -> Void in
    
            guard let weakSelf = self else {return}
            weakSelf.transform = CGAffineTransformIdentity
            weakSelf.layer.shadowOffset = CGSizeMake(0, 10);
    
        })
    }
    
    override func pressesCancelled(presses: Set, withEvent event: UIPressesEvent?) {
    
        if focused{
            UIView.animateWithDuration(animationDuration, animations: { [weak self] () -> Void in
                guard let weakSelf = self else {return}
                weakSelf.transform = CGAffineTransformMakeScale(weakSelf.focusedScaleFactor, weakSelf.focusedScaleFactor)
                weakSelf.layer.shadowOffset = weakSelf.shadowOffSetFocused
            })
        }
    
    }
    
    override func pressesEnded(presses: Set, withEvent event: UIPressesEvent?) {
    
        if focused{
            UIView.animateWithDuration(animationDuration, animations: {[weak self] () -> Void in
    
                guard let weakSelf = self else {return}
                weakSelf.transform = CGAffineTransformMakeScale(weakSelf.focusedScaleFactor, weakSelf.focusedScaleFactor)
                weakSelf.layer.shadowOffset = weakSelf.shadowOffSetFocused
            })
        }
    }
    }
    

提交回复
热议问题