我的项目上有一个图像滑块。当我点击它时,我想获取图像的ID。到目前为止,我已经做到了。
为了获取ID,我创建了一个操作按钮-
@IBAction func onTapGetBannerId(_ sender: Any) {
// var bannersArray = [BannersHome]() //banner array contain image and it's id
for items in bannersArray {
print(items.banner_id)
}
// print(bannersArray[0].banner_id)
}
在图像上点击我会得到所有ID。不是我点击的那张图片的特定ID,我知道为什么要得到这个,但是我想要我点击的那张图片的特定ID-
这是我剩下的代码-
func setBannerImages(_ bannerArray: [BannersHome]) {
totalFirstCount = bannerArray.count
if totalFirstCount == 1 {
// bannerImage.yy_imageURL = URL(string: bannerArray[0].banner_image)
bannerImage.kf.setImage(with: URL(string: bannerArray[0].banner_image))
print("banner id------------\(bannerArray[0].banner_id)")
}
else if !isFirstStarted && (totalFirstCount > 1) {
isFirstStarted = true
totalFirstCount = 0
self.perform(#selector(self.setImage), with: bannerArray, afterDelay: 0.3)
}
}
func setImage(_ thumbArray: [BannersHome]) {
if currentFirst == thumbArray.count {
currentFirst = 0
}
let transition = CATransition()
transition.duration = 0.4
transition.type = kCATransitionFade
bannerImage.layer.add(transition, forKey: kCATransition)
bannerImage.kf.setImage(with: URL(string: thumbArray[currentFirst].banner_image))
// bannerImage.yy_imageURL = URL(string: thumbArray[currentFirst].banner_image)
currentFirst += 1
self.perform(#selector(self.setImage), with: thumbArray, afterDelay: 2.0)
}
我的模特-
class BannersHome: NSObject {
var banner_image:String!
var banner_id:String!
init(BannersHome: NSDictionary) {
self.banner_image = BannersHome["banner_image"] as? String
self.banner_id = String(BannersHome["banner_id"] as! NSInteger)
}
}
我想获取我点击的图像的ID。我怎么能做到这一点?谢谢。
答案 0 :(得分:0)
您可以使用视图的tag
属性
设置
的标签 bannerImage.tag = currentFirst
然后在onTapGetBannerId
中获取发件人的标签
let imageView = sender as! UIImageView
// Do what you have to with the index
let index = imageView.tag