Add UIButton on UITableView when drag , Swipe or touches moved on Cell Using UIGestureRecognizerDelegate in iPhone

A

//——–
.h
//Delegate
UIGestureRecognizerDelegate
UIButton *YourButton;

//——–
.m
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @”SimpleTableIdentifier”;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

if (cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
UISwipeGestureRecognizer *gestureL = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeL:)];
gestureL.direction = UISwipeGestureRecognizerDirectionLeft;
[cell.contentView addGestureRecognizer:gestureL];
[gestureL release];

UISwipeGestureRecognizer *gestureR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeR:)];
gestureR.direction = UISwipeGestureRecognizerDirectionRight;
[cell.contentView addGestureRecognizer:gestureR];
[gestureR release];

YourButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[YourButton addTarget:self action:@selector(YourButtonMethod:) forControlEvents:UIControlEventTouchDown];
[YourButton setTitle:@”MyButton” forState:UIControlStateNormal];
YourButton.frame = CGRectMake(320, 12.5, 104, 35);
[cell.contentView YourButton];
}

-(void)didSwipeL:(UIGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:AppsTable];

NSIndexPath *indexPath = [AppsTable indexPathForRowAtPoint:p];
if (indexPath == nil)
NSLog(@”long press on table view but not on a row”);
else
{
NSLog(@”long press on table view at row %d”, indexPath.row);
UITableViewCell *cell = [AppsTable cellForRowAtIndexPath:indexPath];

CGPoint newLeftCenter = CGPointMake( 198 + AddToFavorities.frame.size.width / 2.0f, AddToFavorities.center.y);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
YourButton.center = newLeftCenter;
[UIView commitAnimations];
YourButton.tag = indexPath.row;
[cell.contentView addSubview:AddToFavorities];
}
}

-(void)didSwipeR:(UIGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:AppsTable];

NSIndexPath *indexPath = [AppsTable indexPathForRowAtPoint:p];
if (indexPath == nil)
NSLog(@”long press on table view but not on a row”);
else
{
NSLog(@”long press on table view at row %d”, indexPath.row);
UITableViewCell *cell = [AppsTable cellForRowAtIndexPath:indexPath];

CGPoint newLeftCenter = CGPointMake( 320.0f + AddToFavorities.frame.size.width / 2.0f, AddToFavorities.center.y);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
YourButton.center = newLeftCenter;
[UIView commitAnimations];
YourButton.tag = indexPath.row;
[cell.contentView addSubview:AddToFavorities];
}
}

– (void) YourButtonMethod : (id) sender
{
//[self retriveAPps];

UIButton *clickedbtn = (UIButton *)sender;
NSLog(@”Added to favorities %d”,clickedbtn.tag);
}

About the author

abdulgafar.nurbash
By abdulgafar.nurbash

Category