使用 tvOS 以编程方式触发 UIPress

Trigger a UIPress programmatically with tvOS

我正在尝试以编程方式在 tvOS 中的按钮上触发 UIPress,以便我可以获得用户在单击按钮时收到的相同动画。我试过这个:

// This is Swift 3 but a Swift 2 answer or even Objective-C is fine
button.sendActions(for: UIControlEvents.touchUpInside)

该命令执行按钮上的单击,但不执行按下焦点按钮的动画。

您可以为 UIButton 设置动画。您也应该使用 .PrimaryActionTriggered 而不是 .touchUpInside。例如:

let animationDuration = 0.2
let focusedScaleFactor: CGFloat = 1.2
UIView.animateWithDuration(animationDuration, animations: {
    button.transform = CGAffineTransformMakeScale(focusedScaleFactor, focusedScaleFactor)
    }) { (finished) in
        UIView.animateWithDuration(animationDuration, animations: {
            button.transform = CGAffineTransformIdentity
        }) { (finished) in
            button.sendActions(for: .PrimaryActionTriggered)
        }
}