1

我正在尝试添加UISwipeGestureRecognition到自定义视图。我想做与 IOS 通知相同的操作,但以另一种方式。我以前用 做过tapGesture,但是当我用滑动添加尝试时,它不起作用。

我正在使用此代码。这是一个隐藏的 tableView,当我swipeUp用一根手指做 a 时会显示它。

我现在的问题是视图上升但在主视图后面。

 -(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){

                         }];
    }

}
4

2 回答 2

4

使用此代码:

 -(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;

}

我使用了两种不同的方法来分别对上下滑动upSwipedownSwipe. 这将简化代码。

-(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){

                         }];
}
于 2013-06-11T21:16:30.607 回答
0

将您的方法更改为:

-(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;
}
于 2013-06-11T19:58:31.220 回答