2015-02-24 16 views
5

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!

Trả lời

11

Trước hết chỉ số hình ảnh của bạn nên được thiết lập để không từ các phần tử mảng bắt đầu từ con số không nhưng thats không phải là một nguồn gốc của vấn đề của bạn

var imageIndex: NSInteger = 0 

sau đó chức năng swiped của bạn nên được như sau

func swiped(gesture: UIGestureRecognizer) { 

if let swipeGesture = gesture as? UISwipeGestureRecognizer { 

    switch swipeGesture.direction { 

    case UISwipeGestureRecognizerDirection.Right : 
     println("User swiped right") 

     // decrease index first 

     imageIndex-- 

     // check if index is in range 

     if imageIndex < 0 { 

      imageIndex = maxImages 

     } 

     image.image = UIImage(named: imageList[imageIndex]) 

    case UISwipeGestureRecognizerDirection.Left: 
     println("User swiped Left") 

     // increase index first 

     imageIndex++ 

     // check if index is in range 

     if imageIndex > maxImages { 

      imageIndex = 0 

     } 

     image.image = UIImage(named: imageList[imageIndex]) 




    default: 
     break //stops the code/codes nothing. 


    } 

} 


} 
+3

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

2

Bạn có thể làm một cái gì đó như thế.

Trước tiên, bạn cần đặt bộ đếm vị trí cho biết bạn đang ở đâu vào lúc này. Chú ý: Bạn cần phải thiết lập các đầu đến 0 nếu bạn muốn bắt đầu từ phần tử đầu tiên, bởi vì mảng bắt đầu đếm từ 0:

var swipePosition = 0 

Sau đó, nếu bạn swipe về phía trước, bạn cần phải kiểm tra xem swipePosition hiện nay là lớn hơn số lượng hình ảnh. Để làm điều đó, bạn có thể sử dụng phương thức count của mảng. Nhưng bạn phải trừ 1 vì vị trí bắt đầu ở 0. Vì vậy, nếu nó đã ở vị trí cao nhất hoặc thấp nhất trong mảng của bạn, bạn không phải làm bất cứ điều gì. Nếu không, bạn thêm hoặc bớt một số các mục:

//swipe forward 
if swipePosition > imageList.count-1{ 
    //do nothing because it is already at the highest position 
}else{ 
    swipePosition += 1 
} 

image.image = UIImage(named: imageList[swipePosition]) 

//swipe backward 

if swipePosition == 0 { 
    //Do nothing because it is already at start. 
}else{ 
    swipePosition -= 1 
} 

image.image = UIImage(named: imageList[swipePosition]) 
1

'

@IBOutlet weak var img: UIImageView!  

var currentnImageIndex:NSInteger = 0 

let arrayOfImages = ["Home Filled-50","Bullish-50","Line Chart Filled-50","Stack of Photos Filled-50","News-50","Download From Ftp Filled-50","Administrator Male Filled-50","Trophy Filled-50","Page Overview Filled-50"] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    img.userInteractionEnabled = true//do not forget to right this line otherwise ...imageView's image will not move 


    let swipeRight = UISwipeGestureRecognizer(target: self, action: "swipedRight:") 
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right 
    img.addGestureRecognizer(swipeRight) 

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: "swipedRight:") 
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left 
    img.addGestureRecognizer(swipeLeft) 

    img.image = UIImage(named: arrayOfImages[currentnImageIndex]) 

    // Do any additional setup after loading the view. 
} 

func swipedRight(gesture : UIGestureRecognizer){ 

    if let swipeGesture = gesture as? UISwipeGestureRecognizer{ 
     switch swipeGesture.direction{ 
     case UISwipeGestureRecognizerDirection.Right: 
      print("User swiped right") 

      if currentnImageIndex < arrayOfImages.count - 1 { 
      ++currentnImageIndex 
      } 

      if currentnImageIndex < arrayOfImages.count { 
      img.image = UIImage(named: arrayOfImages[currentnImageIndex]) 
      } 
     case UISwipeGestureRecognizerDirection.Left: 
      print("User swiped left") 

      // --currentnImageIndex 

      if currentnImageIndex > 0{ 
       --currentnImageIndex 
      } 
      if currentnImageIndex >= 0{ 
       img.image = UIImage(named: arrayOfImages[currentnImageIndex]) 
      } 
      // if curretnImageIndex 

     default: 
      break 
     } 
    ` 
+0

Bạn nên giải thích mã của bạn làm gì để cải thiện câu trả lời của bạn. Vui lòng đọc http://stackoverflow.com/help/how-to-answer – David

+0

im mới để cộng đồng ngăn xếp ... cảm ơn đề xuất .. tôi sẽ đọc cách trả lời – Dhrumil

0
@IBOutlet weak var imageView: UIImageView! 
    var imageIndex:NSInteger = 0 
    var maximages = 3 
    var imageList: [String] = ["burger", "burger2", "burger3", "burger4"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped)) // put : at the end of method name 
     swipeRight.direction = UISwipeGestureRecognizerDirection.right 
     self.view.addGestureRecognizer(swipeRight) 

     let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped)) // put : at the end of method name 
     swipeLeft.direction = UISwipeGestureRecognizerDirection.left 
     self.view.addGestureRecognizer(swipeLeft) 

     imageView.image = UIImage(named:"burger") 
    } 

    func swiped(gesture: UIGestureRecognizer) { 

     if let swipeGesture = gesture as? UISwipeGestureRecognizer { 

      switch swipeGesture.direction { 

       case UISwipeGestureRecognizerDirection.right : 
        print("User swiped right") 

        // decrease index first 

        imageIndex -= 1 

        // check if index is in range 

        if imageIndex < 0 { 

         imageIndex = maximages 
        } 

        imageView.image = UIImage(named: imageList[imageIndex]) 

       case UISwipeGestureRecognizerDirection.left: 
        print("User swiped Left") 

        // increase index first 

        imageIndex += 1 

        // check if index is in range 

        if imageIndex > maximages { 

         imageIndex = 0 
        } 

        imageView.image = UIImage(named: imageList[imageIndex]) 

       default: 
        break //stops the code/codes nothing. 
       } 
      } 
     } 
} 
+0

Bạn có thể cho tôi biết cách tôi có thể thêm kiểm soát trang không Với cái này. –