简体   繁体   English

UIDynamicAnimator落球循环迅速3

[英]UIDynamicAnimator dropping balls loop in swift 3

I have UIDynamicAnimator and balls dropping from top to bottom but, sometimes dropping 3 ball, sometimes dropping 5 ... etc. 我有UIDynamicAnimator ,球从上到下掉落,但有时掉3个球,有时掉5个……等等。

But I added to = 50 in my for loop must be drop 50 ball 但是我在for循环中添加to = 50必须掉落50球

Where is mistake ? 错误在哪里?

My codes under below. 我的代码在下面。

class ViewController: UIViewController {


    var squareView: UIView!
    var numberlet: UILabel!
    var gravity: UIGravityBehavior!
    var animator: UIDynamicAnimator!
    var collision: UICollisionBehavior!
    var itemBehaviour: UIDynamicItemBehavior!

    override func viewDidLoad() {
        super.viewDidLoad()


        let from = 0
        let to = 50
        let by = 1
        for i in stride(from: from, to: to, by: by) {

            let number = randomInt(min: 12, max:99)
            let color = randomInt(min: 1, max:5)

            let seconds = 3 * i
            delayWithSeconds(Double(seconds)) {
                self.start( color : "\(color)", number: "\(number)")
            }

        }

           }
    func randomInt(min: Int, max:Int) -> Int {
        return min + Int(arc4random_uniform(UInt32(max - min + 1)))
    }


    override func loadView() {
        super.loadView()
        view.backgroundColor = UIColor.white

    }



    func start(color : String, number : String){

        squareView = UIView(frame: CGRect(x: view.frame.size.width/2 - 30, y: 0, width: 70, height: 70))
        squareView.layer.cornerRadius = 0
        squareView.backgroundColor = UIColor(patternImage: UIImage(named: "\(color).png")!)
        view.addSubview(squareView)

        numberlet = UILabel(frame:CGRect(x: squareView.frame.size.width/2 - 18 ,  y: 0 , width: 70, height: 70))
        numberlet.textColor = UIColor.black

        numberlet.font =  UIFont.init(name: "Helvetica-Bold", size: 30)
        numberlet.text = number
        squareView.addSubview(numberlet)


        animator = UIDynamicAnimator(referenceView: view)
        gravity = UIGravityBehavior(items: [squareView])
        animator.addBehavior(gravity)

        collision = UICollisionBehavior(items: [squareView])
        collision.translatesReferenceBoundsIntoBoundary = true
        //        collision.addBoundaryWithIdentifier("barrier", fromPoint: CGPointMake(self.view.frame.origin.x, 350), toPoint: CGPointMake(self.view.frame.origin.x + self.view.frame.width, 350))
        animator.addBehavior(collision)

        itemBehaviour = UIDynamicItemBehavior(items: [squareView])
        itemBehaviour.elasticity = 0.3
        animator.addBehavior(itemBehaviour)

        delayWithSeconds(2.9) {
            self.squareView.removeFromSuperview()
            self.numberlet.removeFromSuperview()
          }

    }


    func delayWithSeconds(_ seconds: Double, completion: @escaping () -> ()) {
        DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
            completion()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

I implemented your code. 我实现了您的代码。 The reason for your issue seems to lie here: 您问题的原因似乎在这里:

let seconds = 3 * i
delayWithSeconds(Double(seconds))             
self.start( color : "\(color)", number: "\(number)")

Where you declare 3 * i you are incrementing the time to drop the next one so your seconds to drop become 0-3 seconds, 0-6 seconds, 0-9 seconds, 0-12 seconds, and so on. 在声明3 * i的位置,您要增加丢弃下一个的时间,因此要丢弃的seconds变为0-3秒,0-6秒,0-9秒,0-12秒,依此类推。 So it's not that you're not dropping new squares it's that the time it takes to drop the next one is increasing by 3 seconds every time. 因此,并不是说您没有丢下新方块,而是每次删除下一个方块所花费的时间增加了3秒。

If that's the intended behaviour then I am unsure what your issue is. 如果这是预期的行为,那么我不确定您的问题是什么。 Your code is working on my device. 您的代码正在我的设备上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM