Jak zaimplementować przesuń od lewej do prawej w komórce UITableView podobnej do iOS Mail
Próbuję odtworzyć tę samą technikę, której Apple używa w swojej aplikacji pocztowej do oznaczania poczty jako nieprzeczytanej lub "Oznacz jako nieprzeczytaną", gdy przesuwasz palcem od lewej do prawej w skrzynce pocztowej.
Znalazłem podobne rozwiązania ale tylko dla gestu przesuwania od prawej do lewej. Miałem nadzieję, że to samo rozwiązanie jest dostępne jako część Apple SDK w przeciwnym kierunku.
Jak mogę osiągnąć ten sam efekt gestu od lewej do prawej, co aplikacja pocztowa na iOS?
4 answers
znalazłem podobne rozwiązania, ale tylko dla gestu przesunięcia z Od prawej do lewej.
SWTableViewCell ma wszystkie opcje, które możesz chcieć.
Podczas usuwania komórki wystarczy ustawić lewy / prawy zestaw przycisków w razie potrzeby.
cell.leftUtilityButtons = [self leftButtons];
cell.rightUtilityButtons = [self rightButtons];
cell.delegate = self;
I ustawiając kontroler widoku jako delegata, możesz słuchać kliknięć przycisku. Szczegółowe informacje na temat implementacji znajdują się w tym linku
Ex 1:
Ex 2:
W przypadku, gdy szukasz przycisków ułożonych pionowo sprawdź to .
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-16 11:20:10
Zazwyczaj implementuję go na poziomie tabeli.
- (void)viewDidLoad
{
[super viewDidLoad];
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(leftSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.tableView addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(rightSwipe:)];
recognizer.delegate = self;
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.tableView addGestureRecognizer:recognizer];
}
Następnie masz kontrolę nad kierunkiem i możesz dowolnie dostosowywać kierunek]}
- (void)leftSwipe:(UISwipeGestureRecognizer *)gestureRecognizer
{
//do you left swipe stuff here.
}
- (void)rightSwipe:(UISwipeGestureRecognizer *)gestureRecognizer
{
//do you right swipe stuff here. Something usually using theindexPath that you get that way
CGPoint location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
}
Kredyt idzie do Jade Mind
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-23 18:31:32
Zaakceptowana odpowiedź w podanym linku dotyczy obu kierunków.
Zauważ, że gestureRecognizer.direction
zwraca YES
zarówno dla UISwipeGestureRecognizerDirectionLeft
, jak i UISwipeGestureRecognizerDirectionRight
.
Wystarczy zmodyfikować kilka rzeczy:
Zmiana selektora wywołanego przez get podczas przesuwania, więc wywoła Twoją metodę, zamiast tej W przykładzie posta,
I zmień kierunek machnięcia, aby był tylko z lewej Na prawą, a nie w obu kierunkach, jak to jest obecnie, ponieważ, jak rozumiem, jesteś próbuję ustawić przesunięcie w jedną stronę.
Więc Twój kod powinien wyglądać tak:
// In cellForRowAtIndexPath:, where you create your custom cell
cell.tableView=tableView;
cell.indexPath=indexPath;
UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(YOUR_METHOD_GOES_HERE)];
[cell addGestureRecognizer:swipeGestureRecognizer];
.
-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if([[gestureRecognizer view] isKindOfClass:[UITableViewCell class]] && ((UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)
return YES;
}
Zauważ, że możesz również użyć Odpowiedzi poniżej zaakceptowanej odpowiedzi i po prostu zmodyfikować właściwość rozpoznawania gestów direction
na UISwipeGestureRecognizerDirectionRight
, zamiast bieżącego kierunku w przykładzie, który jest UISwipeGestureRecognizerDirectionLeft
.
Jeśli zdecydujesz się to zaimplementować, twój kontroler viewController musi zaimplementować delegata rozpoznawania gestów, a Twój kod powinien wyglądać tak:
// Call this method in viewDidLoad
- (void)setUpLeftSwipe {
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(swipeRightt:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[self.tableView addGestureRecognizer:recognizer];
recognizer.delegate = self;
}
- (void)swipeRight:(UISwipeGestureRecognizer *)gestureRecognizer {
CGPoint location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
... do something with cell now that i have the indexpath, maybe save the world? ...
}
Uwaga-jeśli się nie mylę, musisz samodzielnie utworzyć animację przesuwania komórki, ponieważ, jak sądzę, domyślna animacja komórki Xcode jest tylko podczas przesuwania w lewo.
Kredyt idzie do MadhavanRP
i Julian
z podanego linku. Właśnie zmodyfikowałem ich odpowiedzi, by lepiej odpowiadały Twoim potrzebom.
Nie próbowałem i sam tego nie wdrożyłem.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-16 09:27:31
Użyj niestandardowego TableViewCell z scrollview, jak podano poniżej:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 120;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return header_view;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array_field1 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Schedule_cell";
Custom_Schedule_Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = (Custom_Schedule_Cell *)[Custom_Schedule_Cell cellFromNibNamed:@"Custom_Schedule_Cell"];
}
x = 0;
UILabel *lbl_title =[[UILabel alloc] initWithFrame:CGRectMake(x,0,cell.scroll_view.frame.size.width,cell.scroll_view.frame.size.height)];
lbl_title.text=@"Title Array";
[lbl_title setTextAlignment:NSTextAlignmentCenter];
[lbl_title setFont:[UIFont fontWithName:@"Raleway" size:18.0f]];
x += lbl_title.frame.size.width+10;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x,0,110,cell.scroll_view.frame.size.height)];
[button setTitle:@"Button1" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor grayColor]];
[button addTarget:self action:@selector(button1:) forControlEvents:UIControlEventTouchDown];
button.tag = indexPath.row;
[button.titleLabel setTextAlignment:NSTextAlignmentCenter];
[button.titleLabel setFont:[UIFont fontWithName:@"Raleway" size:14.0f]];
x += button.frame.size.width+5;
UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(x,0,110,cell.scroll_view.frame.size.height)];
[button2 setTitle:@"Button2" forState:UIControlStateNormal];
[button2 setBackgroundColor:[UIColor grayColor]];
[button2 addTarget:self action:@selector(button2:) forControlEvents:UIControlEventTouchDown];
button2.tag = indexPath.row;
[button2.titleLabel setTextAlignment:NSTextAlignmentCenter];
[button2.titleLabel setFont:[UIFont fontWithName:@"Raleway" size:14.0f]];
x += button2.frame.size.width+5;
UIButton *button3 = [[UIButton alloc] initWithFrame:CGRectMake(x,0,110,cell.scroll_view.frame.size.height)];
[button3 setTitle:@"Button3" forState:UIControlStateNormal];
[button3 setBackgroundColor:[UIColor grayColor]];
[button3 addTarget:self action:@selector(button3:) forControlEvents:UIControlEventTouchDown];
button3.tag = indexPath.row;
[button3.titleLabel setTextAlignment:NSTextAlignmentCenter];
[button3.titleLabel setFont:[UIFont fontWithName:@"Raleway" size:14.0f]];
[cell.scroll_view addSubview:lbl_title];
[cell.scroll_view addSubview:button];
[cell.scroll_view addSubview:button2];
[cell.scroll_view addSubview:button3];
x += button3.frame.size.width+5;
cell.scroll_view.contentSize = CGSizeMake(x,cell.scroll_view.frame.size.height);
cell.scroll_view.showsHorizontalScrollIndicator = NO;
cell.scroll_view.showsVerticalScrollIndicator = NO;
[cell.scroll_view setContentOffset:CGPointMake(0,0) animated:NO];
[cell.scroll_view setPagingEnabled:true];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Custom_Schedule_Cell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
// [selectedCell.scroll_view setContentOffset:CGPointMake(x2,0) animated:NO];
}
-(void)button1:(UIButton *)button
{
NSLog(@“button1 Click ");
[button setBackgroundColor:[UIColor redColor]];
}
-(void)button2:(UIButton *)button
{
NSLog(@“button2 Click");
[button setBackgroundColor:[UIColor greenColor]];
}
-(void)button3:(UIButton *)button
{
NSLog(@“button Click");
[button setBackgroundColor:[UIColor redColor]];
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-20 12:01:44