ios - 如何保持按钮被选中?

标签 ios iphone swift uibutton avplayer

我正在尝试使用 AVPlayer 创建循环按钮。当用户点击一个按钮(循环)时,我希望它保持选中状态,直到用户再次点击它以取消选择它。我有以下代码,但它似乎不起作用。谢谢!

@IBAction func didTapLoopButton(_ sender: Any) {
    guard let loopButton = sender as? UIButton else {
        return
    }

    if loopButton.state == .selected {
        print("selected")


    } else {
        print("deselected")
    }
}

最佳答案

试试这个:

@IBAction func didTapLoopButton(_ sender: Any) {
    guard let loopButton = sender as? UIButton else {
        return
    }

    let selected = !loopButton.isSelected

    if selected {
        print("selected")
    } else {
        print("deselected")
    }

    loopButton.isSelected = selected
}

关于ios - 如何保持按钮被选中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41904068/

相关文章:

objective-c - 复制图标Photos.app

ios - deselectRowAtIndexPath 什么都不做

ios - Stripe SCA 需要处理下一步操作

ios - <错误> : [CoreBluetooth] API MISUSE: Cancelling connection for unused peripheral

ios - 使用 UIImageView 调整 UIImage

HTML 在 iPhone (iOS) 上上传 : filename is always same (image. jpg, image.png, ...)

iphone - UIToolbar 作为 UINavigationbar 中的右键,包含 2 个按钮,它们之间没有间距

iphone - 在 Xcode 中删除/移除组

ios - 在 Swift 中创建导航面板

ios - 如何使用 Swift 在 iOS 中使用 AdMob 实现插页式广告?