2016-08-23 73 views
3

我已經編寫了代碼以在圖庫應用程序中滑動圖像。當它彈出到一個大的圖像時,我想左右滑動圖像並寫下以下代碼。但它仍然無法工作。是否有人建議代碼有什麼問題?如何在彈出圖像時左右滑動圖像

override func viewDidLoad() { 
     super.viewDidLoad() 

self.imageview.image = self.Image 


    var leftSwipe = UISwipeGestureRecognizer(target:self, action: Selector("handleSwipes:")) 
     var rightSwipe = UISwipeGestureRecognizer(target:self, action: Selector("handleSwipes:")) 


     leftSwipe.direction = .Left 
     rightSwipe.direction = .Right 

     view.addGestureRecognizer(leftSwipe) 
     view.addGestureRecognizer(rightSwipe) 
} 

func handleSwipes(sender: UISwipeGestureRecognizer) { 
     if (sender.direction == .Left){ 
      self.imageview.image = self.Image 
     } 

     if (sender.direction == .Right){ 
      self.imageview.image = self.Image 
     } 

    } 

回答

1

handleSwipes FUNC調用self.Image,它永遠不會更新,因爲它被分配(即self.Image一個圖像本身)在viewdidload.Just通過左,右輕掃不同的圖像只是通過不同的圖像在左右滑動。如果(sender.direction ==。左){ self.imageview.image = self.Image }

if (sender.direction == .Right){ 
     self.imageview.image = UIImage(named: "myImage.png"); 
    } 
+0

應該是什麼一旦它在viewDidLoad中被調用,函數SwipeGestureRecognizer的返回類型是什麼? – Venky

+0

編輯我的答案 – datha

1

假設你有UIImage秒的陣列稱爲imagesArray

var currentIndex = 0{ 
    didSet{ 
    self.imageview.image = imagesArray[currentIndex] 
    } 
} // instance variable 

func handleSwipes(sender: UISwipeGestureRecognizer) 
{ 
    if (sender.direction == .Left) 
    { 
     if currentIndex < imagesArray.count - 1 
     { 
      currentIndex++ 
     } 
    } 
    else 
    { 
     if currentIndex > 0 
     { 
      currentIndex-- 
     } 
    } 
}