Thursday, February 21, 2019

Swift make blinking spotlight

We want to have a row of blinking spotlight.
Let make some round icons with same dimension, but different color at center.

Drag icons in to project.

The images with same dimension, so when we change them, it look like they are blinking.
Declare to above viewDidLoad.
let im1 = UIImage(named: "l1");
    let im2 = UIImage(named: "l2");
    let im3 = UIImage(named: "l3");
    let im4 = UIImage(named: "l1");
    let im5 = UIImage(named: "l2")
  
    var ima1: UIImageView!
    var ima2: UIImageView!
    var ima3: UIImageView!
    var ima4: UIImageView!;
    var ima5: UIImageView!;
var count=1
Set position, add to view inside viewDidLoad
ima1 = UIImageView(frame: CGRect(x: le, y: 520, width: 40, height: 40))
        ima2 = UIImageView(frame: CGRect(x: le+45, y: 520, width: 40, height: 40))
        ima3 = UIImageView(frame: CGRect(x: le+90, y: 520, width: 40, height: 40))
        ima4 = UIImageView(frame: CGRect(x: le+135, y: 520, width: 40, height: 40))
        ima5 = UIImageView(frame: CGRect(x: le+180, y: 520, width: 40, height: 40))
        ima1.image = im1
        ima2.image = im2
        ima3.image = im3
        ima4.image = im4
        ima5.image = im5
view.addSubview(ima1)
        view.addSubview(ima2)
        view.addSubview(ima3)
        view.addSubview(ima4)
        view.addSubview(ima5)
Variable le use to make images center screen.
We get screen width.
let r = UIScreen.mainScreen().bounds.size.width
We know images row width 220dp, we calculate margin like this.
let le = (Int(r) - 220)/2
Copy to above last close bracket.
func update(){      
       count = count + 1
        if count%3==1{
            ima1.image = im1
            ima2.image = im2
            ima3.image = im3
            ima4.image = im4
            ima5.image = im5
        }
        else if count%3==2{
            ima1.image = im2
            ima2.image = im3
            ima3.image = im4
            ima4.image = im5
            ima5.image = im1
        }
        else{
            ima1.image = im3
            ima2.image = im4
            ima3.image = im5
            ima4.image = im1
            ima5.image = im2
        }
    }
Last, add this line in to viewDidLoad.
let time=NSTimer.scheduledTimerWithTimeInterval(1.0, target:self,  selector: "update",userInfo: nil, repeats: true)
We change images follow time, there are three phases change after 1, 2, 3 second. We divide time to 3 and get remainder, use them to set if command.

This changing images make them look like blinking.

No comments:

Post a Comment