2016-04-01 22 views
0

Burada yeniyim ve aynı zamanda Swift'de yeni başlayan biriyim!Swing UIImageView dizi döngüsü

Sol ve sağa kaydırmak için bir UIImageView yaratmaya çalışıyorum, son görüntüye geçtiğimde uygulama çökmesine, ilk görüntüdeyken aynı çökme gerçekleşir ve sağa kaydırmaya çalışırım! Yapmak istediğim, Resimler dizim son resme ulaştığı zaman, bir döngü içindeki İlk resme geri gönderdiğinde, herkes bana yardımcı olabilir mi? Teşekkür peşin !!! İşte benim kodum;

 CATransaction.setAnimationDuration(animationDuration) 
     CATransaction.setCompletionBlock { 
      let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(self.switchingInterval * NSTimeInterval(NSEC_PER_SEC))) 
      dispatch_after(delay, dispatch_get_main_queue()) { 
      } 


     } 

     let transition = CATransition() 
     transition.type = kCATransitionFade 
     chracters.layer.addAnimation(transition, forKey: kCATransition) 

     CATransaction.commit() 


     if let swipeGesture = gesture as? UISwipeGestureRecognizer { 

      switch swipeGesture.direction { 

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

       // decrease index 

       imageIndex -= 1 


       // check if index is in range 

       if swipePosition > imageList.count-1{ 

       // Do Nothing 
       } else { 

        swipePosition += 1 
       } 
       chracters.image = UIImage(named: imageList[imageIndex]) 

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

       // increase index first 

       imageIndex += 1 

       // check if index is in range 

       if swipePosition == 0 { 

        //Do Nothing 
       } else { 

        swipePosition -= 1 
       } 

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



      default: 
       break; 
       //stops the code 



      } 

     } 


    } 

cevap

1

Önce koşul swipePosition kendisi yerine ImageIndex kullanabilirsiniz amiguous gibi görünüyor { CATransaction.begin():

fonk (UIGestureRecognizer jest) yürütmüş. İkincisi chracters.image, if/else bloğunun içinde olmalıdır. Aşağıda güncellenmiş snippet var. ImageIndex'i kullanmak isterseniz, buna göre değiştirebilirsiniz. Ayrıca son görüntüden ilk görüntüye ve ilk görüntüden sonuncuya geçmek için kodu ekledik.

if swipePosition > imageList.count-1{ 
    swipePosition = 0 
    chracters.image = UIImage(named:imageList[swipePosition] 
} else { 
    chracters.image = UIImage(named:imageList[swipePosition] 
    swipePosition += 1 
} 

case UISwipeGestureRecognizerDirection.Left: 
print("User swiped Left") 
// check if index is in range 
if swipePosition == 0 { 
    swipePosition = imageList.count-1 
    chracters.image = UIImage(named:imageList[swipePosition] 
} else { 
    chracters.image = UIImage(named:imageList[swipePosition] 
    swipePosition -= 1 
} 
+0

10 Teşekkür ederim, çalıştım :) – Matt

+0

Yardımlarıma sevindim. Eğer işe yaradıysa, bunu doğru cevap olarak işaretleyebilesiniz, böylece aynı sorunla karşılaşan diğer kişiler bundan faydalanabilir. –