[英]Need to restrict movement of UIImageView to region covered by UITableView using UIPanGestureRecognizer in iOS
我目前正在使用我的應用程序中的UIPanGestureRecognizer對象移動UIImageView。 目前,此UIImageView在我的屏幕上非常好地上下移動。 但是,我現在想做的就是將UIImageView的運動邊界限制在位於屏幕中間的UITableView覆蓋的區域內。 我想將此運動限制在UITableView的上下邊界上。 這是我控制UIImageView移動的相關方法:
- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer {
_startLocation = [recognizer locationInView:_imageView];
NSLog(@"The point is: %d", _startLocation);
CGPoint newCenter = _imageView.center;
newCenter.y = [recognizer locationInView:[_imageView superview]].y;
//this is where I figure I need to include an if statement to perform a check to see if the location is within the desired region
_imageView.center = newCenter;
}
我意識到我將需要在我的方法中包括一個“ if”語句,以檢查UIImageView是否在所需區域內,但是問題是我不確定如何檢查這一點。 有人可以幫我嗎?
您應該使用translationInView
方法( 文檔 )。
這將返回用戶將圖像移至的CGPoint
位置。 將圖像視圖的原始位置與翻譯進行比較。 如果平移太大,以致圖像視圖將移動到所需區域之外,請不要移動它。
該代碼應如下所示(我認為您也許可以將其正確放置):
CGPoint translation = [recognizer translationInView:_imageView.superview];
[recognizer setTranslation:CGPointMake(0, 0) inView:_imageView.superview];
CGPoint center = recognizer.view.center;
center.y += translation.y;
if (center.y < _table.frame.origin.y
|| center.y > _table.frame.size.height) {
return;
}
recognizer.view.center = center;
有關替代實施,請參見此問題 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.