这是我的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
我为(可能)不必要的长度道歉,我只是试图弄清楚这几天而且没有运气。这是我尝试做的事情的要点:
非常感谢你忍受所有这一切,如果有人知道什么是错的,这真的会有所帮助。 PS:所有文件#import都是正确的,所以这不是问题。
我正在使用XCode 7,Objective-C语言,以及为iOS开发
Anthony
答案 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的视图。