【发布时间】:2017-04-20 16:02:21
【问题描述】:
我想让船只在蓝色区域左右移动,而不是在绿色区域。当我触摸绿色区域时,船正在移动到那个位置。我不知道会发生什么,它应该只在蓝色区域移动。这是我的触摸开始和感动:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch : UITouch = touches.first as UITouch!
let loc_tmp = touch.location(in: self.view)
location = CGPoint(x: loc_tmp.x, y: boat.center.y)
boat.center = location
}
override func touchesMoved(_ touches: Set<UITouch>, with event:
UIEvent?) {
let touch : UITouch = touches.first as UITouch!
let loc_tmp = touch.location(in: self.view)
location = CGPoint(x: loc_tmp.x, y: boat.center.y)
boat.center = location
}
【问题讨论】:
-
计算蓝色区域左右的x位置,设置为小船x位置的约束。
-
我有类似 var leftSide = 55 var rightSide = 255 你是说我比较 loc_tmp.x
标签: ios swift touchesbegan touchesmoved