UISwipeGesture使用自定义UIView

时间:2016-03-30 03:30:28

标签: 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中,我试图捕捉&#39; stoneUnoView&#39;和&#39; stoneDueView&#39;属性(在TwinstonesStoneView.m文件中设置),为它们提供滑动手势,并使用TwinstonesStoneView.m中提供的方法与它们进行交互。
  3. 我的观点太多了吗?问题在于,当我运行程序时,一切都能按照我在iPhone上看到的内容进行操作。石头出现在我的屏幕上,但是当我尝试与它们互动时,甚至连“NSLog&#39;消息显示在控制台中。
  4. &#39; stoneOneCreation&#39;方法(和......两个)是UIImageView,但正如你所看到的,我将它们存储在UIView指针中。
  5. 我还使用过&#39; [one setUserInteractionEnabled:YES]&#39; (还有......两个)但这也没有帮助。
  6. 如果我将手势识别器添加到self.view,一切正常(石头,游戏板和其他图形的显示出现,当我与屏幕的任何部分交互时,我将指示输出到控制台.. ....只是不是石头特定的互动)。
  7. 非常感谢你忍受所有这一切,如果有人知道什么是错的,这真的会有所帮助。 PS:所有文件#import都是正确的,所以这不是问题。

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

    Anthony
    

2 个答案:

答案 0 :(得分:0)

您应该将滑动手势识别器的不同实例添加到UIView的不同实例中。

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属性。

答案 1 :(得分:0)

您可能是对该视图禁用了用户交互,手势仅适用于具有userInteractionEnable = YES的视图。