2017-12-06 2 views
1

私は、ユーザーが画面上で指をドラッグして点をたどらなければならないアプリケーションを作ろうとしています。ユーザーがドットに達するたびに、次のポイントに移動します(合計で5つの等距離点があり、特定の座標があります)。ボタンを画面の周りに動かし、指のジェスチャーの精度を記録します。

フィンガーによってトレースされたパスの長さは、2つのドットごとに記録され、アレイに格納され、その値は最終的に平均化されます(理想的なパス、最短のパスとのパスを比較します) 。

-(IBAction)button1Clicked:(UIButton *)sender { 
    CGRect buttonFrame = sender.frame; 

    int dotX = arc4random() % (int)(1000 - buttonFrame.size.width); 
    int dotY = arc4random() % (int)(self.view.frame.size.height - buttonFrame.size.height); 

    buttonFrame.origin.x = dotX; 
    buttonFrame.origin.y = dotY; 
    [sender setFrame:buttonFrame]; 
    NSLog(@"x = %f, y = %f", self.button1.frame.origin.x, self.button1.frame.origin.y); 
} 

私はプログラム的に配列に格納され、いくつかのボタンを使用してボタンを追加しようとしましたが、また、直接ストーリーボードから、と私はまだのための最善の方法だろうどの考え出したていませんそれが意味することをするアプリ。

私のコーディングの知識はまだ非常に貧しいされて、私はそれが画面内を移動するために(内部のタッチダウン)をクリックする必要があり、ドット形状のボタンでアプリを作るために管理し、いつましたユーザーはそれを押して、私のiPadの画面上のランダムなポイントに移動します。代わりに、コードが実行を停止して結果が平均化されるまで(コードを "for"ループの内側に置く必要がある)、ジェスチャはタッチダウンするのではなく、内側をドラッグして触れてください。 誰かが私にいくつかのヒントを与えるのを手伝うことができれば素晴らしいだろう!

答えて

1

ボタンを1つだけ追加する必要があると思います。ユーザーにナビゲートさせたい各点の値を含むNSMutableDictionaryの配列を作成します。これには、x座標とy座標、この座標と最後の座標の間の最小距離、およびこの座標と最後の座標の間の実際の移動距離座標。以下の設定は、あなたのために働く必要がありますが、それは未テストです:あなたの座標セットの保存

を、あなたの座標セットを格納する方法は、あなたが持っているどのように多くの座標のセットに依存し、どのように多くの座標はそれぞれです。いくつかの座標セットしかない場合は、私がここにあるようなコードでそれらを書くことができます。多くの座標セットがある場合は、バンドルに格納するファイルを作成し、必要に応じてアクセスし、このプロセスに関連する多くのSOの質問が既に存在する可能性があります。ビューが最初のタッチを検出すると

#import "ViewController.h" 

@interface ViewController(){ 
    NSMutableArray *values; 
    NSInteger buttonCount; 
    __weak IBOutlet UIButton *button; 
    BOOL hasStarted; 
} 

@end 

@implementation ViewController 

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

    UIGestureRecognizer *gestureRecognizer = [[UIGestureRecognizer alloc] init]; 
    [gestureRecognizer setEnabled:YES]; 
    [gestureRecognizer setDelegate:self]; 
    [self.view addGestureRecognizer:gestureRecognizer]; 

    button.hidden = true; 

    buttonCount = 0; 
    values = [[NSMutableArray alloc]init]; 
    [self generateValuesArrayWithCoordiantes]; 
} 

-(void)generateValuesArrayWithCoordiantes{ 
    // for every number between 0 and 20, create an nsdictionary with your desired values for x and y 
    CGPoint lastCoord = CGPointZero; 
    // you need to have your set of coordinates stored somewhere, so that they can put into the button values dictionary 
    // here is an array of dictionary, with an x and y number for each array value, you will require 20 rows, one for each set of coords 

    NSArray *coords = @[@{@"x":@317, @"y":@100}, 
         @{@"x":@200, @"y":@250}, 
         @{@"x":@120, @"y":@400}, 
         @{@"x":@80, @"y":@380}]; 

    for (int i = 0; i<20; i++) { 
     NSMutableDictionary *buttonValues = [[NSMutableDictionary alloc]init]; 

     // get the required coords from your coords array 
     NSDictionary *currentCoord = [coords objectAtIndex:i]; 
     [buttonValues setValue:[currentCoord valueForKey:@"x"] forKey:@"x"]; 
     [buttonValues setValue:[currentCoord valueForKey:@"y"] forKey:@"y"]; 

     // set the distance travelled to 0. this will be updated as your view detects touches moved 
     [buttonValues setValue:[NSNumber numberWithInt:0] forKey:@"distanceTravelled"]; 

     if (i>0) { 
      // calculate the minimum distance required to get from this point to the previous point. not required for the first point 
      CGFloat minDistance = [self distanceBetween:CGPointMake([[currentCoord valueForKey:@"x"]floatValue], [[currentCoord valueForKey:@"y"]floatValue]) and:lastCoord]; 
      [buttonValues setValue:[NSNumber numberWithFloat:minDistance] forKey:@"minDistance"]; 
     } 
     lastCoord = CGPointMake([[currentCoord valueForKey:@"x"]floatValue], [[currentCoord valueForKey:@"y"]floatValue]); 
     [values addObject:buttonValues]; 
    } 
} 


