WWFortuneWheelView

main

A scroll wheel that can be customized.
William-Weng/WWFortuneWheelView

WWFortuneWheelView

Swift-5.6 iOS-14.0 Swift Package Manager-SUCCESS LICENSE

  • A scroll wheel that can be customized.
  • 一個可以自訂數量的滾輪.

dependencies: [
    .package(url: "https://github.com/William-Weng/WWFortuneWheelView.git", .upToNextMajor(from: "1.0.0"))
]

Function - 可用函式

函式 功能
rotate(toNextIndex:) 轉動至特定的Index (直接轉角度) / 旋轉的時候會沒反應 => Button的Tag (不要去改它)

WWFortuneWheelViewDelegate

函式 功能
willRotate(_:unitAngle:startAngle:) 將要轉動
rotating(_:from:to:angle:) 正在轉動
autoRotated(_:from:to:angle:duration) 自動轉動 (手放開時)
didRotated(_:at:angle:) 轉動完成 (停止)

Example

import UIKit
import WWFortuneWheelView

final class ViewController: UIViewController {
    
    @IBOutlet weak var myFortuneWheelView: WWFortuneWheelView!
    @IBOutlet weak var indexLabel: UILabel!
    @IBOutlet weak var angleLabel: UILabel!
    
    private let decimalCount: UInt = 3
    
    private var index = 0
    private var angle: (unit: CGFloat, start: CGFloat, end: CGFloat) = (0, 0, 0)
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        initSetting()
    }
    
    @objc func didPressed(_ sender: UIButton) {
        myFortuneWheelView._rotate(toNextIndex: sender.tag)
    }
}

// MARK: - WWFortuneWheelViewDelegate
extension ViewController: WWFortuneWheelViewDelegate {
    
    func willRotate(_ wheelView: WWFortuneWheelView, unitAngle: CGFloat, startAngle: CGFloat) {
        angle.unit = unitAngle
        angle.start = startAngle
        angleLabel.text = "\(startAngle._decimalPoint(decimalCount))"
    }
    
    func rotating(_ wheelView: WWFortuneWheelView, from startIndex: Int, to endIndex: Int, angle: CGFloat) {
        indexLabel.text = "\(startIndex) to \(endIndex)"
        angleLabel.text = "\(angle._decimalPoint(decimalCount))"
    }
    
    func autoRotated(_ wheelView: WWFortuneWheelView, from startIndex: Int, to endIndex: Int, duration: TimeInterval) {
        indexLabel.text = "\(startIndex) to \(endIndex)"
    }
    
    func didRotated(_ wheelView: WWFortuneWheelView, at index: Int, angle: CGFloat) {
        
        indexLabel.text = "\(index)"
        angleLabel.text = "\(angle._decimalPoint(decimalCount))"

        self.angle.end = angle
        wheelView.bullseyeButtons._isSelected(at: index)
    }
}

// MARK: - 小工具
private extension ViewController {
    
    /// 初始的設定
    func initSetting() {
        
        myFortuneWheelView.myDelegate = self
        myFortuneWheelView.rotateRange = 120...180
        myFortuneWheelView.bullseyeButtons.forEach { button in
            
            let stateImages: [(image: UIImage, state: UIControl.State)] = [
                (image: #imageLiteral(resourceName: "LightOff"), state: .normal),
                (image: #imageLiteral(resourceName: "LightHighlight"), state: .selected),
                (image: #imageLiteral(resourceName: "LightOn"), state: .highlighted),
            ]

            stateImages.forEach { (image, state) in
                button.setImage(image, for: state)
                button.backgroundColor = .black.withAlphaComponent(0.3)
            }
            
            button.setTitle(nil, for: .normal)
            button.addTarget(self, action: #selector(didPressed(_:)), for: .touchUpInside)
        }
    }
}

Description

  • Swift Tools 5.6.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Oct 18 2024 16:37:39 GMT-0900 (Hawaii-Aleutian Daylight Time)