4

在下面的委托函数中,我试图做但没有得到想要的结果

override func didUpdateFocusInContext(context: UIFocusUpdateContext,withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
    if (context.nextFocusedView == self) {
        coordinator.addCoordinatedAnimations({ () -> Void in
            self.animationDidStop(CAAnimation(), finished: true)
            }, completion: { () -> Void in

        })

    }
    else {
        // handle unfocused appearance changes

        coordinator.addCoordinatedAnimations({ () -> Void in
            self.animationDidStop(CAAnimation(), finished: true)
            }, completion: { () -> Void in

        })
    }
    context.nextFocusedView?.layer.shadowOffset = CGSizeZero
    context.nextFocusedView?.layer.shadowOpacity = 0.9;
    context.nextFocusedView?.layer.shadowRadius = 0;
    context.nextFocusedView?.layer.shadowColor= UIColor.orangeColor().CGColor
    context.previouslyFocusedView?.layer.shadowOpacity = 0;
}
4

1 回答 1

5

首先,您必须将按钮类型设置为自定义类型。通过自定义类型,您将不再获得系统动画,因此您必须自己制作所有动画。

然后,您可以在UIViewController中实现didUpdateFocusInContext方法,或者如果单个屏幕上有更多按钮类型,您可以创建自己的UIButton子类。

这是我在UIButton子类中使用的代码。这将提供按钮放大以及焦点上的红色边框,并在焦点丢失时恢复正常状态。

let scale = 1.1    
layer.borderColor = UIColor.redColor().CGColor

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {

    if context.nextFocusedView == self {
        coordinator.addCoordinatedAnimations({ () -> Void in

            self.transform = CGAffineTransformMakeScale(scale, scale)
            self.layer.borderWidth = 2

            }, completion: nil)
    }
    else {
        coordinator.addCoordinatedAnimations({ () -> Void in

            self.transform = CGAffineTransformIdentity
            self.layer.borderWidth = 0

            }, completion: nil)
    }
}
于 2015-10-29T20:04:15.763 回答