2015-01-31 4 views
1

프레임과 같은 여러 가지 이미지로 디자인 된 애니메이션으로 앱을 만들려고합니다. UIImage.animationImages, UIImages.animationDuration 및 UIImage.animationRepeatCount를 사용합니다. 그러나 이것은 지정된 시간에 특정 이미지 목록이있는 애니메이션 만 표시하지만 이미지 목록의 다른 이미지와 마찬가지로 한 이미지에서 많은 초를 중지해야합니다. 예를 들어 안드로이드에서는 xml 설정 이미지와 각 이미지의 지속 시간을 설정하는 옵션이 있습니다. Swift에서 필요한 것은 이것입니다.신속한 애니메이션

답변

0

예를 들어 사용자 정의 UIImageView를 만들 수 있습니다.

class CustomImageView : UIImageView 
{ 
    var animationDurations : [Float] = [] 
    private var isAnimating = false 
    private var animationCount = 0 
    override func startAnimating() { 
     if let images = animationImages 
     { 
      if images.count == countElements(animationDurations) 
      { 
       animationCount = 0 
       isAnimating = true 
       self.changeImages(0) 
      } 
     } 
    } 

    override func stopAnimating() { 
     isAnimating = false 
    } 

    func changeImages(currentIndex : Int) 
    { 
     if currentIndex == 0 { 
      animationCount += 1 
     } 
     if animationCount > self.animationRepeatCount && self.animationRepeatCount > 0 
     { 
      isAnimating = false 
     } 
     if !isAnimating{ 
      return 
     } 
     if let images = animationImages 
     { 
      let nextIndex = (currentIndex + 1) % images.count 
      self.image = images[currentIndex] as? UIImage 

      let floatDelay = animationDurations[currentIndex] * Float(NSEC_PER_SEC) 
      let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(floatDelay)) 

      dispatch_after(delay, dispatch_get_main_queue(), {() -> Void in 
       self.changeImages(nextIndex) 
      }) 
     } 
    } 

} 

그것은 내가이 재사용 할 수있는 외부 클래스를 만드는 시도,하지만 난 시뮬레이터에서 실행하려고 할 때, 그것은 오류를 반환

class ViewController: UIViewController { 

    var imageView : CustomImageView! 

    override func viewDidLoad() { 

     imageView = CustomImageView(frame: CGRectMake(100, 100, 60, 60)) 
     self.view.addSubview(imageView) 
     imageView.animationRepeatCount = 2 

     let images : [UIImage] = [UIImage(named: "1.png")!, 
            UIImage(named: "2.png")!, 
            UIImage(named: "4.png")!] 
     let imageDuration : [Float] = [1.0,2.0,0.6] 

     imageView.animationImages = images 
     imageView.animationDurations = imageDuration 
     imageView.startAnimating() 

    } 
} 
+0

처럼 사용할 수 있습니다 : "스레드 1 : EXC_BAD_ACCESS (코드 = 2, ...) "행 : imageView.animationDurations = imageDuration – PlugInBoy

+0

인터페이스 빌더를 사용하여 IBoutlet으로 추가 하시겠습니까? – rakeshbs

+0

예, 귀하의 코드와 나의 차이점은 내 UIView가 스토리 보드의 IBOutlet이고 하드 코드되지 않은 것입니다. – PlugInBoy