我在视图控制器中有以下代码:
@interface QuizController ()
@property (weak, nonatomic) IBOutlet UITextView *questionText;
@property (weak, nonatomic) IBOutlet UITextView *one;
@property (weak, nonatomic) IBOutlet UITextView *two;
@property (weak, nonatomic) IBOutlet UITextView *three;
@property (weak, nonatomic) IBOutlet UITextView *four;
@property(strong, nonatomic) NSMutableArray* possAnswers;
@end
@implementation QuizController
- (void)viewDidLoad {
[super viewDidLoad];
[self.manager setupGame];
self.possAnswers = [[NSMutableArray alloc] initWithObjects:@"a", @"b", @"c", @"d", nil];
}
//This listens for a tap on any of the TextViews and sets the background to Blue
- (IBAction)fourTapped:(id)sender {
NSLog(@"There's been a tap!");
[self blankBoxes];
if( [sender isKindOfClass:[UITapGestureRecognizer class]] )
{
UITapGestureRecognizer* tgr = (UITapGestureRecognizer*)sender;
UIView* view = tgr.view;
if( [view isKindOfClass:[UITextView class]]){
UITextView* tv = (UITextView*)view;
NSLog([tv text]);
[tv setBackgroundColor:[UIColor blueColor]];
}
}
}
-(void)blankBoxes
{
[_one setBackgroundColor:[UIColor whiteColor]];
[_two setBackgroundColor:[UIColor whiteColor]];
[_three setBackgroundColor:[UIColor whiteColor]];
[_four setBackgroundColor:[UIColor whiteColor]];
}
但是,无论哪个视图被点按,唯一突出显示的UITextView
都是_one
。我不明白为什么会这样。如果有人能指出这一点,我真的很感激。
答案 0 :(得分:1)
手势识别器附加到一个且仅一个视图。这是设计的。如果您想要一个让您识别多个视图上的点击的手势识别器,则必须将其附加到一个通用的封闭式超视图,然后查看点击坐标并确定点击了哪个视图。
通常,您只需为需要响应水龙头的每个视图创建不同的手势识别器。
答案 1 :(得分:1)
将UIGestureRecognizer
添加到包含您的子视图的UIView
。之后,使用CGRectContainsPoint()
检查点击位置是否位于其中一个子视图中,然后从那里继续。
CGPoint *touchLocation = [gesture locationInView:self];
for(UIView *view in self.subviews){
if(CGRectContainsPoint(view.frame, touchLocation))
//Your code here
}