我是 Swift 开发的新手,所以我希望你们能提供帮助。
我有一张游戏 map ,它包含从 1 到 50 的 50 个不同的 ImageView(位置)、4 个字符和一个掷骰子(按钮)。 当我按下掷骰子按钮时,它会返回 1 到 6 之间的随机数。
例如:我按下了掷骰子按钮,它返回了 4,所以现在我想将我的角色移动到新位置(到 ImageView place4)。
这是我的代码:
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
})
}
当我按下按钮时,UIImageView“girl”应该移动到 UIImageView“place4”的 X 和 Y 坐标。要将角色移动到新位置,我使用以下代码:
self.girl.center = CGPointMake(self.place4.center.x, self.place4.center.y - 30)
而女孩图片应该出现在place4图片之上,但实际上女孩图片出现在place4图片后面,请看截图:
但实际上我试图实现的是:
我尝试使用 UiView 方法 bringSubviewToFront 和 sendSubviewToBack,但它没有帮助。
我该如何解决这个问题?或者你们可以建议我更好的解决方案吗?
提前感谢大家。
最佳答案
在添加 place4 ImageView 后添加女孩。
scrollView.addSubview(place4)
结果应该是这样的:
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)
关于ios - 如何将一张图片放在另一张图片前面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35951705/