1

I am trying to draw a line using multiple UIImageView of the same circle image.

I have tried the following:

let imageName = "circle.jpg"
let image = UIImage(named: imageName)

    for var i in 0..<100 {
        i += 1
        let imageView = UIImageView(image: image!)
        imageView.frame = CGRect(x: i, y: 200, width: 25, height: 25)
        view.addSubview(imageView)

      }

This produces one circle at the position (100, 200). This happens because the same UIImageView is being added to the subview so it is only updating the position rather than adding a new UIImageView. If I create a new UIImageView named "imageView1" and add it to the subview, it will create a new circle.

For the purpose of forming a line by overlapping the UIImageViews of these circles, manually creating a UIImageView for each circle is obviously inefficient.

So, how can I use the same UIImage to draw multiple UIImageViews? Any other suggestions on how I can get around accomplishing this?

Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41

3 Answers3

1

You need a circle to form a line like this

enter image description here

Well here the code I used for this using While loop

let imageName = "synergy-many-people-turning-in-gears_gg55375354.jpg"
        let image = UIImage(named: imageName)
        var i = 0
        while(i<100)
        {
            let imageView = UIImageView(image: image!)
            imageView.frame = CGRect(x: i, y: 200, width: 25, height: 25)
            view.addSubview(imageView)
            i = i + 25//Here I set it to the width so that the images don't overlap 
            //you can use any value for the desired effect it just sets the location 
            //of X-Co-ordinate where the next image would be added
         }
Md. Ibrahim Hassan
  • 5,359
  • 1
  • 25
  • 45
0

I would quickly try out something like this (didn't test it though):

var imageView = UIImageView(image: image!)

TekMi
  • 191
  • 6
0

Creating multiple UIImageViews is the thing you need to do. If this will be to slow (btw you should place your image inside assets catalogue to allow caching and improve performance), you should switch to lower level api. Check iOS - An easy way to draw a circle using CAShapeLayer And, btw, why do you change i variable inside loop, this is very bad practice

Community
  • 1
  • 1
user3237732
  • 1,976
  • 2
  • 21
  • 28