我正在使用https://github.com/cwRichardKim/TinderSimpleSwipeCards。我已经在viewController中创建了一个Array,其中的数据来自JSON。我希望仅将一定数量的卡创建为JSON数组中的数据。请帮助我,我从最近3天开始这样做,但尚未成功。
这是我的视图控制器中的数组
[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)initWithFrame:(CGRect)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 *)createDraggableViewWithDataAtIndex:(NSInteger)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)cardSwipedLeft:(UIView *)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 objectAtIndex:(MAX_BUFFER_SIZE-1)] belowSubview:[loadedCards objectAtIndex:(MAX_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)cardSwipedRight:(UIView *)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 objectAtIndex:(MAX_BUFFER_SIZE-1)] belowSubview:[loadedCards objectAtIndex:(MAX_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)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@结束
最佳答案
您可以像这样更改-(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];
}