Search code examples
iosswiftxcodeuiimageviewuilabel

How to move the UIImageVIew to the location of the UILabel in real time?


I want to create an image that follows the location of the label in SWIFT. I already created an UIimageview, but I don't know How I can get the image to follow the label But there is many labels, So I want to get the image to find Labels with specific strings and move image to location of label. And it's not shown in the code below, but the label is moving in real time. So I think that I have to update its location everytime.

Label's Code is here.

let label : UILabel
init(value: Int, position: Position, frame: CGRect) {
        self.label = UILabel(frame: CGRect(origin: CGPoint.zero, size: frame.size))
        self.value = value
        super.init(position: position, frame: frame)
        self.label.minimumScaleFactor = 1 / self.label.font.pointSize
        self.label.adjustsFontSizeToFitWidth = true
        self.label.textAlignment = .center
        self.label.numberOfLines = 1


        self.addSubview(label)
        self.value = value
}

Solution

  • I'm a bit confused by the question.

    Maybe the question is asking how to arrange an image in formation with a label, and the two views will move together if the label move somewhere else. In this case I would just put both the label and image in a containing view, and reposition the containing view when the label needs to move and the image needs to follow.

    Maybe the question is asking about independent labels and images on screen that are matched up somehow and must move into formation, and then after pairing up and moving into formation the label may move again, and the image should follow. I would create some object that stored the label and an optional reference to a paired image. Then I'd write a method for this object that would animate an image view into formation with the label. Then I'd write another method that paired the label with the view which would call the first method to move the image. Then I'd write another method that would move the label and call the first method to move the image to the new destination. All pairing and moving logic in the program would use this object and its methods to keep the image in formation.