2017-11-02 3 views
0

私のアプリプロジェクトでは、自分の画像をランダムに107個表示しています。私はすべての画像を配列に入れるforループを設定しています。私はその配列をとり、ランダムなインデックスを選択します。そのインデックスは画像と相関し、ユーザーが左にスワイプするとその画像が画面に表示されます。私の質問は、すべての(またはアプリケーションを閉じるまでの金額)がランダムに選択されるまで、私のコードが配列内で同じインデックスを繰り返さないようにすることです。ここに私のコードはランダムなUIImagesを生成しないでください

class ViewController: UIViewController { 

var pictureArray: [String] = [] 
@IBOutlet weak var quoteImage: UIImageView! 
override func viewDidLoad() { 
    super.viewDidLoad() 

    for i in 0...107{ 
     pictureArray.append("quote\(i).jpg") 
     print(pictureArray) 
    } 

    // Do any additional setup after loading the view, typically from a nib. 
    let swipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector (changeQuotes)) 
    swipeRecognizer.direction = UISwipeGestureRecognizerDirection.left 
    self.view.addGestureRecognizer(swipeRecognizer) 
} 

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

} 
override var prefersStatusBarHidden: Bool{ 
    return true 
} 


@objc func changeQuotes(){ 

    let numberOfImages: UInt32 = 107 
    let randomIndex = Int(arc4random_uniform(UInt32(pictureArray.count))) 
    let imageName = "\(pictureArray[randomIndex])" 
    print(imageName) 
    quoteImage.image = UIImage(named: imageName) 


    } 
} 

ありがとう!

+0

ランダムに配列を使用する.dropFirst()または.dropLast()? – koropok

+0

繰り返しのない配列を検索しましたか?なぜあなたは質問タイトルで「UIImage」まで絞り込んでいますか?すべての選択肢が悪い – dfd

答えて

2

ランダムに配列をフラッシュし、次に「ソート」されたイメージを1つずつ取得します。

extension Sequence { 
    func randomSorted() -> [Element] { 
     var result = Array(self) 
     guard result.count > 1 else { return result } 

     for (i, k) in zip(result.indices, stride(from: result.count, to: 1, by: -1)) { 
      let newIndex = Int(arc4random_uniform(UInt32(k))) 
      if newIndex == i { continue } 
      result.swapAt(i, newIndex) 
     } 
     return result 
    } 
} 
0

あなたは1つがまだ表示されていない画像や、すでにあなたのコード

var pictureArray: [String] = [] 
var selectedPictureArray: [String] = [] 

static let TOTAL_IMAGES = 108 

@IBOutlet weak var quoteImage: UIImageView! 
override func viewDidLoad() { 

    super.viewDidLoad() 
    self.fillPictureArray() 
    let swipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector (changeQuotes)) 
    swipeRecognizer.direction = UISwipeGestureRecognizerDirection.left 
    self.view.addGestureRecognizer(swipeRecognizer) 
} 

func fillPictureArray() { 

    self.selectedPictureArray.removeAll() 
    for i in 0..<ViewController.TOTAL_IMAGES { 
     pictureArray.append("quote\(i).jpg") 
    } 
    print(pictureArray) 
} 

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

} 
override var prefersStatusBarHidden: Bool{ 
    return true 
} 


func changeQuotes(){ 

    if self.pictureArray.count == 0 { 

     self.fillPictureArray() 
    } 

    var randomIndex = 0 
    while true { 

     randomIndex = Int(arc4random_uniform(UInt32(ViewController.TOTAL_IMAGES))) 
     if self.selectedPictureArray.contains("quote\(randomIndex).jpg") { 
      continue 
     }else { 
      break 
     } 
    } 
    let imageName = "quote\(randomIndex).jpg" 
    self.selectedPictureArray.append(imageName) 
    print(imageName) 
    quoteImage.image = UIImage(named: imageName) 
    let index = self.pictureArray.index(of: imageName) 
    self.pictureArray.remove(at: index!) 
} 

ハッピーコーディングの変更次

メイクを表示されている1つが含まれている2つの配列を管理することにより、独自の乱数を生成することができます:)

関連する問題