Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update position of UIImageView Swift

I'm trying to make a draggable image (monkey) takes the place of another draggable image (banana) when I'm pushing a button.

So far, I've been able to get the coordinates of the banana, and store them as the new x,y coordinates of the monkey. But, the view is not updating and the monkey does not moves to it's new place.

I'm sure that I need to update the view, but can't figure it out. I've tried to add a new subview (self.view.addSubview(monkey)) but when I do that, it's the entire view that is reset and both monkey and banana are going back to their original coordinates.

Here's the code of the action button :

@IBAction func getCoordinates (sender : AnyObject) {
    var bananax = banana.frame.origin.x
    var bananay = banana.frame.origin.y

    var monkeyx = monkey.frame.origin.x
    var monkeyy = monkey.frame.origin.y
    println("Banana : \(bananax) et \(bananay)")
    println("Monkey's default coordinates : \(monkeyx) et \(monkeyy)")


    monkeyx = bananax
    monkeyy = bananay
    println("Monkey's new coordinates : \(monkeyx) et \(monkeyy)")
}

Any idea on how to update only the monkey and not reseting the entire view ?

Thanks :)

like image 966
Nicolas Kalogeropoulos Avatar asked Aug 17 '14 16:08

Nicolas Kalogeropoulos


3 Answers

These values are just thrown away when you set monkeyx and monkeyy below.

var monkeyx = monkey.frame.origin.x
var monkeyy = monkey.frame.origin.y

Assigning the values below just assigns the values from one local variable to another. It does not actually change the coordinates for monkey.

monkeyx = bananax
monkeyy = bananay

You must set the new coordinates on monkey by constructing a new CGRect and assigning monkey's frame:

monkey.frame = CGRect(x: bananax, y: bananay, width: monkey.frame.size.width, height: monkey.frame.size.height)

As you are just setting monkey's origin to banana's origin, you can remove all the var declarations and reduce this to one line by constructing monkey's new frame with monkey's size and banana's origin:

monkey.frame = CGRect(origin: banana.frame.origin, size: monkey.frame.size)
like image 84
BergQuester Avatar answered Oct 09 '22 09:10

BergQuester


Hi create this extends if you want. For Swift

Create File Extends.Swift and add this code

/**
    Extension UIView
    by DaRk-_-D0G
*/
extension UIView {
    /**
    Set x Position

    :param: x CGFloat
    by DaRk-_-D0G
    */
    func setX(#x:CGFloat) {
        var frame:CGRect = self.frame
        frame.origin.x = x
        self.frame = frame
    }
    /**
    Set y Position

    :param: y CGFloat
    by DaRk-_-D0G
    */
    func setY(#y:CGFloat) {
        var frame:CGRect = self.frame
        frame.origin.y = y
        self.frame = frame
    }
    /**
    Set Width

    :param: width CGFloat
    by DaRk-_-D0G
    */
    func setWidth(#width:CGFloat) {
        var frame:CGRect = self.frame
        frame.size.width = width
        self.frame = frame
    }
    /**
    Set Height

    :param: height CGFloat
    by DaRk-_-D0G
    */
    func setHeight(#height:CGFloat) {
        var frame:CGRect = self.frame
        frame.size.height = height
        self.frame = frame
    }
}

For Use (inherits Of UIView)

inheritsOfUIView.setX(x: 100)
button.setX(x: 100)
view.setY(y: 100)
like image 36
YannSteph Avatar answered Oct 09 '22 08:10

YannSteph


Now you can do

monkey.frame.origin = banana.frame.origin
like image 1
Dmitry Kozlov Avatar answered Oct 09 '22 08:10

Dmitry Kozlov