如何调整 iOS 选择器轮以生成随机索引
Posted
技术标签:
【中文标题】如何调整 iOS 选择器轮以生成随机索引【英文标题】:How to adjust iOS picker wheel to generated random Index 【发布时间】:2016-11-28 11:29:16 【问题描述】:使用 XCode UI 测试有什么方法可以调整 ios 选择轮以生成索引值或获取索引值的文本并调整选择轮。我能够使用以下代码获取索引值。但无法获取索引值的值。
let picker = app.pickerWheels.element(boundBy: 1).otherElements.count
print(picker)
let randomNumber = arc4random_uniform((UInt32(picker)))
print(randomNumber)
使用选择轮的计数,我从中生成一个随机值。使用 randomNumber 的索引号,我试图获取该值。 有什么方法可以获取索引的值并将选择器轮调整为该值。
【问题讨论】:
【参考方案1】:这就是我在 XCTest UI 测试框架中处理选择器的方式。
我有一个对象 (PickerState
),它可以表示选择轮的状态,它使用选择轮的值填充,格式为 "Some value, 4 of 5"
。 getPickerState
方法将值 String 转换为 PickerState
对象。
generateRandomNewPickerPosition
生成一个随机索引供选取器移动到,确保不返回选取器的当前位置。
然后我使用 XCUIElement 上的扩展方法移动到 changePickerSelection
中随机生成的值。
/// Object to represent the state of a picker wheel.
class PickerState
let selectedValue: String
let currentPosition: UInt
let maximumPosition: UInt
init(selectedValue: String, currentPosition: UInt, maximumPosition: UInt)
self.selectedValue = selectedValue
self.currentPosition = currentPosition
self.maximumPosition = maximumPosition
/// Retrieve a PickerState object, given the value of a picker wheel.
func getPickerState(_ text: String)
// Separate value
let splitText = text.componentsSeparatedByString(",")
let selectedValue = splitText[0]
// Extract numbers
var numbers = splitText[1].componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet)
numbers = numbers.filter $0 != ""
let currentPosition = UInt(numbers[0])!
let maximumPosition = UInt(numbers[1])!
return PickerState(selectedValue: selectedValue, currentPosition: currentPosition, maximumPosition: maximumPosition)
/// Generate a new position (1-indexed) for the picker.
func generateRandomNewPickerPosition(pickerState: PickerState) -> UInt
let valueCount = pickerState.maximumPosition
let random = UInt(arc4random_uniform(UInt32(valueCount - 1)))
let newPosition = ((pickerState.currentPosition + random) % valueCount) + 1
return newPosition
/// Move up/down the picker options until the given `selectionPosition` is reached.
func changePickerSelection(pickerWheel: XCUIElement, selectionPosition: UInt)
// Select the new value
var valueSelected = false
while !valueSelected
// Get the picker wheel's current position
if let pickerValue = pickerWheel.value
let currentPosition = UInt(getPickerState(String(pickerValue)).currentPosition)
switch currentPosition.compared(to: selectionPosition)
case .GreaterThan:
pickerWheel.selectPreviousOption()
case .LessThan:
pickerWheel.selectNextOption()
case .Equal:
valueSelected = true
/// Extend XCUIElement to contain methods for moving to the next/previous value of a picker.
extension XCUIElement
/// Scrolls a picker wheel up by one option.
func selectNextOption()
let startCoord = self.coordinateWithNormalizedOffset(CGVector(dx: 0.5, dy: 0.5))
let endCoord = startCoord.coordinateWithOffset(CGVector(dx: 0.0, dy: 30.0))
endCoord.tap()
/// Scrolls a picker wheel down by one option.
func selectPreviousOption()
let startCoord = self.coordinateWithNormalizedOffset(CGVector(dx: 0.5, dy: 0.5))
let endCoord = startCoord.coordinateWithOffset(CGVector(dx: 0.0, dy: -30.0))
endCoord.tap()
let pickerWheel = app.pickerWheels.element(boundBy: 0)
let newPosition = generateRandomNewPickerPosition(getPickerState(pickerWheel.value))
changePickerSelection(pickerWheel, selectionPosition: newIndex)
【讨论】:
以上是关于如何调整 iOS 选择器轮以生成随机索引的主要内容,如果未能解决你的问题,请参考以下文章
scss 使用Bourbon Neat和选择器调整网格大小。需要http://bourbon.io和http://neat.bourbon.io