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

randomise 배열 .dropFirst() 또는 .dropLast()를 사용 하시겠습니까? – koropok

+0

비 반복 배열을 검색하셨습니까? 왜 당신은 질문 제목으로 UIImage로 좁혀 가고 있습니까? 주위 모든 선택의 여지가 없습니다. – dfd

답변

2

배열을 임의로 플러시 한 다음 "정렬 된"이미지를 하나씩 가져옵니다.

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

당신은 하나가 아직 표시되지 않은 이미지와 이미 코드

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!) 
} 

해피 코딩의 변화 다음

만들기를 표시 하나를 포함 2 개 배열을 관리하여 고유의 난수를 생성 할 수 있습니다 :)

관련 문제