I want to spin all UIPickerView
Component like lottery wheel using timer in swift. I have a 3 component in UIPickerView
want to spin all the components same on same time. I am suing below code to spin the component in UIPickerView
:
let timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(scrollRandomly), userInfo: nil, repeats: true);
@objc func scrollRandomly() {
let row:Int = Int(arc4random() % 9);
let row1:Int = Int(arc4random() % 9);
let row2:Int = Int(arc4random() % 9);
pickerView.selectRow(row, inComponent: 0, animated: true)
pickerView.selectRow(row1, inComponent: 1, animated: true)
pickerView.selectRow(row2, inComponent: 2, animated: true)
}
Thanks, Rushabh
I use this extension
self.view.beginRotating()
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
self.view.stopRotating()
}
_
fileprivate var RotatingObjectHandle: UInt8 = 0
fileprivate var RotationCycleHandler: UInt8 = 0
extension UIView {
private var shouldStopRotating: Bool {
get {
return objc_getAssociatedObject(self, &RotationCycleHandler) as? Bool ?? false
}
set {
objc_setAssociatedObject(self, &RotationCycleHandler, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
private(set) var isRotating: Bool {
get {
return objc_getAssociatedObject(self, &RotatingObjectHandle) as? Bool ?? false
}
set {
objc_setAssociatedObject(self, &RotatingObjectHandle, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
func beginRotating() {
guard !self.isRotating else { return }
self.isRotating = true
self.shouldStopRotating = false
self.rotate()
}
private func rotate() {
UIView.animate(withDuration: 0.65, delay: 0, options: .curveLinear, animations: {
self.transform = self.transform.rotated(by: CGFloat(Double.pi))
}, completion: { _ in
if !self.shouldStopRotating {
self.rotate()
} else {
self.isRotating = false
self.shouldStopRotating = false
}
})
}
func stopRotating() {
self.shouldStopRotating = true
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.