我环顾四周,我认为没有办法做到这一点,但我正在寻找的是比较按钮图像的能力.我有一种情况,我希望在更改之前将按钮设置为某个图像.伪代码就像是
if (myCell.followButton.image == UIImage(named: "")) {
//do something here
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*per 29
由于kakubei而更新为iOS 8
UIButton有一个叫做的属性currentImage,所以用它来比较图像:
iOS 8+
if myCell.followButton.currentImage.isEqual(UIImage(named: "yourImageName")) {
//do something here
}
Run Code Online (Sandbox Code Playgroud)
IOS 7-
if (myCell.followButton.currentImage == UIImage(named: "yourImageName")) {
//do something here
}
Run Code Online (Sandbox Code Playgroud)
实现此功能的更好方法是跟踪按钮的选定状态并根据该状态更改其图像.如果您更改图像的名称,这将使其更灵活.
Matt Cooper的解决方案无法在iOS 8中运行,因为Apple已经改变了缓存图像的方式.你现在需要使用.isEqual
如:
// assuming both button and newButton are UIImage instances
if button.currentImage!.isEqual(newButton) {
...
Run Code Online (Sandbox Code Playgroud)
来自Apple Docs:
从iOS 8开始,您不能再依赖指针相等来比较缓存的UIImage对象,因为缓存机制可能不会返回相同的UIImage对象,而是会分别缓存图像数据.您必须使用isEqual:来正确测试相等性.
这样做的缺点是它不适用于iOS 7!我不知道哪种方式可以兼顾,但我希望有,我自己也在寻找:)
| 归档时间: |
|
| 查看次数: |
11350 次 |
| 最近记录: |