• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - tinderSwipeCards 数组人口

[复制链接]
菜鸟教程小白 发表于 2022-12-12 12:30:05 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap