【问题标题】:How to cancel the touch when the green part in the image is selected?选择图像中的绿色部分时如何取消触摸?
【发布时间】: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


【解决方案1】:

假设绿色部分的宽度是widthGreen,板的宽度是boatWidth。

let clapmedX = min(view.frame.size.width - widthGreen - boatWidth / 2, max(loc_tmp.x, widthGreen + boatWidth / 2))
location = CGPoint(x: clapmedX, y: boat.center.y)
boat.center = location

【讨论】:

  • 绿色部分的宽度在屏幕的两侧对吧?
  • 顺便说一下,我把水背景的绿色部分,作为背景图片!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多