I am building a game with cards in a 7 x 7 grid and 3 x 7 side grids (north east south and west of 7 x 7. I want to use the pangesture to scroll in the cards that are to the side but right now the addition of cards on the right side causes the pangesture to work on both sides but not top or bottom. The line "arrayAllCards.append(button)" makes it not work for north and south.
`
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
}
)}
}
//
}
The issue lay with the array size in the pan gesture.
was needed instead of
arrayAllCards[90]. I was not accounting for the extra created cards.