【问题标题】:Spin UIPickerView Component using Timer使用 Timer 旋转 UIPickerView 组件
【发布时间】:2020-12-15 02:30:23
【问题描述】:

我想在 swift 中使用计时器旋转所有 UIPickerView 组件,如彩票轮。我在UIPickerView 中有一个 3 组件,想同时旋转所有组件。我正在使用下面的代码来旋转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)


}

谢谢, 拉沙布

【问题讨论】:

  • 那么...你有什么问题?
  • @AndreasOetjen 参考链接:github.com/MaorS/iOS-Proj-CasinoGame
  • 再一次,这不是一个问题。您面临什么问题?
  • @AndreasOetjen 我无法在 10 秒内旋转组件,我该如何以编程方式旋转组件

标签: ios swift iphone xcode swift3


【解决方案1】:

我用这个扩展

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多