在 touchesMoved:withEvent: 中,保持 center.y 不變,僅將 center.x 設為觸摸點的 x 座標,即可實現水平拖曳限制。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
basketView.center = CGPointMake(point.x, basketView.center.y);
}
basketView.center.y 維持原值,限制垂直位置。MIN/MAX 判斷:CGFloat newX = MAX(minX, MIN(maxX, point.x));UIPanGestureRecognizer 的 translationInView: 計算。