分页控制 (是pagecontrol不是pagecontroller)和手势操作

本文介绍如何在iOS应用中实现左右滑动切换不同城市天气的功能,包括使用UISwipeGestureRecognizer处理滑动事件及更新UIPageControl显示当前页码。

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

    self.recognizerRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    self.recognizerLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    
    [self.recognizerLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.view addGestureRecognizer:self.recognizerLeft];
    
    [self.recognizerRight setDirection:UISwipeGestureRecognizerDirectionRight];


<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(0, 175, 202);"><pre name="code" class="objc"> self.pg = [[UIPageControl alloc]initWithFrame:CGRectMake(viewBounds.origin.x, viewBounds.origin.y, viewBounds.size.width,
                                                             60)];
    self.pg.backgroundColor = [UIColor grayColor];
    self.pg.alpha = 0.1;
    self.pg.numberOfPages = 5;
    self.pg.currentPage = 0;
      [self.view addSubview:self.pg];






-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recongnizer
{
     int page = self.pg.currentPage ;
    if (recongnizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"left");
       
        page--;
        if (page<0) return;
        else
            self.pg.currentPage = page;
        
    }
    if (recongnizer.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"right");
        page++;
        if (page >4) return;
        else
            self.pg.currentPage = page;
        
    }
    
    switch (self.pg.currentPage) {
        case 0:
            [self getWeatherDataByCityname:@"南京"];
            break;
        case 1:
            [self getWeatherDataByCityname:@"北京"];
            break;
        case 2:
            [self getWeatherDataByCityname:@"上海"];
            break;
        case 3:
            [self getWeatherDataByCityname:@"广州"];
            break;
        case 4:
            [self getWeatherDataByCityname:@"深圳"];
            break;
        default:
            break;
    }
    
    [self setTheCurrentMessage:self.weatherCurrentData];
    [self.tableView reloadData];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值