使用tvOS以编程方式触发UIPress

时间:2016-06-20 18:52:46

标签: ios swift tvos

我试图以编程方式触发tvOS中的按钮上的UIPress,这样我就可以获得用户点击按钮时收到的相同动画。我试过这个:

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

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

1 个答案:

答案 0 :(得分:2)

您可以为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)
        }
}