图片幻灯片Swift ios

问题描述:

我是新来的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

这是很多代码,我不太确定这是最好的方法。你尝试过使用Collection View Controller吗?它几乎就像一个Table视图,其中你将为每个单元格分配一个来自数组的图像。 Ps我也是iOS新手,但我100%确定你应该在这种情况下使用集合视图对象