Ios 使用自定义UIView时,UISwipe无法正常工作
这是我的GameViewController.m文件:Ios 使用自定义UIView时,UISwipe无法正常工作,ios,objective-c,xcode,uiswipegesturerecognizer,Ios,Objective C,Xcode,Uiswipegesturerecognizer,这是我的GameViewController.m文件: - (void)viewDidLoad { [super viewLoad]; . . . _board = [[TwinstonesBoardModel alloc] init]; [_board setToInitialStateMain]; TwinstonesStoneView* twinstonesBoard = [[TwinstonesStoneView alloc]
- (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
您应该将滑动手势识别器的不同实例添加到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];
我认为这是因为手势识别器具有只读属性。您应该将滑动手势识别器的不同实例添加到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识别器具有只读属性。Hi may be user interaction已禁用该视图,手势仅适用于userInteractionEnable=YES的视图。Hi may be user interaction已禁用该视图,手势仅适用于userInteractionEnable=YES的视图。不幸的是,仍然没有响应。是不是参考文献太多了?当我把它画出来时,参考资料似乎是可靠的。我正确导入了所有文件。是否存在子视图被掩埋在子视图下的情况?我无法与自定义视图交互的原因是因为它们不知何故隐藏在我手指交互的最前面的“表面”之下吗?什么是只读视图属性?有什么我必须更改的吗?我的意思是什么识别器只能附加到一个视图。并且,在您的代码中,您在initWithFrame中创建了_stoneUnoView和_stoneDueView。。。方法,但在代码中,使用init方法创建TwinstonesStonesView(stoneOne和storeTwo)的实例。我想你必须打开调试器,检查1和2到零的值。哇!它现在真的起作用了!首先,我决定在GameViewController.m文件中创建stone视图,然后将它们作为UIViews传递给initWithMainFrame:函数。然后将我的滑动方法移回GameViewController.m文件。不幸的是,仍然没有响应。是不是参考文献太多了?当我把它画出来时,参考资料似乎是可靠的。我正确导入了所有文件。是否存在子视图被掩埋在子视图下的情况?我无法与自定义视图交互的原因是因为它们不知何故隐藏在我手指交互的最前面的“表面”之下吗?什么是只读视图属性?有什么我必须更改的吗?我的意思是什么识别器只能附加到一个视图。并且,在您的代码中,您在initWithFrame中创建了_stoneUnoView和_stoneDueView。。。方法,但在代码中,使用init方法创建TwinstonesStonesView(stoneOne和storeTwo)的实例。我想你必须打开调试器,检查1和2到零的值。哇!它现在真的起作用了!首先,我决定在GameViewController.m文件中创建stone视图,然后将它们作为UIViews传递给initWithMainFrame:函数。然后将我的滑动方法移回GameViewController.m文件。