我有视图 - 卡片,就像在Tinder中一样,我想通过$ mapfile -t my_array < <( echo "* one two"; echo "[three four]")
$ declare -p my_array
declare -a my_array='([0]="* one two" [1]="[three four]")'
将图像添加到这些卡片中。我做到了,一切正常,但在这里我遇到了一些问题:
例如,我有3张牌和一个数组addSubview
。当我刷卡时,我会增加images
。所以,当我刷卡时,我打印cardIndex并正确显示日志。图像名称也可以正确打印,但图像在我的卡上不会改变。
如何在显示新图像之前更新我的卡片视图?
我试图在显示图像后将imageView设置为nil,但它也不起作用:
cardIndex++
这是我的功能:
_ = UIImageView(image: nil)
更新
var images = ["apple.jpg", "orange.jpg", "banana.jpg"]
swipeableView.nextView = {
let cardView = CardView(frame: self.swipeableView.bounds)
cardView.backgroundColor = UIColor.whiteColor()
if self.loadCardsFromXib {
let contentView = NSBundle.mainBundle().loadNibNamed("CardContentView", owner: self, options: nil).first! as! UIView
contentView.translatesAutoresizingMaskIntoConstraints = false
contentView.backgroundColor = cardView.backgroundColor
let cardHeight = self.swipeableView.frame.height
let cardWidth = self.swipeableView.frame.width
// Mark: Adding Image to cards
let image = UIImage(named: self.images[self.cardIndex])
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 0, y: 0, width: cardWidth, height: cardHeight - 70)
contentView.addSubview(imageView)
cardView.addSubview(contentView)
_ = UIImageView(image: nil)
}
return cardView
}
答案 0 :(得分:1)
您应该在cardIndex
关闭中检查并增加swipeableView.nextView
:
var images = ["apple.jpg", "orange.jpg", "banana.jpg"]
self.cardIndex = 0
swipeableView.nextView = {
if self.cardIndex >= images.count {
return nil
}
let cardView = CardView(frame: self.swipeableView.bounds)
cardView.backgroundColor = UIColor.whiteColor()
if self.loadCardsFromXib {
let contentView = NSBundle.mainBundle().loadNibNamed("CardContentView", owner: self, options: nil).first! as! UIView
contentView.translatesAutoresizingMaskIntoConstraints = false
contentView.backgroundColor = cardView.backgroundColor
let cardHeight = self.swipeableView.frame.height
let cardWidth = self.swipeableView.frame.width
// Mark: Adding Image to cards
let image = UIImage(named: self.images[self.cardIndex])
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 0, y: 0, width: cardWidth, height: cardHeight - 70)
contentView.addSubview(imageView)
cardView.addSubview(contentView)
}
self.cardIndex++
return cardView
}
要获取当前显示卡片索引,您应该定义其他currentCardIndex
:
let currentCardIndex = 0
并设置swipeableView.didSwipe
:
swipeableView.didSwipe = {
_, _, _ in
self.currentCardIndex++
}
答案 1 :(得分:0)
您需要保留对UIImageView
的引用(通过制作班级/全局变量)并在滑动时设置新图像。像这样:
var imageView : UIImageView // Declare in global context - outside all class functions
然后从内部函数访问它(第一次和图像索引中的任何更改):
imageView = UIImageView(image: image!) // Do not re-define like let imageView