首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

iOS 限制圖片僅跟隨手指水平移動:在 touchesMoved 中僅更新 center.x

     編輯:程式設計綜合問答

在 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: 計算。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved