滚动表视图,其中包含长按选择的单元格
-
20-12-2019 - |
题
我有一个表格视图,当一个选定的单元格并"弹出"它并在表格视图中移动它时,我想滚动它。表格视图滚动得很好,长按选择单元格可以工作,但是当我选择了单元格时,我无法滚动表格视图。
我读了 UIGestureRecognizerDelegate
但我不确定这是否有所作为。我也调用这些方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
这是我的代码的一部分:
- (IBAction)longPressGestureRecognized:(id)sender {
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
UIGestureRecognizerState state = longPress.state;
//longPress.delegate = self;
CGPoint location = [longPress locationInView:self.personTableView];
NSIndexPath *indexPath = [self.personTableView indexPathForRowAtPoint:location];
static UIView *snapshot = nil; ///< A snapshot of the row user is moving.
static NSIndexPath *sourceIndexPath = nil; ///< Initial index path, where gesture begins.
switch (state) {
case UIGestureRecognizerStateBegan: {
if (indexPath) {
sourceIndexPath = indexPath;
UITableViewCell *cell = [self.personTableView cellForRowAtIndexPath:indexPath];
// capture the color of the cell before blacking out
savedTextColor = cell.detailTextLabel.textColor;
// Take a snapshot of the selected row using helper method.
snapshot = [self customSnapshotFromView:cell];
// Add the snapshot as subview, centered at cell's center...
__block CGPoint center = cell.center;
snapshot.center = center;
snapshot.alpha = 0.0;
[self.personTableView addSubview:snapshot];
[UIView animateWithDuration:0.25 animations:^{
// Offset for gesture location.
center.y = location.y;
snapshot.center = center;
snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);
snapshot.alpha = 0.98;
// Black out.
cell.detailTextLabel.alpha = 0;
cell.textLabel.alpha = 0;
} completion:nil];
}
break;
}
case UIGestureRecognizerStateChanged: {
CGPoint center = snapshot.center;
center.y = location.y;
snapshot.center = center;
// Is destination valid and is it different from source?
if (indexPath && ![indexPath isEqual:sourceIndexPath]) {
// ... update data source.
[dataSingelton saveUpdatedPersonList:(int)indexPath.row sourceIndex:(int)sourceIndexPath.row];
//[[dataSingelton mutableDataArray] exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];
// ... move the rows.
[self.personTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];
// ... and update source so it is in sync with UI changes.
sourceIndexPath = indexPath;
}
break;
}
default: {
// Clean up.
UITableViewCell *cell = [self.personTableView cellForRowAtIndexPath:sourceIndexPath];
[UIView animateWithDuration:0.25 animations:^{
snapshot.center = cell.center;
snapshot.transform = CGAffineTransformIdentity;
snapshot.alpha = 0.1;
cell.detailTextLabel.alpha = 1;
cell.textLabel.alpha = 1;
} completion:^(BOOL finished) {
[snapshot removeFromSuperview];
snapshot = nil;
cell.hidden = NO;
}];
sourceIndexPath = nil;
break;
}
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (UIView *)customSnapshotFromView:(UIView *)inputView {
UIView *snapshot = [inputView snapshotViewAfterScreenUpdates:YES];
snapshot.alpha = 0.1;
snapshot.layer.masksToBounds = NO;
snapshot.layer.cornerRadius = 0.0;
snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);
snapshot.layer.shadowRadius = 5.0;
snapshot.layer.shadowOpacity = 0.4;
return snapshot;
}
当我像这样将委托设置为self时:
longPress.delegate = self;
Able视图滚动,但随后我选择的单元格不会移动。我错过了什么,使这项工作?
*更新 * 在做了一些更多的阅读/研究之后,我认为为了让我达到我想要的效果(Apple的天气应用程序中TableView上的滚动效果)我认为我的长按手势也将不得不处理我的默认滚动开始立即滚动整个表格,因为我需要表格只有当长按单元格位于我的TableView的顶部或底部时才滚动。我这样想是正确的吗?我一直在试图找到一个例子来分解和研究,但没有运气找到类似的东西。
**更新 **
所以我试图弄清楚如何在用户将单元格向上拖动到顶部时触发向上滚动计时器。我现在遇到的问题是弄清楚该单元格何时接近顶部,何时位于tableview的顶部,这很容易我可以使用类似的东西 tableViewHight + 100
, ,但是当tableview向下滚动时,此代码将不起作用。这是我到目前为止所拥有的:
NSLog(@"This is table view hight %.2f", tableViewHight);
NSLog(@"This is content offset %f", (personTableView.contentOffset.y));
NSLog(@"Should be 100 place holder %.2f", (tableViewHight - tableViewHight) + personTableView.contentOffset.y + 100);
NSLog(@"location y %f", location.y);
// gets called repeatedly
// Step 1: check to see if we need to start scrolling
if (location.y < tableViewHight - tableViewHight + 100) {
// ** top **
// Step 2: make sure the current y postoin isn't greater then the previous y postion
if (location. y <= originalLocation.y) {
// NSLog(@"This is the offset y %f", self.personTableView.contentOffset.y);
// Step 4: check to see if we have a timer and if not create one
[self startTimer];
}
else{
//Step 3: they started to scroll up so end the timer
[self endTimer];
}
}
else if(location.y > tableViewHight - 100)
{
// ** bottom **
// Step 2: make sure the current y postoin isn't less then the previous y postion
if (location. y >= originalLocation.y) {
NSLog(@"its less then 0");
if (!timer) {
// Step 4: check to see if we have a timer and if not create one
[self startTimer];
}
}
else{
//Step 3: they started to scroll up so end the timer
[self endTimer];
}
}
else{
// ** middle **
// Step 2: check to see if we have a timer and if so destory it
[self endTimer];
}
基本上我想在单元格小于100px时触发if语句从可滚动表视图的高度。
解决方案
从 UILongPressGestureRecognizer
你可以得到电流 y
触摸的位置。从中你可以计算出离底部或顶部有多近。 UITableView
触摸是。如果你确定它足够接近开始滚动,然后开始重复 NSTimer
这会增加或减少你的内容集。 UITableView
通过1像素,滚动你的表。通过更改 timeInterval
计时器的。
当 UILongPressGestureRecognizer
结束,或者如果 y
触摸的位置移动到滚动区域之外,然后使计时器无效,这将停止滚动。
如果你觉得冒险,你也可以随着用户越来越接近滚动的边缘而动态地提高滚动的速度。 UITableView
通过改变 timeInterval
当用户向上或向下移动他们的触摸时,这通常是这种类型的功能的工作方式。
祝你好运!