iOS 幸运大转盘

2021-04-12  本文已影响0人  秋叶红90

SanJiaoView 类

import UIKit

class SanJiaoView: UIView {
    
    var angle:CGFloat = 0
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initCommon()
    }
    
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        self.initCommon()
    }
    
    func initCommon() {
        self.addSubview(self.contentView)
        
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        var rect = self.bounds
        rect.size.height = rect.size.height/2
        self.contentView.frame = rect
    }
    
    override func draw(_ rect: CGRect) {
        print("rect  \(rect)")
        self.test(rect)
//        let w = rect.size.width/2
//        let h = rect.size.height
//
//        let jd = asin(w/h)
//        let origin = CGPoint.init(x: w, y: h)
//        let radius:CGFloat = 100;
//        let startAngle = -jd - CGFloat.pi/2;
//        let endAngle =  jd - CGFloat.pi/2;
//        let sectorPath = UIBezierPath.init(arcCenter: origin, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
//        sectorPath.addLine(to: origin)
//        UIColor.darkGray.set()
//        sectorPath.fill()
    }

    func test(_ rect: CGRect) {
        
        let w = rect.size.width/2
        let h = rect.size.height/2
        let jd = -self.angle/2 - CGFloat.pi/2
        let jd1 = self.angle/2 - CGFloat.pi/2
        let context = UIGraphicsGetCurrentContext()
        let p2 = CGPoint.init(x: w, y: h)

        context?.addArc(center: p2, radius: h, startAngle: jd, endAngle: jd1, clockwise: false)
        context?.addLine(to: p2)
        let red = CGFloat(arc4random()%255)/255.0
        let green = CGFloat(arc4random()%255)/255.0
        let blue = CGFloat(arc4random()%255)/255.0
        let color = UIColor.init(red: red, green: green, blue: blue, alpha: 1)
        context?.setFillColor(color.cgColor)
        context?.fillPath()
         
        context?.closePath()
        
    }
    
}


import UIKit

class LiveRoundTurntableView: UIView, CAAnimationDelegate {
    var isAnimation:Bool = false
    
    
    var prizeArray:[String] = ["谢谢参与","一等奖","谢谢参与","二等奖","谢谢参与","三等奖","谢谢参与","特等奖","特等奖","特等奖"]
    var listViews:[SanJiaoView] = []
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.loadData(list: ["谢谢参与","一等奖","谢谢参与","二等奖","谢谢参与","三等奖","谢谢参与","特等奖","特等奖","特等奖"])
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        self.loadData(list: ["谢谢参与","一等奖","谢谢参与","二等奖","谢谢参与","三等奖","谢谢参与","特等奖","特等奖","特等奖"])
    }
    
    func loadData(list:[String]) {
        self.prizeArray = list
        if self.listViews.isEmpty == false {
            for item in self.listViews {
                item.removeFromSuperview()
            }
            self.listViews.removeAll()
        }
        for item in self.prizeArray {
            let itemV = SanJiaoView.init()
            itemV.backgroundColor = UIColor.clear
            self.addSubview(itemV)
            self.listViews.append(itemV)
        }
        self.setNeedsDisplay()
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let radius = self.bounds.size.height/2
        let count = self.prizeArray.count
        if count == 0 {
            return
        }
        
        let arc:CGFloat =  CGFloat.pi*2/CGFloat(count)
        var sinZ = sin(arc/2.0)
        if sinZ > 1 {
            sinZ = 1
        }
        var itemW = sinZ * radius * 2
        
        let itemH = radius*2
        if count <= 1 {
            itemW = itemH
        }
        
        for (index,item) in self.listViews.enumerated() {
            let angle = arc * CGFloat(index);
            
            item.angle = arc
            item.frame = CGRect.init(x: self.bounds.size.width/2 - itemW/2, y: 0, width: itemW, height: itemH)
            item.transform = CGAffineTransform.init(rotationAngle: angle)
            item.setNeedsDisplay()
        }
    }
    
    
    func startAnimation(endIndex:Int) {
        let allCount = self.prizeArray.count
        if allCount == 0 {
            return
        }
        if allCount < endIndex  {
            return
        }
        
        let unitAnglePi = CGFloat.pi / 180.0
        
        //        每个的角度
        let angleUnitItem:CGFloat = 360.0 / CGFloat(allCount)
        let offsetAngle:CGFloat = angleUnitItem/4.0
        let randomAngle:CGFloat = CGFloat((arc4random()%UInt32(angleUnitItem - offsetAngle))) - angleUnitItem/2.0
        
        let endAngle = CGFloat(allCount - endIndex) * angleUnitItem + randomAngle
        
        
        let circleNum = 4
        
        let rotationAnimation = CABasicAnimation.init(keyPath: "transform.rotation")
        
        let angle:CGFloat =  (CGFloat(circleNum) * 360.0 + endAngle) * unitAnglePi

        rotationAnimation.toValue = NSNumber.init(value: Float(angle))
        rotationAnimation.duration = 3
        rotationAnimation.isCumulative = false
        rotationAnimation.delegate = self
        rotationAnimation.timingFunction = CAMediaTimingFunction.init(name: CAMediaTimingFunctionName.easeOut)
        rotationAnimation.fillMode = .forwards
        rotationAnimation.isRemovedOnCompletion = false
        self.layer.add(rotationAnimation, forKey: "rotationAnimation")
    }

    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        
    }
}


使用

class ViewController: UIViewController {
    var listArr:[String] = [String]()
    @IBOutlet weak var ranView: LiveRoundTurntableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    @IBAction func zhuanCLick(_ sender: Any) {
        ranView.startAnimation(endIndex: 5)
    }
    
    @IBAction func ddd(_ sender: Any) {
        
        if listArr.count == 10 {
            listArr.removeAll()
            
        }
        listArr.append("1")
        ranView.loadData(list: listArr)
    }
    
}
上一篇下一篇

猜你喜欢

热点阅读