-(IBAction)button1Clicked:(UIButton *)sender { 
    if (!hasStarted) { 
     NSLog(@"It Has Begun"); 
     hasStarted = true; 
    } 
    buttonCount = buttonCount + 1; 
    if (buttonCount == 20){ 
     // The button has been clicked 20 times, compare the minDistance of the dictionaries, to the actualDistance of the dictionaries 
     NSLog(@"We are done"); 
     return; 
    } 
    //get the last frame of the button 
    CGRect lastFrame = sender.frame; 

    NSMutableDictionary *nextButtonValues = [values objectAtIndex:buttonCount]; 

    CGRect nextFrame = CGRectMake([[nextButtonValues valueForKey:@"x"]floatValue], [[nextButtonValues valueForKey:@"y"]floatValue], lastFrame.size.width, lastFrame.size.height); 

    [sender setFrame:nextFrame]; 
} 

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 
    button.hidden = false; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    // get this and last touch location and the distance moved between them 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 
    CGPoint lastLocation = [touch previousLocationInView:self.view]; 
    CGFloat distanceMoved = [self distanceBetween:touchLocation and:lastLocation]; 

    //NSLog(@"MOVED %.2f", distanceMoved); 

    // get the current course from values, and update the distanceTravelled to include the distance moved 
    NSMutableDictionary *currentCourse = [values objectAtIndex:buttonCount]; 
    CGFloat courseTravelled = [[currentCourse valueForKey:@"distanceTravelled"]floatValue]; 
    [currentCourse setValue:[NSNumber numberWithFloat:courseTravelled + distanceMoved] forKey:@"distanceTravelled"]; 

    // detect if the touch is inside the button frame 
    for (UITouch *t in touches) { 
     CGPoint touchPoint = [t locationInView:self.view]; 

     CGPoint testPoint = [self.view convertPoint:touchPoint toView:button]; 
     if ([button pointInside:testPoint withEvent:event]) { 
      [self button1Clicked:button]; 
      return; 
     } 
    } 
} 

-(CGFloat)distanceBetween:(CGPoint)point1 and:(CGPoint)point2{ 
    CGFloat xDist = (point1.x - point2.x); 
    CGFloat yDist = (point1.y - point2.y); 
    return sqrt((xDist * xDist) + (yDist * yDist)); 
} 

@end 

は、ボタンが再表示されます、そしてゲームが

+0

ありがとう開始されます!これは私を助けてくれました!唯一残っている問題は、各ドットの座標がランダムではなく、定義されているのが好きではないということです。私が意味することは、一定の回数の反復に達するまで、ドットが固定されたパス(私が選んだポイントのセットによって定義される)に従って、ゲームの全期間中それに固執するということです。また、配列に値が追加されていないため(NSLog(@ "%lu"、unsigned long)values.countを追加すると値を数える "if"ステートメントが機能していないようです。私がドットを何回も押しても、コードは1になる。 –

+0

1のカウントの理由は、各ボタンタップでvalues配列を 'init'する可能性が高いということです。これは、空を宣言していることを意味しますボタンをタップするたびに配列を返します。値の配列はinterfaceと宣言し、viewDidLoadでは 'init'を使います。ランダムではなく座標のセットを生成するには、必要なボタンのタップごとに辞書を作成し、x、y、minDistance、 actualDistance、それぞれの値をviewDidLoadで値に追加します。すべてのボタンタップでインクリメントするintを宣言して、値配列のインデックスを知ることができます。後で必要に応じて回答を更新することができます – Shayno

+0

ありがとうございました!はい、私は絶対初心者であり、まだObj-C配列や辞書に慣れていないので、できるだけ早く回答を更新できるのであれば、本当に役に立ちます。私はあなたの指示に従うことができます! –

関連する問題