OStack程序员社区-中国程序员成长平台

标题: ios - tinderSwipeCards 数组人口 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:30
标题: ios - tinderSwipeCards 数组人口

我正在使用 https://github.com/cwRichardKim/TinderSimpleSwipeCards .我在 viewController 中创建了一个数组,其中数据来自 JSON。我希望只有该数量的卡片将创建为我的 JSON 数组中的数据。请帮助我,我从过去 3 天开始这样做,但尚未成功。

这是我的 View Controller 中的数组

  [self.arrAllCards addObject:model];
                     //[self.arrExamplecards addObject:model.senderImage];

                     NSLog(@"%@",_arrAllCards);

                     //_draggableVC.allCards = _arrAllCards;


                     //[_draggableVC.exampleCardLabels isEqualToArray:_arrInitationsMainContainer];
                     if ([_arrAllCards count] >0) {

                         DraggableViewBackground *draggableBackground = [[DraggableViewBackground alloc]initWithFrame:self.invitationViewContainer.frame];


                        draggableBackground.exampleCardLabels = [NSMutableArray arrayWithArray:_arrAllCards];
                         draggableBackground.alpha = 0; //opti
                         [self.invitationViewContainer addSubview:draggableBackground];

                         [draggableBackground setNeedsDisplay];
                     }

而 DraggableViewBackground 是 cwRichardKim 提供的 View 类

   - (id)initWithFrameCGRect)frame 
{

self = [super initWithFrame:frame];
if (self) {
    [super layoutSubviews];
    [self setupView];

    exampleCardLabels =  [NSArray new];  //%%% placeholder for card-specific information
    loadedCards = [[NSMutableArray alloc] init];
    allCards = [[NSMutableArray alloc] init];
    cardsLoadedIndex = 0;
    [self loadCards];
}
return self;
     }

    //%%% sets up the extra buttons on the screen



 // to get rid of it (eg: if you are building cards from data from the internet)
      -(DraggableView *)createDraggableViewWithDataAtIndexNSInteger)index
      {

     //MGInvitationModel *model = self.exampleCardLabels[index];

        DraggableView *draggableView = [[DraggableView  alloc]initWithFrame:CGRectMake(0, -50, CARD_WIDTH,  CARD_HEIGHT)];//[[DraggableView  alloc]initWithFrame:CGRectMake((self.bounds.size.width)/2, (self.bounds.size.height)/2, CARD_WIDTH, CARD_HEIGHT)]; //[[DraggableView  alloc]initWithFrame:CGRectMake(0, -40, CARD_WIDTH, CARD_HEIGHT)];
     //draggableView.information.text = [exampleCardLabels objectAtIndex:index];        //%%% placeholder for card-specific information



     draggableView.delegate = self;
     draggableView.layer.cornerRadius = 10.0f;


    nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 280, 150, 50)];
     nameLabel.font = [UIFont fontWithName"Arial" size:12.0f];
     nameLabel.text = @"NIVESH";
     [nameLabel setTextAlignment:NSTextAlignmentLeft];
      nameLabel.textColor = [UIColor blackColor];

