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

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

[cell.customLocationButton addTarget:self action:@selector(customLocationButtonTap:) forControlEvents:UIControlEventTouchUpInside]; 

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

有同样的问题。 我可以使用setTitleColor:forState:来改变突出显示颜色的按钮,但它不像其他系统按钮那样从高亮显示恢复到正常状态。

事实certificate这是因为我的按钮是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) 

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

在此处输入图像描述

你可以做的另一件事是:

迅速:

 button.showsTouchWhenHighlighted = true button.setTitleColor(UIColor.blueColor(), forState: .Normal) button.setTitleColor(UIColor.lightGrayColor(), forState: .Selected) 

我一直在网上搜索,最后我从StackOverFlow的post中找到了一个解决方案。

对于Swift 4你可以添加这个UIButton扩展,它会正常工作。

 extension UIButton { override open func touchesBegan(_ touches: Set, with event: UIEvent?) { isHighlighted = true super.touchesBegan(touches, with: event) } override open func touchesEnded(_ touches: Set, with event: UIEvent?) { isHighlighted = false super.touchesEnded(touches, with: event) } override open func touchesCancelled(_ touches: Set, with event: UIEvent?) { isHighlighted = false super.touchesCancelled(touches, with: event) } }