[英]Trying to limit movement of a UIImage when using UIPanGestureRecognizer in iOS
我正在開發一個允許用戶移動標尺在UITableView上滾動的應用程序。 此刻,標尺移動得很好,只是我想將其移動限制為僅UITableView的行,而沒有別的。 不幸的是,此刻,用戶能夠從頂部滾動整個表格,並超出表格底部到屏幕底部。 我已附上圖片以顯示此內容:
這是我擁有的相關代碼:
- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer {
CGPoint newCenter = _imageView.center;
newCenter.y = [recognizer locationInView:[_imageView superview]].y;
if (newCenter.y - _imageView.frame.size.height / 2 < self.table.frame.origin.y)
newCenter.y = self.table.frame.origin.y + _imageView.frame.size.height / 2;
else if (newCenter.y + _imageView.frame.size.height / 2 > self.table.frame.origin.y + self.table.frame.size.height)
newCenter.y = self.table.frame.origin.y + self.table.frame.size.height - _imageView.frame.size.height / 2;
_imageView.center = newCenter;
if ([recognizer state] == UIGestureRecognizerStateChanged) {
if (newCenter.y == self.table.frame.origin.y + _imageView.frame.size.height / 2) {
CGPoint topPoint = [recognizer locationInView:_table];
NSIndexPath *nextRow = [_table indexPathForRowAtPoint:topPoint];
int currentRow = nextRow.row;
if (currentRow != 0) {
nextRow = [NSIndexPath indexPathForRow:currentRow-- inSection:0];
[UIView animateWithDuration:.3 animations:^{
[_table selectRowAtIndexPath:nextRow animated:NO scrollPosition:UITableViewScrollPositionBottom];
}];
}
}
//below is the specific section where I believe is the issue
else if (newCenter.y == self.table.frame.origin.y + self.table.frame.size.height - _imageView.frame.size.height / 2) {
CGPoint bottomPoint = [recognizer locationInView:_table];
NSIndexPath *nextRow = [_table indexPathForRowAtPoint:bottomPoint];
int currentRow = nextRow.row;
if (currentRow != [self.tireCode count]-1) {
nextRow = [NSIndexPath indexPathForRow:currentRow++ inSection:0];
[UIView animateWithDuration:.3 animations:^{
[_table selectRowAtIndexPath:nextRow animated:NO scrollPosition:UITableViewScrollPositionBottom];
}];
}
}
}
就像我說的那樣,我希望標尺僅滾動UITableView的行,而沒有其他內容。 我做錯了什么?
在分配超出范圍的值之前,應檢查newCenter.y的值。 為其定義一個最小值和最大值。
float calculatedY = self.table.frame.origin.y + _imageView.frame.size.height / 2;
calculatedY = MAX(calculatedY, MINIMUM_VALUE_HERE);
calculatedY = MIN(calculatedY, MAXIMUM_VALUE_HERE);
newCenter.y = calculatedY;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.