Reputation: 177
I am trying to add UISwipeGestureRecognition
to a custom View. I would like to do the same as IOS notifications but in the other way. I did it before with tapGesture
, but when I add try with swipe, it does not work.
I am using this code. It is a hidden tableView that I display when I do a swipeUp
with one finger.
My problem now is that the view goes up but behind the main view.
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *headerLabel = [[UILabel alloc]init];
headerLabel.tag = section;
headerLabel.userInteractionEnabled = YES;
headerLabel.backgroundColor = [UIColor salmonUser];
headerLabel.text = [NSString stringWithFormat:@"Interesteds"];
headerLabel.frame = CGRectMake(100, 0, 120,tableView.tableHeaderView.frame.size.height);
[headerLabel setTextAlignment:NSTextAlignmentCenter];
UISwipeGestureRecognizer *upRecognizer= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[upRecognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
UISwipeGestureRecognizer *downRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[downRecognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[headerLabel addGestureRecognizer:upRecognizer];
[headerLabel addGestureRecognizer:downRecognizer];
return headerLabel;
//return nil;
}
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
{
NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
BOOL switchUpDown = [d boolForKey:@"switchUpDown"];
UISwipeGestureRecognizerDirection up = switchUpDown ? UISwipeGestureRecognizerDirectionUp : UISwipeGestureRecognizerDirectionDown;
UISwipeGestureRecognizerDirection down = switchUpDown ? UISwipeGestureRecognizerDirectionDown : UISwipeGestureRecognizerDirectionUp;
if (recognizer.direction == up){
[UIView animateWithDuration:0.3
delay:0.3
options: UIViewAnimationCurveEaseOut
animations:^{
CGRect rect = self.view.frame;
rect.origin.y = self.view.frame.size.height-(self.view.frame.size.height/2);
self.usersView.frame = rect;
mostrada =true;
}
completion:^(BOOL finished){}];
} else if(recognizer.direction == down){
[UIView animateWithDuration:0.3
delay:0.3
options: UIViewAnimationCurveEaseOut
animations:^{
CGRect rect = self.view.frame;
rect.origin.y = self.view.frame.size.height-(self.view.frame.size.height/11);
self.usersView.frame = rect;
mostrada=false;
}
completion:^(BOOL finished){
}];
}
}
Upvotes: 1
Views: 5100
Reputation: 1544
Use This code :
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *headerLabel = [[UILabel alloc]init];
headerLabel.tag = section;
headerLabel.userInteractionEnabled = YES;
headerLabel.backgroundColor = [UIColor salmonUser];
headerLabel.text = [NSString stringWithFormat:@"Interesteds"];
headerLabel.frame = CGRectMake(100, 0, 120,tableView.tableHeaderView.frame.size.height);
[headerLabel setTextAlignment:NSTextAlignmentCenter];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(upSwipe:)];
[swipeLeft setDirection: UISwipeGestureRecognizerDirectionUp ];
[headerLabel addGestureRecognizer:swipeLeft];
swipeLeft=nil;
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(downSwipe:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionDown];
[headerLabel addGestureRecognizer:swipeRight];
return headerLabel;
}
I have used two different method to take action individually for up and down swipe as
upSwipe
anddownSwipe
. This will simplefy the code.
-(void)upSwipe:(UISwipeGestureRecognizer *)gesture
{
[UIView animateWithDuration:0.3
delay:0.3
options: UIViewAnimationCurveEaseOut
animations:^{
CGRect rect = self.view.frame;
rect.origin.y = self.view.frame.size.height-(self.view.frame.size.height/2);
self.usersView.frame = rect;
mostrada =true;
}
completion:^(BOOL finished){}];
}
-(void)downSwipe:(UISwipeGestureRecognizer *)gesture
{
[UIView animateWithDuration:0.3
delay:0.3
options: UIViewAnimationCurveEaseOut
animations:^{
CGRect rect = self.view.frame;
rect.origin.y = self.view.frame.size.height-(self.view.frame.size.height/11);
self.usersView.frame = rect;
mostrada=false;
}
completion:^(BOOL finished){
}];
}
Upvotes: 4
Reputation: 7343
Change your method to this:
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *headerLabel = [[UILabel alloc]init];
headerLabel.tag = section;
headerLabel.userInteractionEnabled = YES;
headerLabel.backgroundColor = [UIColor salmonUser];
headerLabel.text = [NSString stringWithFormat:@"Interesteds"];
headerLabel.frame = CGRectMake(100, 0, 120,tableView.tableHeaderView.frame.size.height);
[headerLabel setTextAlignment:NSTextAlignmentCenter];
UISwipeGestureRecognizer *upRecognizer= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[upRecognizersetDirection:(UISwipeGestureRecognizerDirectionUp)];
UISwipeGestureRecognizer *downRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[downRecognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[headerLabel addGestureRecognizer:upRecognizer];
[headerLabel addGestureRecognizer:downRecognizer];
return headerLabel;
//return nil;
}
Upvotes: 0