Ios 左右滑动以加载项目

Ios 左右滑动以加载项目,ios,uigesturerecognizer,Ios,Uigesturerecognizer,我有一个数组,它包含10个项目,分别是1,2,3,4,5,6,7,8,9,10。当我加载页面时,项目位于3。因此,当用户向左滑动时,它将变为4,向右滑动时,它将变为2。我可以知道怎么做吗 newsID=[[NSMutableArray alloc]init]; [newsID addObject:@"1"]; [newsID addObject:@"2"]; [newsID addObject:@"3"]; [newsID addObject:@"4"]; [newsID addObject:@

我有一个数组,它包含10个项目,分别是1,2,3,4,5,6,7,8,9,10。当我加载页面时,项目位于3。因此,当用户向左滑动时,它将变为4,向右滑动时,它将变为2。我可以知道怎么做吗

newsID=[[NSMutableArray alloc]init];
[newsID addObject:@"1"];
[newsID addObject:@"2"];
[newsID addObject:@"3"];
[newsID addObject:@"4"];
[newsID addObject:@"5"];
[newsID addObject:@"6"];
[newsID addObject:@"7"];
[newsID addObject:@"8"];
[newsID addObject:@"9"];
[newsID addObject:@"10"];
UISwipeGestureRecognizer * swipeleft=
    [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeleft:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];
UISwipeGestureRecognizer * swiperight=
    [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swiperight:)];
swiperight.direction=UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperight];

下面是一个处理滑动手势的简单示例。在这里,我采取一个UILabel和改变它的文本,同时刷卡

- (void)viewDidLoad 
  {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    _lblText.text=@"3";
    _lblText.textAlignment=NSTextAlignmentCenter;


    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeRight];


    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeLeft];

}

-(void)handleRightSwipe
{

    if ([_lblText.text intValue]>1)
    {
        _lblText.text=[NSString stringWithFormat:@"%d",[_lblText.text intValue]-1];
    }
    else
    {
        return;
    }
}

-(void)handleLeftSwipe
{

    if ([_lblText.text intValue]<10)
    {
        _lblText.text=[NSString stringWithFormat:@"%d",[_lblText.text intValue]+1];
    }
    else
    {
        return;
    }

}
-(void)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
_lblText.text=@“3”;
_lblText.textAlignment=NSTextAlignmentCenter;
UISweepGestureRecognizer*swipeRight=[[UISweepGestureRecognizer alloc]initWithTarget:自操作:@selector(HandlerRightSwipe)];
swipeRight.direction=UISWipGestureRecognitizerDirectionRight;
[self.view addgesturecognizer:swipeRight];
UISweepGestureRecognizer*swipeLeft=[[UISweepGestureRecognizer alloc]initWithTarget:自操作:@selector(handleLeftSwipe)];
swipelft.direction=uiswipgesturerecognizerDirectionLeft;
[self.view addgesturecognizer:swipleft];
}
-(无效)扶手刷
{
如果([\u lblText.text intValue]>1)
{
_lblText.text=[NSString stringWithFormat:@“%d”,[[u lblText.text intValue]-1];
}
其他的
{
返回;
}
}
-(无效)手摇抹布
{
如果([\u lblText.text intValue]请尝试此操作。
我使用一个UILabel并在刷卡时更改其文本。使用一个int变量跟踪刷卡

- (void)viewDidLoad 
{
   [super viewDidLoad];
   newIdArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
   num_index = 3;
   ValueLabel.text = [newIdArray objectAtIndex:num_index];
   leftGesture = [[UISwipeGestureRecognizer alloc]init];
   leftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
   [leftGesture addTarget:self action:@selector(PerformSwipegesture:)];
   [self.view addGestureRecognizer:leftGesture];
   rightGesture = [[UISwipeGestureRecognizer alloc]init];
   rightGesture.direction = UISwipeGestureRecognizerDirectionRight;
   [rightGesture addTarget:self action:@selector(PerformSwipegesture:)];
   [self.view addGestureRecognizer:rightGesture]; 
} 


 - (IBAction)PerformSwipegesture:(UISwipeGestureRecognizer *)sender
 {
     if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
     {
       if (num_index <9)
       {
         num_index = num_index +1;
         ValueLabel.text = [newIdArray objectAtIndex:num_index];
       }
     }
     else if (sender.direction == UISwipeGestureRecognizerDirectionRight)
     {
         if (num_index >0) 
         {
            if (num_index >1)
              num_index = num_index -2;
            else
              num_index = num_index -1;
            ValueLabel.text = [newIdArray objectAtIndex:num_index];
         }
     }
}
-(void)viewDidLoad
{
[超级视图下载];
newIdArray=@[“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10];
num_指数=3;
ValueLabel.text=[newIdArray objectAtIndex:num_index];
LeftSignature=[[UISweepGestureRecognitor alloc]init];
LeftSignature.direction=UISweepGestureRecognitizerDirectionLeft;
[左手势添加目标:自我操作:@selector(performsweepprotate:)];
[self.view addGestureRecognitor:LeftSignature];
RightSignature=[[UISweepGestureRecognitor alloc]init];
rightspirate.direction=UISweepGestureRecognitizerDirectionRight;
[右手势添加目标:自我操作:@selector(performsweepprotate:)];
[self.view addgestureRecognitor:Right手势];
} 
-(iAction)PerformSweep手势:(UISweepGestureRecognitor*)发送方
{
if(sender.direction==UISWIPgestureRecognitizerDirectionLeft)
{
if(num_索引0)
{
如果(数值索引>1)
num_index=num_index-2;
其他的
num_index=num_index-1;
ValueLabel.text=[newIdArray objectAtIndex:num_index];
}
}
}

你知道你可以说
newsID=@[@“1”、@“2”、@“3”、@“4”、@“5”、@“6”、@“7”、@“8”、@“9”、@“10”];
?因此,无论如何,你需要实现
swipeleft:
swiperight:
方法,以便每个方法都“转到”数组中的上一项或下一项-无论“转到什么”“应该对你很重要。@matt mean将转到下一个或上一个。”