我是Swift开发的新手,所以我希望你们能帮忙。
我有一个游戏地图,它包含50个不同的ImageView(位置),从1到50,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,但它没有帮助。
如何解决此问题?或许你们可以建议我更好的解决方案?
提前感谢所有人。
答案 0 :(得分:1)
添加place4图片视图后添加女孩。
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)
答案 1 :(得分:0)
试试这个....
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
})
}