UISwipeGestureRecognizer (侧边栏)

本文介绍了如何在iOS应用中实现滑动手势控制视图切换的功能,包括左右滑动触发不同的视图变化,并通过动画平滑过渡实现更好的用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- (void)creatSwipeGesture {

    UISwipeGestureRecognizer *swipeGestureRecognizerLeft = [[UISwipeGestureRecognizer allocinit];

    [swipeGestureRecognizerLeft addTarget:self action:@selector(gestureRecognizerHandleLeft:)];

    [swipeGestureRecognizerLeft setDirection:UISwipeGestureRecognizerDirectionLeft];

    [self.view addGestureRecognizer:swipeGestureRecognizerLeft];

    

    UISwipeGestureRecognizer *swipeGestureRecognizerRight = [[UISwipeGestureRecognizer allocinit];

    [swipeGestureRecognizerRight addTarget:self action:@selector(gestureRecognizerHandleRight:)];

    [swipeGestureRecognizerRight setDirection:UISwipeGestureRecognizerDirectionRight];

    [self.view addGestureRecognizer:swipeGestureRecognizerRight];

    

}

- (void)gestureRecognizerHandleLeft:(UISwipeGestureRecognizer *)gesture {

    CGRect tableViewFrame = _tabelView.frame;

    tableViewFrame.origin.x = 0;

    CGRect frame = _drawerView.frame;

    frame.origin.x = -100;

    [UIView animateWithDuration:0.5 animations:^{

        _tabelView.frame = tableViewFrame;

        _drawerView.frame = frame;

    }];

}

- (void)gestureRecognizerHandleRight:(UISwipeGestureRecognizer *)gesture {

    CGRect tableViewFrame = _tabelView.frame;

    tableViewFrame.origin.x = 100;

    CGRect frame = _drawerView.frame;

    frame.origin.x = 0;

    [UIView animateWithDuration:0.5 animations:^{

        _tabelView.frame = tableViewFrame;

        _drawerView.frame = frame;

    }];

    

}

- (void)creatDrawerView {

    _selectString = @"亚洲";

    _drawerView = [[UIView allocinitWithFrame:CGRectMake(-1000100ScreenSize.height64)];

    

    UILabel *backLabel = [[UILabel allocinitWithFrame:CGRectMake(00100ScreenSize.height64)];

    backLabel.backgroundColor = [UIColor lightGrayColor];

    backLabel.alpha = 0.5;

    [_drawerView addSubview:backLabel];

    

    NSArray *countryNames = @[@"亚洲",@"欧洲",@"北美洲",@"南美洲",@"大洋洲",@"非洲",@"专题"];

    for (int i = 0; i < countryNames.count; i++) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

        button.frame = CGRectMake(0, (61)*i, 10060);

        button.backgroundColor = [UIColor clearColor];

        [button setTitle:countryNames[i] forState:UIControlStateNormal];

        UIImageView *lineView = [[UIImageView allocinitWithFrame:CGRectMake(5, (60+1)*i, 901)];

        lineView.image = [UIImage imageNamed:@"cut_off_rule"];

        [_drawerView addSubview:lineView];

        [button setTitleColor:[UIColor blackColorforState:UIControlStateNormal];

        button.titleLabel.font = [UIFont systemFontOfSize:17];

        button.tag = 101+i;

        [button addTarget:self action:@selector(drawerButtonClick:) forControlEvents:UIControlEventTouchUpInside];

        [_drawerView addSubview:button];

    }

    [self.view addSubview:_drawerView];

}

 

- (void)drawerButtonClick:(UIButton *)button {

    [_currentDataArr removeAllObjects];

    [_cellDataArr removeAllObjects];

    [_countryNameArr removeAllObjects];

    _selectString = button.currentTitle;

    

    for (GuideModel *model in _dataArr) {

        //亚洲 欧洲 ...

        if ([model.category_title isEqualToString:_selectString]) {

            //NSLog(@"****%@",_selectString);

            //NSLog(@"%@",model);

            [_currentDataArr addObject:model];

        }

    }

    for (int i = 0; i < _currentDataArr.count; i++) {

        GuideModel *model = _currentDataArr[i];

        //不包含则添加 印度 日本 ...

        if(![_countryNameArr containsObject:model.country_name_cn]){

            //NSLog(@"%@",model.country_name_cn);

            [_countryNameArr addObject:model.country_name_cn];

        }

    }

    for (int i = 0; i < _countryNameArr.count; i++) {

        NSMutableArray *tmpArr = [[NSMutableArray allocinit];

        for (GuideModel *model in _currentDataArr) {

            

            if ([model.country_name_cn isEqualToString:_countryNameArr[i]]) {

                //巴西 秘鲁 . . .

                //NSLog(@"%@",model.country_name_cn);

                [tmpArr addObject:model];

            }

        }

        

        [_cellDataArr addObject:tmpArr];

    }

    [_tabelView reloadData];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值