dakath
dakath

Reputation: 89

How to position one image in front of another?

I'm new in Swift development, so I hope you guys can help.

I have a game map, it contains 50 different ImageView (position) from 1 to 50, 4 characters, and a dice roll(button). When I press i dice roll button it returns a random number between 1 to 6.

For example: I pressed dice roll button and it returned 4, so now i want to move my character to new position(to ImageView place4).

Here is my code:

var girl: UIImageView!
var place4 = UIImageView(image: UIImage(named: "4.png"))

girl.frame = CGRect(x: 200, y: 200, width: 45, height: 100) 
scrollView.addSubview(girl)

place4.frame = CGRect(x: 210, y: 145, width: 60, height: 30)
scrollView.addSubview(place4)

func moveCharacter(sender:UIButton!) {
let duration = 1.0
    let delay = 0.0 // delay will be 0.0 seconds (e.g. nothing)
    let options = UIViewAnimationOptions.CurveEaseInOut // change the timing curve to `ease-in ease-out`

    UIView.animateWithDuration(duration, delay: delay, options: options, animations: {
        // any changes entered in this block will be animated
        self.girl.center = CGPointMake(self.place4.center.x, self.place4.center.y - 30)

        }, completion: { finished in
            // any code entered here will be applied
            // once the animation has completed

            //self.place4.hidden = true

    })
}

When I press button, UIImageView "girl" should move to X and Y coordinate of UIImageView "place4". To move character to new position I use following code:

self.girl.center = CGPointMake(self.place4.center.x, self.place4.center.y - 30)

and girl image should appear over place4 image, but actually girl image appear behind place4 image, pls look at the screenshot:

enter image description here

But actually what i try to achieve is this:

enter image description here

I tried to use UiView methods bringSubviewToFront and sendSubviewToBack, but it did'n help.

How can I fix this issue? Or maybe you guys can advise me better solution?

Thanks to all in advance.

Upvotes: 1

Views: 1387

Answers (2)

DJ1
DJ1

Reputation: 944

Try this one ....

var girl: UIImageView!
var place4 = UIImageView(image: UIImage(named: "4.png"))

place4.frame = CGRect(x: 210, y: 145, width: 60, height: 30)
scrollView.addSubview(place4)

girl.frame = CGRect(x: 200, y: 200, width: 45, height: 100) 
scrollView.addSubview(girl)

func moveCharacter(sender:UIButton!) {
    let duration = 1.0
    let delay = 0.0 // delay will be 0.0 seconds (e.g. nothing)
    let options = UIViewAnimationOptions.CurveEaseInOut // change the timing curve to `ease-in ease-out`

    UIView.animateWithDuration(duration, delay: delay, options: options, animations: {
        // any changes entered in this block will be animated
        self.girl.center = CGPointMake(self.place4.center.x, self.place4.center.y - 30)

    }, completion: { finished in
        // any code entered here will be applied
        // once the animation has completed

        //self.place4.hidden = true
    })
}

Upvotes: 0

Sasha Kozachuk
Sasha Kozachuk

Reputation: 1303

Add girl after you add place4 image view.

scrollView.addSubview(place4)

Result should be like this:

girl.frame = CGRect(x: 200, y: 200, width: 45, height: 100)

place4.frame = CGRect(x: 210, y: 145, width: 60, height: 30)

scrollView.addSubview(place4)
scrollView.addSubview(girl)

Upvotes: 1

Related Questions