我有一个表格视图,我想在选定的单元格时滚动并且" pop"它出来并在桌面视图周围移动它。表格视图滚动得很好,长按以选择单元格,但是当我选择单元格时,我无法滚动表格视图。
我在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;
}
当我将委托设置为自我时:
longPress.delegate = self;
可查看的视图滚动,但随后我选择的单元格无法移动。我缺少什么使这项工作?
*已更新* 在做了一些阅读/研究后,我认为为了让我达到我想要的效果(在Apple的天气应用程序中对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语句。
答案 0 :(得分:1)
从UILongPressGestureRecognizer
,您可以获得当前y
的触摸位置。从中可以计算出触摸UITableView
的底部或顶部的接近程度。如果您确定它足够接近开始滚动,则启动重复NSTimer
,将UITableView
的contentOffset递增或递减1个像素,滚动表格。通过更改计时器的timeInterval
来更改滚动的速度。
当UILongPressGestureRecognizer
结束时,或者触摸的y
位置移动到滚动区域之外时,则会使计时器无效,这将停止滚动。
如果您感觉有冒险精神,当用户向上或向下移动触摸时,通过更改UITableView
,当用户接近timeInterval
的边缘时,您也可以动态地提高滚动速度,因为这通常是这种功能的工作原理。