UISwipeGesture 无法使用自定义 UIView
UISwipeGesture Not working using custom UIView
这是我的 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
对于(可能)不必要的长度,我深表歉意,我只是想弄清楚这几天,但没有成功。这是我正在尝试做的事情的要点:
- setInititalStateMain 设置两个棋子在 5x5 网格中的位置
- 在 GameViewController.m 中,我正在尝试捕获 'stoneUnoView' 和 'stoneDueView' 属性(在 TwinstonesStoneView.m 文件中设置),为它们提供滑动手势,并进行交互与他们一起使用 TwinstonesStoneView.m 中提供的方法。
- 我是否产生了过多的观看次数?要注意的是,当我 运行 程序时,一切都按照我在 IPhone 上看到的内容进行。石头出现在我的屏幕上,但当我尝试与它们互动时,控制台中甚至没有显示 'NSLog' 消息。
- 'stoneOneCreation' 方法(和...两个)是 UIImageView 的,但是,如您所见,我将它们存储在 UIView 指针中。
- 我还使用了“[一个setUserInteractionEnabled:YES]”(和...两个),但这也没有帮助。
- 如果我将手势识别器添加到 self.view,一切正常(石头、游戏板和其他图形的显示出现,当我与屏幕的任何部分交互时,我将指示输出到控制台……只是不是特定于石头的交互)。
非常感谢您忍受这一切,如果有人知道出了什么问题,这将非常有帮助。 PS: 所有文件 #import 都是正确的,所以这不是问题。
我正在使用 XCode 7,Objective-C 语言,并为 iOS
开发
Anthony
您应该将不同的滑动手势识别器实例添加到不同的 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];
我认为这是因为手势识别器只读 view 属性.
您好,可能是该视图禁用了用户交互,手势仅适用于具有 userInteractionEnable = YES 的视图。
这是我的 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
对于(可能)不必要的长度,我深表歉意,我只是想弄清楚这几天,但没有成功。这是我正在尝试做的事情的要点:
- setInititalStateMain 设置两个棋子在 5x5 网格中的位置
- 在 GameViewController.m 中,我正在尝试捕获 'stoneUnoView' 和 'stoneDueView' 属性(在 TwinstonesStoneView.m 文件中设置),为它们提供滑动手势,并进行交互与他们一起使用 TwinstonesStoneView.m 中提供的方法。
- 我是否产生了过多的观看次数?要注意的是,当我 运行 程序时,一切都按照我在 IPhone 上看到的内容进行。石头出现在我的屏幕上,但当我尝试与它们互动时,控制台中甚至没有显示 'NSLog' 消息。
- 'stoneOneCreation' 方法(和...两个)是 UIImageView 的,但是,如您所见,我将它们存储在 UIView 指针中。
- 我还使用了“[一个setUserInteractionEnabled:YES]”(和...两个),但这也没有帮助。
- 如果我将手势识别器添加到 self.view,一切正常(石头、游戏板和其他图形的显示出现,当我与屏幕的任何部分交互时,我将指示输出到控制台……只是不是特定于石头的交互)。
非常感谢您忍受这一切,如果有人知道出了什么问题,这将非常有帮助。 PS: 所有文件 #import 都是正确的,所以这不是问题。
我正在使用 XCode 7,Objective-C 语言,并为 iOS
开发Anthony
您应该将不同的滑动手势识别器实例添加到不同的 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];
我认为这是因为手势识别器只读 view 属性.
您好,可能是该视图禁用了用户交互,手势仅适用于具有 userInteractionEnable = YES 的视图。