我在UIScrollView
子类的自定义类中使用了UIView
。在该scrollview中,我添加了其他几个自定义对象(所有子类化UIView
),如下所示:
UITapGestureRecognizer *tap;
for (int count = 0; count < ColorSchemeCount; count++) {
//Check for next page first
[self managePageAdditions];
//Set up the scheme
ColorScheme *scheme = [[ColorScheme alloc]
initWithFrame:[self determineSchemeCircleFrameFromCount:pageCheck]
title:[self getColorSchemeTitleFromIndex:pageCheck]
colors:[self getColorSchemeFromIndex:pageCheck]];
scheme.tag = pageCheck;
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(schemeTouchedAtIndex:)];
tap.cancelsTouchesInView = NO;
[scheme addGestureRecognizer:tap];
[self.scrollView addSubview:scheme];
//See if next pass requires a new page or not
if(pageCheck > 0) needsNextPage = (pageCheck % kSchemesPerPage == 0);
needsNextPage ? pageCheck = 0 : pageCheck++;
}
然后我尝试看到ColorScheme
的标签做出相应的回应:
- (void)schemeTouchedAtIndex:(UITapGestureRecognizer *)gesture{
CGPoint touchPointInSuperview = [gesture locationInView:self.scrollView];
ColorScheme *touchedView = (ColorScheme *)[self.scrollView hitTest:touchPointInSuperview withEvent:nil];
NSLog(@"%li", (long)touchedView.tag);
}
无论我做什么,始终将标记记录为零。
有几点意见:
ColorScheme
都添加得很好。tap.cancelsTouchesInView = NO
,因此UIScrollView
不会吞下所有接触。locationInView
我尝试使用self
代替self.scrollView
而没有运气。同样,它所在的代码位于子类UIView
。难以理解这一点,非常感谢任何帮助。
答案 0 :(得分:23)
-(void)schemeTouchedAtIndex:(UITapGestureRecognizer *)gesture{
NSLog(@"%ld", gesture.view.tag);
}
由于您的手势被挂钩到ColorScheme对象,因此从手势识别器本身抓取视图不那么容易。