2016-03-30 8 views
1

を使用して動作していないここに私のGameViewController.mファイルがあります:UISwipeGestureカスタムUIViewの

- (void)viewDidLoad { 
    [super viewLoad]; 
    . 
    . 
    . 
    _board = [[TwinstonesBoardModel alloc] init]; 
    [_board setToInitialStateMain]; 
    TwinstonesStoneView* twinstonesBoard = [[TwinstonesStoneView alloc] 
             initWithMainFrame:CGRectMake(12, 160, 301.5, 302.5) 
             andBoard:_board]; 
    [self.view addSubview:twinstonesBoard]; 

    TwinstonesStonesView *stoneOne = [[TwinstonesStoneView alloc] init]; 
    TwinstonesStonesView *one = (TwinstonesStoneView*)stoneOne.stoneUnoView; 
    TwinstonesStonesView *stoneTwo = [[TwinstonesStoneView alloc] init]; 
    TwinstonesStonesView *two = (TwinstonesStoneView*)stoneTwo.stoneDueView; 

    UISwipeGestureRecognizer* swipeLeft = [[UISwipeGestureRecognizer alloc] 
             initWithTarget:self 
             action:@selector(swipeLeft:)]; 
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; 
    swipeLeft.numberOfTouchesRequired = 1; 
    [one addGestureRecognizer:swipeLeft]; 
    [two addGestureRecognizer:swipeLeft]; 

ここに関連するコードは、私のTwinstonesStoneView.mファイルにあります:

@implementation TwinstonesStoneView 
{ 
    NSMutableArray* _array; 
    NSMutableArray* _emptyArray; 
    CGRect _frame; 
    NSUInteger _column; 
    NSUInteger _row; 
    TwinstonesBoardModel* _board; 

    int _i; 
} 

- (id)initWithMainFrame:(CGRect)frame andBoard: 
            (TwinstonesBoardModel*)board 
{ 
    if (Self = [super initWithFrame:frame]) 
    { 
    float rowHeight = 49.0; 
    float columnWidth = 49.0; 
    float barrierHorizontalRowHeight = 12.5; 
    float barrierVerticalColumnWidth = 12.5; 

    for (int row = 0; row < 5; row++) 
    { 
     for (int col = 0; col < 5; col++) 
     { 
     TwinstonesStonesView* square = [[TwinstonesStoneView alloc] 
      initWithEmptyFrame:CGRectFrame(//spacial equations, not important) 
      column:col 
      row:row 
      board:board]; 

     BoardCellState state = [board cellStateAtColumn:col andRow:row]; 

     if (state == BoardCellStateStoneOne) { 
      // _stoneUnoView is a public property 
      // 'stoneOneCreation' creates a UIImageView of the stone 
      _stoneUnoView = [UIImageView stoneOneCreation]; 
      [self addSubview:square]; 
      [square addSubview:_stoneUnoView]; 
      [_array insertObject:_stoneUnoView atIndex:0]; 
     } else if (state == BoardCellStateStoneTwo) { 
      // same idea as above 
      _stoneDueView = [UIImageView stoneTwoCreation]; 
      [self addSubview:square]; 
      [square addSubview:_stoneDueView]; 
      [_array insertObject:_stoneDueView atIndex:1]; 
     } else { 
      // based on the 'init' method I write below, I assumed this 
      // would return an empty square cell 
      [self addSubview:square]; 
      [_emptyArray insertObject:square atIndex:_i]; 
      _i++; 
     } 
     } 
    } 
    self.backgroundColor = [UIColor clearColor]; 
    } 
    return self; 
} 

- (UIView*)stoneUnoView { 
    return _stoneUnoView; 
} 

- (UIView*)stoneDueView { 
    return _stoneDueView; 
} 

- (id)initWithEmptyFrame:(CGRect)frame 
        column:(NSUInteger)column 
        row:(NSUInteger)row 
        board:(TwinstonesBoardModel*)board 
{ 
    self = [super initWithFrame:frame]; 
    return self; 
} 

- (void)swipeLeft:(UIGestureRecognizer*)recognizer 
{ 
    NSLog(@"Swipe Left"); 
    UIView* view = recognizer.view; 
    [self move:CGPointMake(-1, 0) withView:view]; 
} 

- (void)move:(CGPoint)direction withView:view { 
    // whatever code I decide to put for stone movement 
} 

@end 

私は(おそらく)、不要な長さのために謝罪、私はちょうどカップルの日のこれを把握しようとしていると運がなかった。

  1. setInititalStateMainがGameViewController.mで
  2. が、私は「stoneUnoView」と 'をキャプチャしようとしている5x5のグリッドに二つの石の配置を設定します。ここで私は何をしようとしているの箇条書きをです(TwinstonesStoneView.mファイルで設定された)「stoneDueView」プロパティを取得し、スワイプジェスチャーを与え、TwinstonesStoneView.mで提供されるメソッドを使用してスクロールジェスチャーを操作します。
  3. ビューが多すぎますか?キャッチは、私がプログラムを実行するときに私が私のIPhoneで見ることができるものに関してすべてが機能するということです。私の画面には石が現れますが、それらとやりとりしようとすると、コンソールに「NSLog」というメッセージも表示されません。
  4. 'stoneOneCreation'メソッド(と... 2つ)はUIImageViewのものですが、わかりますように、それらをUIViewポインタに格納します。
  5. 私は '[one setUserInteractionEnabled:YES]'(と... two)も使用しましたが、どちらも役に立ちませんでした。
  6. ジェスチャーレコグナイザーをself.viewに追加すると、すべてが機能します(石、ゲームボード、その他のグラフィックの表示が表示され、画面の任意の部分とやり取りするとコンソールに出力されます)。 ....石に特有の相互作用ではない)。

これをすべてお守りいただきありがとうございます。誰かが間違っていることが分かっている場合、これは本当に役に立ちます。 PS:すべてのファイル#importは正しいので、問題はありません。

私はXCodeの7、Objective-C言語を使用して、そしてあなたがのUIViewの異なるインスタンスにスワイプジェスチャー認識器の異なるインスタンスを追加する必要のiOS

Anthony 

答えて

0

のために開発しています。

UISwipeGestureRecognizer* swipeLeft1 = [[UISwipeGestureRecognizer alloc] 
            initWithTarget:self 
            action:@selector(swipeLeft:)]; 
swipeLeft1.direction = UISwipeGestureRecognizerDirectionLeft; 
swipeLeft1.numberOfTouchesRequired = 1; 
[one addGestureRecognizer:swipeLeft1]; 

UISwipeGestureRecognizer* swipeLeft2 = [[UISwipeGestureRecognizer alloc] 
            initWithTarget:self 
            action:@selector(swipeLeft:)]; 
swipeLeft2.direction = UISwipeGestureRecognizerDirectionLeft; 
swipeLeft2.numberOfTouchesRequired = 1; 
[two addGestureRecognizer:swipeLeft2]; 

私はgestire認識器は、読み取り専用view性質を持っているので、それがあると仮定します。

+0

残念ながら応答はありません。参照が多すぎますか?私がそれをマップすると、参考文献は健全であるように見えます。私はすべてのファイルを正しくインポートしました。サブビューの下に埋め込まれているサブビューのようなものはありますか?彼らは私の指が相互作用している一番最初の「表面」の下に何らかの形で埋め込まれているので、自分のカスタムビューとやり取りできないのはなぜですか? –

+0

読み込み専用のビュープロパティはどういう意味ですか?私が変えなければならないことはありますか? –

+0

私は認識装置が1つのビューにのみ添付できることを意味します。 –

0

こんにちは、ユーザーインタラクションが無効になっている可能性があります。そのビューでは、userInteractionEnable = YESのビューに対してのみジェスチャーが動作します。

関連する問題