我正在构建一个游戏,其中包含 7 x 7 网格和 3 x 7 侧网格(7 x 7 的东北、南和西)的卡片。 我想使用 pangesture 滚动侧面的卡片,但现在在右侧添加卡片会导致 pangesture 在两侧工作,但不能在顶部或底部工作。 “arrayAllCards.append(button)”行使其不适用于南北。
`
for j in 0...6{
for i in 0...3 {
let image = UIImage(named: arrayShuffle[(i+1)*(j+1)].suitRank) as UIImage?
var cardPos: [Int] = []
cardPos.append(j)
cardPos.append(7+i)
var button = Card(suitRank: arrayShuffle[(i+1)*(j+1)].suitRank,
position: cardPos,
suit: arrayShuffle[(i+1)*(j+1)].suit,
rank: arrayShuffle[(i+1)*(j+1)].rank,
x: cardPos[0] * (Int(screensize)/7),
y: cardPos[1] * (Int(screensize)/7),
isChosen: false)
arrayShuffle.remove(at: (i+1)*(j+1))
let valueX = Int(CGFloat(0 + (i * sizeButton)))
let valueY = Int(CGFloat(middlepos - CGFloat(sizeButton * j)))
button.frame = CGRect(x: valueX,
y: valueY,
width: sizeButton,
height: sizeButton)
button.backgroundColor = .red
button.configuration?.imagePadding = 0
button.setImage(image, for: .normal)
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
arrayAllCards.append(button)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
let scrollGesture = UITapGestureRecognizer(target: self, action: #selector(swipeUp))
button.isUserInteractionEnabled = false
view.addSubview(button)
view.addGestureRecognizer(scrollGesture)
//
// l
}
}
//Here is the UIPangesture code
@objc func swipeUp(_ gesture: UIPanGestureRecognizer) {
var vertical: Bool = false
let moveDirection = gesture.translation(in: self.view)
if (abs(moveDirection.x) < abs(moveDirection.y)){
vertical = true
}
let result = UIPanGestureRecognizer.State.ended.rawValue
var screenRealEstate: Int = (Int) (self.screenheight - self.screensize) / 2
screenRealEstate -= (Int)(screensize) / 7 * 2
//var stop: Int = (Int)(screensize) / 7 * 3
var stoppage: Bool = false
for i in arrayAllCards {
let translation = gesture.translation(in: self.view)
//
// let value1 : Int = Int(arrayAllCards[6].center.y)
// let value: Int = self.origin - 3 * self.buttonSize
//
let value1 : Int = Int(arrayAllCards[90].center.y)
let value: Int = (self.origin + 7 * buttonSize) - 3 * self.buttonSize
let value2: Int = (self.origin + 13 * buttonSize)
let value3 : Int = Int(arrayAllCards[90].center.x)
let value4 : Int = 4 * self.buttonSize
let value5 : Int = 10 * self.buttonSize
if(value3 > value4 && value3 < value5 && !vertical){
i.center.x = translation.x/10 + i.center.x
}
if(value1 > value && value1 < value2 && vertical){
i.center.y = translation.y/10 + i.center.y // Customize this.
}
if(gesture.state.rawValue == 3){
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
for k in 0..<self.arrayAllCards.count
{
self.arrayAllCards[k].center.x = CGFloat(self.arrayAllCards[k].position[1]) * CGFloat(self.buttonSize) + CGFloat(self.buttonSize/2)
self.arrayAllCards[k].center.y = CGFloat((Int)(self.origin)) + CGFloat(self.arrayAllCards[k].position[0] * self.buttonSize) + CGFloat(self.buttonSize/2)
}
}, completion: { finished in
}
)}
}
//
}
问题在于平移手势中的数组大小。
let value1 : Int = Int(arrayAllCards[133].center.y) was needed instead of arrayAllCards[90]. I was not accounting for the extra created cards.