[draggableView addSubview:nameLabel];
return draggableView;



}




     //%%% loads all the cards and puts the first x in the "loaded cards" array
     -(void)loadCards
       {




if([exampleCardLabels count] > 0) {
    NSInteger numLoadedCardsCap =(([exampleCardLabels count] > MAX_BUFFER_SIZE)?MAX_BUFFER_SIZE:[exampleCardLabels count]);
    //%%% if the buffer size is greater than the data size, there will be an array error, so this makes sure that doesn't happen



    //%%% loops through the exampleCardsLabels array to create a card for each label.  This should be customized by removing "exampleCardLabels" with your own array of data
    for (int i = 0; i<[exampleCardLabels count]; i++) {
        DraggableView* newCard = [self createDraggableViewWithDataAtIndex:i];
        [allCards addObject:newCard];

        if (i<numLoadedCardsCap) {
            //%%% adds a small number of cards to be loaded
            [loadedCards addObject:newCard];
        }
    }



    //%%% displays the small number of loaded cards dictated by MAX_BUFFER_SIZE so that not all the cards
    // are showing at once and clogging a ton of data
    for (int i = 0; i<[loadedCards count]; i++) {
        if (i>0) {
            [self insertSubview:[loadedCards objectAtIndex:i] belowSubview:[loadedCards objectAtIndex:i-1]];
        } else {
            [self addSubview:[loadedCards objectAtIndex:i]];
        }
        cardsLoadedIndex++; //%%% we loaded a card into loaded cards, so we have to increment
    }
}else if ([exampleCardLabels count] == 0){

    [self removeFromSuperview];
    //[DraggableViewBackground.self removeFromSuperview];
    }
     }

     #warning include own action here!
     //%%% action called when the card goes to the left.
     // This should be customized with your own action
    -(void)cardSwipedLeftUIView *)card;
   {
//do whatever you want with the card that was swiped
//    DraggableView *c = (DraggableView *)card;

    [loadedCards removeObjectAtIndex:0]; //%%% card was swiped, so it's no       longer a "loaded card"

      if (cardsLoadedIndex < [allCards count]) { //%%% if we haven't reached the end of all cards, put another into the loaded cards
    [loadedCards addObject:[allCards objectAtIndex:cardsLoadedIndex]];
    cardsLoadedIndex++;//%%% loaded a card, so have to increment count
    [self insertSubview:[loadedCards objectAtIndexMAX_BUFFER_SIZE-1)]  belowSubview:[loadedCards objectAtIndexMAX_BUFFER_SIZE-2)]];
}
   }

      #warning include own action here!
   //%%% action called when the card goes to the right.
    // This should be customized with your own action
    -(void)cardSwipedRightUIView *)card
     {
//do whatever you want with the card that was swiped
//    DraggableView *c = (DraggableView *)card;

    [loadedCards removeObjectAtIndex:0]; //%%% card was swiped, so it's no  longer a "loaded card"

   if (cardsLoadedIndex < [allCards count]) { //%%% if we haven't reached the end of all cards, put another into the loaded cards
    [loadedCards addObject:[allCards objectAtIndex:cardsLoadedIndex]];
    cardsLoadedIndex++;//%%% loaded a card, so have to increment count
    [self insertSubview:[loadedCards objectAtIndexMAX_BUFFER_SIZE-1)] belowSubview:[loadedCards objectAtIndexMAX_BUFFER_SIZE-2)]];
}

  }

   //%%% when you hit the right button, this is called and substitutes the swipe
    -(void)swipeRight
      {
DraggableView *dragView = [loadedCards firstObject];
dragView.overlayView.mode = GGOverlayViewModeRight;
[UIView animateWithDuration:0.2 animations:^{
    dragView.overlayView.alpha = 1;
}];
[dragView rightClickAction];
   }

  //%%% when you hit the left button, this is called and substitutes the swipe
   -(void)swipeLeft
  {
DraggableView *dragView = [loadedCards firstObject];
dragView.overlayView.mode = GGOverlayViewModeLeft;
[UIView animateWithDuration:0.2 animations:^{
    dragView.overlayView.alpha = 1;
}];
[dragView leftClickAction];
  }

    -(void)swipeIgnore
 {
DraggableView *dragView = [loadedCards firstObject];
dragView.overlayView.mode = GGOverlayViewModeLeft;
[UIView animateWithDuration:0.2 animations:^{
    dragView.overlayView.alpha = 1;
}];
[dragView leftClickAction];


   }

   /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
   - (void)drawRectCGRect)rect
   {
  // Drawing code
   }
   */

@结束



Best Answer-推荐答案


你能像这样改变你的 -(void)loadCards 方法吗-

-(void)loadCards NSArray*) array {
   exampleCardLabels = array;

   //put here your existing code.
   //...
}

还在 DraggableViewBackground.h 文件中添加 -(void)loadCards :(NSArray*) array;

并注释 [self loadCards]; 来自 - (id)initWithFrame:(CGRect)frame 方法

然后像这样改变这个方法-

if ([_arrAllCards count] >0) {

                         DraggableViewBackground *draggableBackground = [[DraggableViewBackground alloc]initWithFrame:self.invitationViewContainer.frame];

                         [draggableBackground loadCards:_arrAllCards];
                         draggableBackground.alpha = 0; //opti
                         [self.invitationViewContainer addSubview:draggableBackground];

                         //[draggableBackground setNeedsDisplay];
                     }

关于ios - tinderSwipeCards 数组人口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40926460/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4