2015-02-04 2 views
0

Я создаю приложение (шутка), основанное на прокрутке карты tinder, что позволяет мне вести счет Magic the Gathering Game.Objective-C Index Array

В прошлом я просто сделал это, увеличивая и уменьшая числа в виде строк. Это отлично работает.

Теперь я использую Richard Kim's TinderSimpleSwipeCards, который загружает карты из массива целыми числами. Существует метод, вызываемый для загрузки следующего числа в массиве при прокрутке карты. Будучи новичком в Objective-C, я потратил час, пытаясь перепрофилировать массив для моих нужд. Но я застрял.

Поскольку игроки могут получить и потерять жизнь в Magic The Gathering, моя цель состоит в том, чтобы иметь возможность первой загрузки 20, затем проведите пальцем вправо, чтобы увеличить число, и проведите влево, уменьшив число (до 0).

Моя первая идея состояла в том, чтобы создать массив с 40 числами, а затем запустить метод с индексом 20, который будет загружать карту с целым числом 20. Затем, вызывая метод в любом направлении, я мог бы загружать карты в положительном или отрицательном направлении.

Но это не сработало.

Это файл Richard's DraggableViewBackground.m. Он вызывает метод in - (void) loadCards, где я пытаюсь манипулировать номером индекса.

Я уверен, что это невозможно сделать с помощью массива, просто начиная с индекса в середине, затем запуская цикл For в любом направлении (i-1) или (i + 1). Когда я вызываю объект по определенному номеру индекса, метод будет продолжать создавать карты только в этом индексе (например, если я создаюDraggableViewWithDataAtIndex: 4, новая карта из 16 - пятый элемент в массиве будет просто продолжать загружать карта с 16).

Может кто-нибудь подтвердить это может или не может быть сделано с помощью одного массива? Если это возможно, может ли кто-нибудь угодить привести меня в направлении того, как загрузить карту из правильного индекса?

// 
// DraggableViewBackground.m 
// testing swiping 
// 
// Created by Richard Kim on 8/23/14. 
// Copyright (c) 2014 Richard Kim. All rights reserved. 
// 

#import "DraggableViewBackground.h" 

@implementation DraggableViewBackground{ 
    NSInteger cardsLoadedIndex; //%%% the index of the card you have loaded into the loadedCards array last 
    NSMutableArray *loadedCards; //%%% the array of card loaded (change max_buffer_size to increase or decrease the number of cards this holds) 

    UIButton* menuButton; 
    UIButton* messageButton; 
    UIButton* checkButton; 
    UIButton* xButton; 
} 
//this makes it so only two cards are loaded at a time to 
//avoid performance and memory costs 
static const int MAX_BUFFER_SIZE = 2; //%%% max number of cards loaded at any given time, must be greater than 1 
static const float CARD_HEIGHT = 200; //%%% height of the draggable card 
static const float CARD_WIDTH = 290; //%%% width of the draggable card 

@synthesize exampleCardLabels; //%%% all the labels I'm using as example data at the moment 
@synthesize allCards;//%%% all the cards 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [super layoutSubviews]; 
     [self setupView]; 
     exampleCardLabels = [[NSArray alloc]initWithObjects:@"20",@"19",@"18",@"17",@"16",@"15",@"14",@"13",@"12",@"11",@"10",@"9",@"8",@"7",@"6",@"5",@"4",@"3",@"2",@"1",@"DEATH", nil]; //%%% 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 
-(void)setupView 
{ 
#warning customize all of this. These are just place holders to make it look pretty 
    self.backgroundColor = [UIColor colorWithRed:.92 green:.93 blue:.95 alpha:1]; //the gray background colors 
    menuButton = [[UIButton alloc]initWithFrame:CGRectMake(17, 34, 22, 15)]; 
    [menuButton setImage:[UIImage imageNamed:@"menuButton"] forState:UIControlStateNormal]; 
    messageButton = [[UIButton alloc]initWithFrame:CGRectMake(284, 34, 18, 18)]; 
    [messageButton setImage:[UIImage imageNamed:@"messageButton"] forState:UIControlStateNormal]; 
    xButton = [[UIButton alloc]initWithFrame:CGRectMake(60, 485, 59, 59)]; 
    [xButton setImage:[UIImage imageNamed:@"xButton"] forState:UIControlStateNormal]; 
    [xButton addTarget:self action:@selector(swipeLeft) forControlEvents:UIControlEventTouchUpInside]; 
    checkButton = [[UIButton alloc]initWithFrame:CGRectMake(200, 485, 59, 59)]; 
    [checkButton setImage:[UIImage imageNamed:@"checkButton"] forState:UIControlStateNormal]; 
    [checkButton addTarget:self action:@selector(swipeRight) forControlEvents:UIControlEventTouchUpInside]; 
    [self addSubview:menuButton]; 
    [self addSubview:messageButton]; 
    [self addSubview:xButton]; 
    [self addSubview:checkButton]; 
} 

#warning include own card customization here! 
//%%% creates a card and returns it. This should be customized to fit your needs. 
// use "index" to indicate where the information should be pulled. If this doesn't apply to you, feel free 
// to get rid of it (eg: if you are building cards from data from the internet) 
-(DraggableView *)createDraggableViewWithDataAtIndex:(NSInteger)index 
{ 
    DraggableView *draggableView = [[DraggableView alloc]initWithFrame:CGRectMake((self.frame.size.width - CARD_WIDTH)/2, (self.frame.size.height - CARD_HEIGHT)/2, CARD_WIDTH, CARD_HEIGHT)]; 
    draggableView.information.text = [exampleCardLabels objectAtIndex:index]; //%%% placeholder for card-specific information 
    draggableView.delegate = self; 
    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 
     } 
    } 
} 

#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]; 
} 

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

@end 
+0

Правильно, поэтому я не уверен, что полностью понимаю ваш вопрос, но я думаю, что вы боретесь с «exampleCardLabels», из которого извлекается информация (первая карта получает информацию по индексу 0). кажется, вам это действительно не нужно, поэтому я бы попытался добавить карту без использования exampleCardLabels – cwRichardKim

ответ

1

Если я правильно понял вас, массив не нужен. Я бы сохранил текущий индекс карт в целочисленном значении и метод для увеличения и уменьшения его путем проверки границ. Существует уже Ивар NSInteger cardsLoadedIndex;

В моей opionion метод, как это будет работать:

- (NSUInteger) getIndexDirectionRight:(BOOL)direction{ 
    if (direction){ 
     return (cardsLoadedIndex < MAX_VALUE) ? cardsLoadedIndex + 1 : 0; 
    }else{ 
     return (cardsLoadedIndex > 0) ? cardsLoadedIndex - 1 : MAX_VALUE; 
    } 
} 

И вы можете вызвать метод в swipeRight или swipeLeft и инициализации cardsLoadedIndex 20 в методе initWithFrame (так как он уже установлен в 0).