2015-02-24 222 views
1

我是新來的ios開發。我正在嘗試製作一個簡單的全屏圖像幻燈片放映。在左側滑動時,幻燈片應顯示下一張圖像,向右滑動幻燈片應顯示上一張圖像。圖片幻燈片Swift ios

但是,如果我快速連續刷卡,我會看到一個空白屏幕,就好像動畫沒有跟上一樣,然後當我稍等片刻並再次滑動時,圖像視圖加速到位並再次正常工作。任何想法我做錯了什麼?什麼是最佳實踐,當涉及到實現這樣一個圖像旋轉木馬動態數量的圖像(這裏他們硬編碼)?

import UIKit 

var imageArr = ["imageOne.jpg", "imageTwo.jpg", "imageThree.jpg", "imageFour.jpg", "imageFive.jpg"] 
var imageIndex = 0; 

class ViewController: UIViewController { 

    var currImage = UIImageView() 
    var rightImage = UIImageView() 
    var leftImage = UIImageView() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     var bounds:CGRect = UIScreen.mainScreen().bounds 
     var width:CGFloat = bounds.size.width 
     var height:CGFloat = bounds.size.height 


     currImage.frame = CGRect(x: 0.0, y: 0.0, width: width, height: height) 
     currImage.image = UIImage(named: imageArr[imageIndex]) 

     rightImage.frame = CGRect(x: width, y: 0.0, width: width, height: height) 
     rightImage.image = UIImage(named: imageArr[imageIndex + 1]) 

     leftImage.frame = CGRect(x: -width, y: 0.0, width: width, height: height) 
     leftImage.image = UIImage(named: imageArr[imageArr.count - 1]) 

     self.view.addSubview(currImage) 
     self.view.addSubview(rightImage) 
     self.view.addSubview(leftImage) 

     var swipeLeft = UISwipeGestureRecognizer(target: self, action: "handleSwipe:") 
     swipeLeft.direction = UISwipeGestureRecognizerDirection.Left 
     self.view.addGestureRecognizer(swipeLeft) 

     var swipeRight = UISwipeGestureRecognizer(target: self, action: "handleSwipe:") 
     swipeRight.direction = UISwipeGestureRecognizerDirection.Right 
     self.view.addGestureRecognizer(swipeRight) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 

    } 

    let transitionManager = TransitionManager() 


    func handleSwipe(gesture: UIGestureRecognizer) { 

     var bounds:CGRect = UIScreen.mainScreen().bounds 
     var width:CGFloat = bounds.size.width 
     var height:CGFloat = bounds.size.height 

     if let swipeGesture = gesture as? UISwipeGestureRecognizer { 

      if (swipeGesture.direction == UISwipeGestureRecognizerDirection.Left) { 

       UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.2, options: UIViewAnimationOptions.CurveEaseIn, animations: { 


        self.currImage.frame = CGRect(x: -width, y: 0.0, width: width, height: height) 
        self.rightImage.frame = CGRect(x: 0.0, y:0.0, width: width, height: height) 

        }, completion: { finished in 

         if (!finished) { return } 

         imageIndex++ 
         imageIndex = imageIndex <= imageArr.count-1 ? imageIndex : 0 

         var leftIndex = imageIndex - 1 < 0 ? imageArr.count - 1 : imageIndex - 1 

         self.leftImage.image = UIImage(named: imageArr[leftIndex]) 
         self.leftImage.frame = CGRect(x: -width, y: 0.0, width: width, height: height) 

         var tempImg = self.currImage 

         self.currImage = self.rightImage 

         self.rightImage = tempImg 

         self.rightImage.frame = CGRect(x: width, y: 0.0, width: width, height: height) 

         var rightIndex = imageIndex + 1 > imageArr.count - 1 ? 0 : imageIndex + 1 
         self.rightImage.image = UIImage(named: imageArr[rightIndex]) 

       }) 
      } 

      if (swipeGesture.direction == UISwipeGestureRecognizerDirection.Right) { 


       UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.2, options: UIViewAnimationOptions.CurveEaseIn, animations: { 

        self.currImage.frame = CGRect(x: width, y: 0.0, width: width, height: height) 
        self.leftImage.frame = CGRect(x: 0.0, y: 0.0, width: width, height: height) 

        }, completion: { finished in 


         imageIndex-- 
         imageIndex = imageIndex < 0 ? imageArr.count - 1 : imageIndex 

         var rightIndex = imageIndex + 1 > imageArr.count - 1 ? 0 : imageIndex + 1 

         self.rightImage.image = UIImage(named: imageArr[rightIndex]) 
         self.rightImage.frame = CGRect(x: width, y: 0.0, width: width, height: height) 

         var tempImg = self.currImage 

         self.currImage = self.tempImg 
         self.leftImage = tempCurr 

         self.leftImage.frame = CGRect(x: -width, y: 0.0, width: width, height: height) 
         var leftIndex = imageIndex - 1 < 0 ? imageArr.count - 1 : imageIndex - 1 
         self.leftImage.image = UIImage(named: imageArr[leftIndex]) 

       }) 
      } 

     } 
    } 

} 

任何幫助,非常感謝!

+0

在這一行:self.leftImage = tempCurr什麼是tempCurr?有一個編譯錯誤。 – Andy 2015-07-01 00:23:26

+0

我想通了:'var tempImg = self.currImage self.currImage = self.leftImage self.leftImage = tempImg' – Andy 2015-07-01 23:38:17

回答

0

這是很多代碼,我不太確定這是最好的方法。你嘗試過使用Collection View Controller嗎?它幾乎就像一個Table視圖,其中你將爲每個單元格分配一個來自數組的圖像。 Ps我也是iOS新手,但我100%確定你應該在這種情況下使用集合視圖對象