UIButton默认点击UITableViewController自定义单元格中缺少的淡入淡出动画

who*_*cow 9 iphone objective-c uibutton uitableview ios

我有一个自定义单元格,里面有几个UIButton.我在按钮上创建了类似的目标操作:

[cell.customLocationButton addTarget:self action:@selector(customLocationButtonTap:) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)

尽管我没有在UIButtons上看到默认的iOS淡入淡出动画,但这些动作效果很好?是否有我需要启用的东西 - 我认为它们是使用IB的UIButton时的标准?

ksm*_*ksm 9

有同样的问题。我可以使用按钮更改突出显示上的颜色setTitleColor:forState:,但是它没有像其他系统按钮一样从突出显示淡入正常状态。

原来是因为我的按钮是type UIButtonTypeCustom。切换UIButtonTypeSystem为我解决。请注意,我正在iOS9上运行它。

这是一个假设为或子类的代码段(Swift):selfUIView

let button = UIButton(type: .System)
button.setTitle("Title", forState: .Normal)
button.sizeToFit() // Sizes the button frame based on the title.
addSubview(button)
Run Code Online (Sandbox Code Playgroud)


小智 6

我一直在互联网上搜索,最后我从 StackOverFlow 上的帖子中找到了解决方案,请在此处查看

对于 Swift 4,您可以添加此 UIButton 扩展,它会正常工作。

extension UIButton {

    override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        isHighlighted = true
        super.touchesBegan(touches, with: event)
    }

    override open func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        isHighlighted = false
        super.touchesEnded(touches, with: event)
    }

    override open func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        isHighlighted = false
        super.touchesCancelled(touches, with: event)
    }

}
Run Code Online (Sandbox Code Playgroud)


Hus*_*bir 0

在您的 XIB 内部属性检查器中选择您的按钮,然后在内部按钮类型中您将获得以下选项,您可以相应地选择和更改按钮的外观和感觉:-

在此输入图像描述