为什么我的UIGestureRecognizer滑动不起作用? Xcode中

时间:2016-03-30 00:35:37

标签: ios objective-c xcode uiswipegesturerecognizer

这是我的GameViewController.m文件:

    - (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设置5x5网格中两块宝石的位置         2.在GameViewController.m中,我正在尝试捕获'stoneUnoView'和 'stoneDueView'属性(在TwinstonesStoneView.m文件中设置),为他们提供滑动手势,并使用TwinstonesStoneView.m中提供的方法与它们进行交互。         我的观点太多了吗?问题在于,当我运行程序时,一切都可以在我的IPhone上看到。石头出现在我的屏幕上,但当我尝试与它们交互时,甚至没有“NSLog”消息显示在控制台中。         4.“stoneOneCreation”方法(和......两个)是UIImageView的,但正如您所看到的,我将它们存储在UIView指针中。
        5.我也使用'[one setUserInteractionEnabled:YES]'(和......两个),但这也没有帮助。         6.如果我将手势识别器添加到self.view,一切正常(石头,游戏板和其他图形的显示出现,当我与屏幕的任何部分交互时,我将指示输出到控制台... ......不是石头特定的互动。)

非常感谢你忍受所有这一切,如果有人知道什么是错的话,这真的会有所帮助。 PS:所有文件#import都是正确的,所以这不是问题。

我正在使用XCode 7,Objective-C语言,以及为iOS开发

  • 安东尼

2 个答案:

答案 0 :(得分:0)

引起我注意的一件事是我不认为你可以为多个视图添加相同的手势识别器。我怀疑,最多只有最后一个视图实际上是接收它。

答案 1 :(得分:0)

尝试这个,但我不确定尝试这个,只需创建2个滑动手势 在GameViewController.m

 - (void)viewDidLoad {
    [super viewLoad];
    //.... other code
    //comment below line 
   // UISwipeGestureRecognizer* swipeLeft = [[UISwipeGestureRecognizer alloc]
                                     //initWithTarget:self //setting self is the problem is the problem
                                     //action:@selector(swipeLeft:)];
   UISwipeGestureRecognizer* swipeLeft = [[UISwipeGestureRecognizer alloc]
                                initWithTarget:stoneOne //set target will be one
                                     action:@selector(swipeLeft:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    swipeLeft.numberOfTouchesRequired = 1;

    [one addGestureRecognizer:swipeLeft];

    UISwipeGestureRecognizer* swipeLeft_2 = [[UISwipeGestureRecognizer alloc]
                             initWithTarget:stoneTwo //this will be two
                             action:@selector(swipeLeft:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    swipeLeft.numberOfTouchesRequired = 1;

    [two addGestureRecognizer:swipeLeft_2]; //set the gesture
 }

您将手势设置为self,这意味着操作会发送到GameViewController.m,但我们希望操作位于TwinstonesStoneView.m,因此请更改目标以查看TwinstonesStoneView 。如果是图像视图,您正在添加手势,则为每个图像视图启用用户交互setUserInteractionEnabled:

试试吧