UITableViewCell をセットアップして、右にスワイプしてアクションを表示し、左にスワイプして非表示にできるようにします。それらはオフスクリーンに設定され、セルがスワイプされると、セルの原点が左 -120px に遷移してコントロールが表示されます。
問題は、それらが公開されてクリックされると発砲しないことです。これが私のコードです。
セルのセットアップ
UISwipeGestureRecognizer *completeSwipeGestureShow = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(showCompleteControl:)];
UISwipeGestureRecognizer *completeSwipeGestureHide = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hideCompleteControl:)];
completeSwipeGestureShow.direction = UISwipeGestureRecognizerDirectionRight;
completeSwipeGestureHide.direction = UISwipeGestureRecognizerDirectionLeft;
UIView *cellControlDone = [[UIView alloc] initWithFrame:CGRectMake(-60, 0, 60, 127)];
UIView *cellControlDelete = [[UIView alloc] initWithFrame:CGRectMake(-120, 0, 60, 127)];
cellControlDone.backgroundColor = [UIColor colorWithRed:(36.0/255.0) green:(195.0/255.0) blue:(28.0/255.0) alpha:1.0];
cellControlDelete.backgroundColor = [UIColor colorWithRed:(236.0/255.0) green:(1.0/255.0) blue:(1.0/255.0) alpha:1.0];
UIButton *cellControlDoneIcon = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *cellControlDeleteIcon = [UIButton buttonWithType:UIButtonTypeCustom];
[cellControlDoneIcon setBackgroundImage:[UIImage imageNamed:@"Done.png"] forState:UIControlStateNormal];
[cellControlDeleteIcon setBackgroundImage:[UIImage imageNamed:@"Delete.png"] forState:UIControlStateNormal];
cellControlDoneIcon.frame = CGRectMake(((60 - 22) / 2), ((127 - 22) / 2), 22, 22);
cellControlDeleteIcon.frame = CGRectMake(((60 - 22) / 2), ((127 - 22) / 2), 22, 22);
[cellControlDoneIcon addTarget:self action:@selector(completeTask) forControlEvents:UIControlEventTouchUpInside];
[cellControlDeleteIcon addTarget:self action:@selector(cancelTask) forControlEvents:UIControlEventTouchUpInside];
[cellControlDone addSubview:cellControlDoneIcon];
[cellControlDelete addSubview:cellControlDeleteIcon];
[cell addSubview:cellControlDone];
[cell addSubview:cellControlDelete];
cell.clipsToBounds = YES;
[cell addGestureRecognizer:completeSwipeGestureShow];
[cell addGestureRecognizer:completeSwipeGestureHide];
アクション
- (void) showCompleteControl:(UISwipeGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:tableView];
NSIndexPath *swipedIndexPath = [tableView indexPathForRowAtPoint:location];
UITableViewCell *swipedCell = [tableView cellForRowAtIndexPath:swipedIndexPath];
[UIView animateWithDuration:0.25
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{
[swipedCell setBounds:CGRectMake(-120, 0, 320, 127)];
NSArray* subviews = [swipedCell.contentView subviews];
for (UIView* subview in subviews) {
if(subview.tag == 9999)
{
subview.frame = CGRectMake(0, 126, 10, 1);
subview.backgroundColor = [UIColor colorWithRed:(224.0/255.0) green:(224.0/255.0) blue:(224.0/255.0) alpha:1.0];
}
}
}
completion:^(BOOL finished){
// Do nothing
}
];
}
- (void) hideCompleteControl:(UISwipeGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:tableView];
NSIndexPath *swipedIndexPath = [tableView indexPathForRowAtPoint:location];
UITableViewCell *swipedCell = [tableView cellForRowAtIndexPath:swipedIndexPath];
[UIView animateWithDuration:0.25
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{
[swipedCell setBounds:CGRectMake(0, 0, 320, 127)];
NSArray* subviews = [swipedCell.contentView subviews];
for (UIView* subview in subviews) {
if(subview.tag == 9999)
{
subview.frame = CGRectMake(0, 0, 10, 127);
subview.backgroundColor = [UIColor colorWithRed:(124.0/255.0) green:(124.0/255.0) blue:(124.0/255.0) alpha:1.0];
}
}
}
completion:^(BOOL finished){
// Do nothing
}
];
}
- (void) completeTask
{
NSLog(@"Task Complete");
}
- (void) cancelTask
{
NSLog(@"Task Canceled");
}