【发布时间】:2018-11-04 02:09:09
【问题描述】:
我正在尝试在应用程序中实现一个收藏按钮,这是我的尝试。这是我目前拥有的:
let favoriteButton: UIButton = {
let button = UIButton(type: .custom)
var emptyHeartImg = UIImage(named: "emptyheart")
var fullHeartImg = UIImage(named: "fullheart")
emptyHeartImg = emptyHeartImg?.maskWithColor(color: UIColor(r: 128, g: 171, b: 103))
fullHeartImg = fullHeartImg?.maskWithColor(color: UIColor(r: 128, g: 171, b: 103))
let imageView = UIImageView(image: emptyHeartImg, highlightedImage: fullHeartImg)
imageView.contentMode = .scaleAspectFill
button.setImage(imageView.image, for: .normal)
button.addTarget(self, action: #selector(handleFavorite), for: .touchUpInside)
return button
}()
@objc private func handleFavorite() {
if (favoriteButton.imageView?.isHighlighted)! == false {
favoriteButton.imageView?.isHighlighted = true
} else {
favoriteButton.imageView?.isHighlighted = false
}
}
目前这不会根据需要更改图像。实现收藏按钮的任何提示或替代方法?
【问题讨论】:
标签: ios swift uibutton uiimageview