Tôi có một loạt hình ảnh mà tôi muốn có thể vuốt về phía trước (trái) sang hình ảnh tiếp theo hoặc quay lại (phải) đến hình ảnh trước đó. Khi imageList chạm -1/ngoài phạm vi, ứng dụng gặp sự cố. Tôi đang gặp khó khăn trong việc tìm ra logic về cách giữ nó trong phạm vi.Vuốt qua lại qua hàng loạt hình ảnh Swift
Đây là mã của tôi:
var imageList:[String] = ["image1.jpg", "image2.jpg", "image3.jpg"]
let maxImages = 2
var imageIndex: NSInteger = 1
Các swipe cử chỉ là trong phương pháp của tôi viewDidLoad(), không chắc chắn nếu điều này là đúng nơi ...:
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 trong nâng cao!
Xin chào, đây là làm việc nhưng làm thế nào tôi có thể thêm kéo hoạt hình. – myatmins