Implementing the delete button on a UIView similar to UITableView controller delete

You might seen the delete button on a table view when you swipe from right to left  or viceversa. Lets try doing that here on a UIView. It can be done as below.
Manipulate the layer and use CATransitions :
[sourcecode language=”objc”]
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(10, 300, 1, 45)];
[button setTitle:@"helloSir" forState:UIControlStateNormal];
[self.view addSubview:button];
CATransition *animation = [CATransition animation];
animation.type = kCATransitionFromRight;
animation.duration = 0.25f;
[[[button titleLabel] layer] addAnimation:animation forKey:nil];
[/sourcecode]
You also can do using the manual method
[sourcecode language=”objc”]
-(void) myLeftAction:(UIGestureRecognizer*) recognizer{
testButton.frame = CGRectMake(200.0, 210.0, 60.0, 40.0);
[testButton.layer setBorderWidth:0.0f];
[self.view addSubview:testButton];
[UIView animateWithDuration:0.0f delay:0.1f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
testButton.frame = CGRectMake(210.0, 210.0, 50.0, 40.0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.0f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
testButton.frame = CGRectMake(220.0, 210.0, 40.0, 40.0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.0f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
testButton.frame = CGRectMake(230.0, 210.0, 30.0, 40.0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.0f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
testButton.frame = CGRectMake(240.0, 210.0, 20.0, 40.0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.0f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
testButton.frame = CGRectMake(250.0, 210.0, 10.0, 40.0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.0f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
testButton.frame = CGRectMake(260.0, 210.0, 0.0, 40.0);
} completion:^(BOOL finished) {
[testButton setTitle:@"" forState:UIControlStateNormal];
}];
}];
}];
}];
}];
}];
}
 
-(void) myRightAction:(UIGestureRecognizer*) recognizer {
[testButton addTarget:self
action:@selector(showFriends) forControlEvents:UIControlEventTouchDown];
testButton.frame = CGRectMake(260.0, 210.0, 0.0, 40.0);
[testButton.layer setBorderWidth:0.0f];
[self.view addSubview:testButton];
[UIView animateWithDuration:0.0f delay:0.1f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
testButton.frame = CGRectMake(250.0, 210.0, 10.0, 40.0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.0f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
testButton.frame = CGRectMake(240.0, 210.0, 20.0, 40.0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.0f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
testButton.frame = CGRectMake(230.0, 210.0, 30.0, 40.0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.0f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
testButton.frame = CGRectMake(220.0, 210.0, 40.0, 40.0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.0f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
testButton.frame = CGRectMake(210.0, 210.0, 50.0, 40.0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.0f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
testButton.frame = CGRectMake(200.0, 210.0, 60.0, 40.0);
} completion:^(BOOL finished) {
[testButton setTitle:@"Delete" forState:UIControlStateNormal];
}];
}];
}];
}];
}];
}];
}
– (void)viewDidLoad
{
[super viewDidLoad];
testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(10, 200, 300, 200)];
[testView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:testView];
UISwipeGestureRecognizer * rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(myRightAction:)];
[rightRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[testView addGestureRecognizer:rightRecognizer];
UISwipeGestureRecognizer * leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(myLeftAction:)];
[leftRecognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[testView addGestureRecognizer:leftRecognizer];
}
[/sourcecode]

Leave a Reply

Your email address will not be published. Required fields are marked *