ios – Replicating Background Habits From Brawl Stars

[ad_1]

So an attention-grabbing downside/query, however I have been attempting to copy this background impact right here (brawl stars), with the icons filling the background and transferring in chosen path with a particular velocity off the display.

I have been in a position to initialize the background tiles/icons to instantly present up when the view is loaded, nonetheless I’m having hassle timing new tiles/icons to come back in on the proper time, and on the proper velocity to look the identical. I additionally do not know if that is the very best method and this simply represents my feeble try at messing round and attempting to copy this conduct.

Any recommendation or solutions welcome.

func initializeBackgroundTiles()
{
    let itemsPerRow = 4

    let itemHeight = self.body.width / 7
    let widthBetween = (itemHeight * 2) / 5
    let columnHeight = widthBetween * 4
    
    for column in 0...10 {
        
        for row in 0...5 {
            
            let columnPosition = (columnHeight * CGFloat(column))
            
            let xCalc = CGFloat(
                -(itemHeight + widthBetween) // one row to the left as properly
                + (itemHeight * Double(row) + (widthBetween * Double(row))))
            
            let yCalc = (((self.body.peak - itemHeight) - columnPosition * 2) +
                         ((row % 2 == 1 ? columnHeight : 0)))
                    
            let tileSubview = CustomImageView(body: CGRect(
                x: xCalc,
                y: yCalc,
                width: itemHeight,
                peak: itemHeight))
            
            tileSubview.picture = UIImage(named: "lightBand")!.withTintColor(.white)
            tileSubview.alpha = 0.04
            ContentView.addSubview(tileSubview)
            
            UIView.animate(withDuration: 30, delay: 0, choices: [.curveLinear]) {
                
                let newX = ((itemHeight * Double(row)) + (widthBetween * Double(row))) + widthBetween * 3
                let newY = 0 - ((itemHeight + (row % 2 == 0 ? columnHeight : 0)) + columnPosition * 2)
                
                tileSubview.body = CGRect(
                    x: newX,
                    y: newY,
                    width: itemHeight,
                    peak: itemHeight
                )
            } completion: { accomplished in
                tileSubview.removeFromSuperview()
            }
        }
    }
}

func addBackgroundTilesTimer()
{
    Timer.scheduledTimer(timeInterval: 6, goal: self, selector: #selector(continueBackgroundTiles), userInfo: nil, repeats: true)
}

@objc func continueBackgroundTiles()
{
    // spawn a vertical and horizontal row each x seconds
    let itemHeight = self.body.width / 7
    let widthBetween = (itemHeight * 2) / 5
    let columnHeight = widthBetween * 4

        
        for row in 0...7 {
            
            let columnPosition = (columnHeight * CGFloat(1))
            
            let xCalc = CGFloat(
                -((itemHeight + widthBetween) * 2) // two buffer rows to the left as properly
                + (itemHeight * Double(row) + (widthBetween * Double(row))))
            
            let yCalc = (((self.body.peak - itemHeight) - columnPosition * 2) +
                         ((row % 2 == 1 ? columnHeight : 0)))
                    
            let tileSubview = CustomImageView(body: CGRect(
                x: xCalc,
                y: yCalc,
                width: itemHeight,
                peak: itemHeight))
            
            tileSubview.picture = UIImage(named: "lightBand")!.withTintColor(.white)
            tileSubview.alpha = 0.04
            self.ContentView.addSubview(tileSubview)
             
            UIView.animate(withDuration: 30, delay: 0, choices: [.curveLinear]) {
                
                let newX = ((itemHeight * Double(row)) + (widthBetween * Double(row))) + widthBetween * 3
                let newY = 0 - ((itemHeight + (row % 2 == 0 ? columnHeight : 0)) + columnPosition * 2)
                
                tileSubview.body = CGRect(
                    x: newX,
                    y: newY,
                    width: itemHeight,
                    peak: itemHeight
                )
            } completion: { accomplished in
                tileSubview.removeFromSuperview()
            }
        }
    }

[ad_2]

Leave a Reply