ios - Swift: making a UIPickerView go round as a loop -


"there's similar question answered, doesn't work on app"

is there way can make uipickerview round loop? i've got 0 9 on each column don't want end @ 9. have uibutton thats have func makes numbers skip digit , starts 000 999, if make uipickerview loop don't want make changes or effects on mathematics order here's have far:

class viewcontroller: uiviewcontroller, uipickerviewdatasource, uipickerviewdelegate {  @iboutlet weak var label: uilabel! @iboutlet weak var pickerview: uipickerview!  let numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]  func numberofcomponents(in pickerview: uipickerview) -> int {     return 3 }  func pickerview(_ pickerview: uipickerview, titleforrow row: int, forcomponent component: int) -> string? {     return numbers[row] }  func pickerview(_ pickerview: uipickerview, numberofrowsincomponent component: int) -> int {     return numbers.count }  func pickerview(_ pickerview: uipickerview, didselectrow row: int, incomponent component: int) {      let val1 = numbers[pickerview.selectedrow(incomponent: 0)]     let val2 = numbers[pickerview.selectedrow(incomponent: 1)]     let val3 = numbers[pickerview.selectedrow(incomponent: 2)]      label.text = "\(val1) \(val2) \(val3)" }  fileprivate func num(_ i: int) -> int {     return pickerview.selectedrow(incomponent: i) }  @ibaction func buttonpressed() {     let currentnum = num(0) * 100 + num(1) * 10 + num(2)     let nextnum = currentnum + 1      pickerview.selectrow(nextnum % 1000 / 100, incomponent: 0, animated: true)     pickerview.selectrow(nextnum % 100 / 10, incomponent: 1, animated: true)     pickerview.selectrow(nextnum % 10, incomponent: 2, animated: true)      changelabeltext() }  fileprivate func changelabeltext() {     label.text = "\(num(0)) \(num(1)) \(num(2))" } 

according other question thats been answered, have edit code like:

class viewcontroller: uiviewcontroller, uipickerviewdatasource, uipickerviewdelegate {  func valueforrow(row: int) -> int {     // rows repeat every `pickerviewdata.count` items     return pickerviewdata[row % pickerviewdata.count] }  func rowforvalue(value: int) -> int? {     if let valueindex = find(pickerviewdata, value) {         return pickerviewmiddle + value     }     return nil }  func pickerview(pickerview: uipickerview, titleforrow row: int, forcomponent component: int) -> string! {     return "\(valueforrow(row))" }  func numberofcomponentsinpickerview(pickerview: uipickerview) -> int {     return 3 }  func pickerview(pickerview: uipickerview, numberofrowsincomponent component: int) -> int {     return pickerviewrows }  // whenever picker view comes rest, we'll jump // row current value closest middle func pickerview(pickerview: uipickerview, didselectrow row: int, incomponent component: int) {     let newrow = pickerviewmiddle + (row % pickerviewdata.count)     pickerview.selectrow(newrow, incomponent: 0, animated: false) }    @iboutlet weak var label: uilabel! @iboutlet weak var pickerview: uipickerview!  let numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]  func numberofcomponents(in pickerview: uipickerview) -> int {     return 3 }  func pickerview(_ pickerview: uipickerview, titleforrow row: int, forcomponent component: int) -> string? {     return numbers[row] }  func pickerview(_ pickerview: uipickerview, numberofrowsincomponent component: int) -> int {     return numbers.count }  func pickerview(_ pickerview: uipickerview, didselectrow row: int, incomponent component: int) {      let val1 = numbers[pickerview.selectedrow(incomponent: 0)]     let val2 = numbers[pickerview.selectedrow(incomponent: 1)]     let val3 = numbers[pickerview.selectedrow(incomponent: 2)]      label.text = "\(val1) \(val2) \(val3)" }  fileprivate func num(_ i: int) -> int {     return pickerview.selectedrow(incomponent: i) }  @ibaction func buttonpressed() {     let currentnum = num(0) * 100 + num(1) * 10 + num(2)     let nextnum = currentnum + 1      pickerview.selectrow(nextnum % 1000 / 100, incomponent: 0, animated: true)     pickerview.selectrow(nextnum % 100 / 10, incomponent: 1, animated: true)     pickerview.selectrow(nextnum % 10, incomponent: 2, animated: true)      changelabeltext() }  fileprivate func changelabeltext() {     label.text = "\(num(0)) \(num(1)) \(num(2))" }   private let pickerviewdata = array(0...59)     // contents 0, 1, 2, 3...59, change whatever want private let pickerviewrows = 10_000            // big number private let pickerviewmiddle = ((pickerviewrows / pickerviewdata.count) / 2) * pickerviewdata.count       override func viewdidload() {     super.viewdidload()     self.picker.delegate = self     self.picker.datasource = self     let initialvalue = 0     if let row = rowforvalue(initialvalue) {         self.picker.selectrow(row, incomponent: 0, animated: false)     }     // or if want start in middle:     // self.picker.selectrow(pickerviewmiddle, incomponent: 0, animated: false) } 

but i'm getting 6 errors on these 6 different lines:

 if let valueindex = find(pickerviewdata, value) {         return pickerviewmiddle + value // return "\(valueforrow(row))" //  private let pickerviewmiddle = ((pickerviewrows / pickerviewdata.count) / 2) * pickerviewdata.count  // self.picker.delegate = self     self.picker.datasource = self     let initialvalue = 0     if let row = rowforvalue(initialvalue) {         self.picker.selectrow(row, incomponent: 0, animated: false) 

this not infinite loop should work pretty well. idea increase data source range multiple of whatever margin want add (i randomly set margin 40) , when scroll stops, resume middle of data source range. implementation work infinite loop unless user scroll (40 / 2 x 10) items without stopping scroll. added code show idea.

let loopingmargin: int = 40  var data: [string] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]  var picker: uipickerview  = uipickerview()  override func viewdidload() {     super.viewdidload()      picker.delegate = self     picker.datasource = self     view.addsubview(picker)     picker.selectrow((loopingmargin / 2) * data.count, incomponent: 0, animated: false) }   // mark: uipickerviewdelegate, uipickerviewdatasource func numberofcomponents(in pickerview: uipickerview) -> int {     return 1 }  func pickerview(_ pickerview: uipickerview, numberofrowsincomponent component: int) -> int {     return loopingmargin * data.count }  func pickerview(_ pickerview: uipickerview, titleforrow row: int, forcomponent component: int) -> string? {     return data[row % data.count] }  func pickerview(_ pickerview: uipickerview, didselectrow row: int, incomponent component: int) {     let currentindex = row % data.count     picker.selectrow((loopingmargin / 2) * data.count + currentindex, incomponent: 0, animated: false) } 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -