Mam wiele obrazów, które chcę przesuwać w przód (w lewo) do następnego obrazu lub wracać (w prawo) do poprzedniego obrazu. Kiedy imageList osiąga -1/poza zasięgiem, aplikacja ulega awarii. Mam problem z ustaleniem logiki, jak utrzymać ją w zasięgu.Przesuwaj w tył i w przód poprzez tablicę obrazów Swift
Oto mój kod:
var imageList:[String] = ["image1.jpg", "image2.jpg", "image3.jpg"]
let maxImages = 2
var imageIndex: NSInteger = 1
bezstykowa gesty są w sposobie mojego viewDidLoad(), nie wiem, czy to jest właściwe miejsce ...:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var swipeRight = UISwipeGestureRecognizer(target: self, action: "swiped:") // put : at the end of method name
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)
var swipeLeft = UISwipeGestureRecognizer(target: self, action: "swiped:") // put : at the end of method name
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
self.view.addGestureRecognizer(swipeLeft)
image.image = UIImage(named:"image1.jpg")
}
func swiped(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right :
println("User swiped right")
/*No clue how to make it go back to the previous image and
when it hits the last image in the array, it goes back to
the first image.. */
case UISwipeGestureRecognizerDirection.Left:
println("User swiped Left")
if imageIndex > maxImages {
imageIndex = 0
}
image.image = UIImage(named: imageList[imageIndex])
imageIndex++
default:
break //stops the code/codes nothing.
}
}
}
Thanks a LOT postęp!
Witam, to działa, ale w jaki sposób mogę dodać przeciąganie animacji. – myatmins