I want to allow the boat to move only left and right in blue area, not in green area. When I touch the green area the boat is moving to that position. I don't what this to happen, it should only move only in blue area. Here is my touches began and touches moved :
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
}
Let say the width of green part is widthGreen and the width of the boad is 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