Role a visualização da tabela com uma célula que foi selecionada com um toque longo
-
20-12-2019 - |
Pergunta
Eu tenho uma visualização de tabela que gostaria de rolar quando uma célula selecionada e "retirá-la" e movê-la pela visualização de tabela.A visualização da tabela rola perfeitamente e o toque longo para selecionar uma célula funciona, mas não consigo rolar a visualização da tabela quando tenho uma célula selecionada.
eu li em UIGestureRecognizerDelegate
mas não tenho certeza se isso está fazendo diferença.Eu também chamo esses métodos
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Aqui está parte do meu código:
- (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;
}
Quando defino o delegado como self assim:
longPress.delegate = self;
A visualização capaz rola, mas minha célula selecionada não se move.O que estou faltando para fazer isso funcionar?
* Atualizada *Depois de fazer mais algumas leituras/pesquisas, acho que para conseguir o efeito que desejo (o efeito de rolagem no TableView no aplicativo de clima da Apple), acho que meu gesto de pressionar longamente também terá que lidar com a rolagem da minha visualização de tabela.A rolagem padrão começa a rolar a tabela inteira instantaneamente, onde preciso que a tabela role apenas quando a célula pressionada longamente estiver na parte superior ou inferior do meu TableView.Estou correto em pensar assim?Tenho tentado encontrar um exemplo para analisar e estudar, mas não tive sorte em encontrar algo assim.
** Atualizada **Então, estou tentando descobrir como acionar o cronômetro de rolagem quando o usuário arrasta uma célula para cima.O problema que estou tendo agora é descobrir quando aquela célula está perto do topo, quando está no topo da tableview, é fácil, eu poderia usar algo como tableViewHight + 100
, mas esse código não funcionará quando o tableview for rolado para baixo.Aqui está o que tenho até agora:
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];
}
Basicamente, quero acionar essa instrução if quando a célula estiver a menos de 100px da altura da visualização da tabela rolável.
Solução
De UILongPressGestureRecognizer
você pode obter o atual y
posição do toque.A partir disso você pode calcular o quão próximo da parte inferior ou superior do UITableView
o toque é.Se você determinar que está próximo o suficiente para começar a rolar, inicie uma repetição NSTimer
que aumenta ou diminui o contentOffset do seu UITableView
por 1 pixel, rolando sua tabela.Altere a velocidade de rolagem alterando o timeInterval
do temporizador.
Quando o UILongPressGestureRecognizer
termina, ou se o y
a posição do toque se move para fora da zona de rolagem e invalida o cronômetro, que interromperá a rolagem.
Se você se sentir aventureiro, também poderá aumentar a velocidade da rolagem dinamicamente à medida que o usuário se aproxima da borda da tela. UITableView
alterando o timeInterval
conforme o usuário move o toque para cima ou para baixo, pois geralmente é assim que esse tipo de funcionalidade funciona.
Boa sorte!