這裏是我的代碼:IOS滾動視圖無法點擊
int i = 0;
self.userInteractionEnabled = YES;
self.exclusiveTouch = YES;
self.canCancelContentTouches = YES;
self.delaysContentTouches = YES;
self.translatesAutoresizingMaskIntoConstraints = YES;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gototest)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[self addGestureRecognizer:tapGestureRecognizer];
for (NSString *message in self.messages) {
UILabel * label = [[UILabel alloc] initWithFrame:CGRectZero];
self.userInteractionEnabled = YES;
label.text = message;
label.tag = i;
CGSize size = [message sizeWithFont:label.font];
CGFloat width = size.width + kPADDING;
label.frame = CGRectMake(xPos, 0.0, width, self.frame.size.height);
[self addSubview:label];
i++;
xPos += width;
NSLog(@"%@",NSStringFromCGRect(label.frame));
}
self.messagesWidth = xPos;
self.contentSize = CGSizeMake(xPos, self.frame.size.height);
self.contentOffset = CGPointMake(-self.frame.size.width, 0.0);
}
-(void)gototest
{
NSLog(@"test %@",@"ccc ");
}
,然後選取框 - (無效)去{
if (!self.period) self.period = self.messagesWidth/100;
// so it always takes about the same (fudged, but reasonable) amount of time to scroll the whole array
[UIView animateWithDuration:self.period
delay:0.0
options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionRepeat
animations:^{
self.contentOffset = CGPointMake(self.messagesWidth, 0.0);
} completion:^(BOOL finished){}];
}
所以,我的目標是創建新聞選框,每個新聞都可以點擊查看點擊新的詳細信息。
但UITapGestureRecognizer不工作,我不知道爲什麼。
請注意,自我是滾動視圖,因爲我的類從UIScrollView延伸。
所以,請幫我
我喜@Limon我不關心標籤,我需要滾動視圖點擊。 – Houssam
你的意思是_clickable_?在iOS中沒有像_click_這樣的事件,我們正在這裏使用_touches_和_gestures_。 – holex
hi @holex我喜歡觸摸,所以當我觸摸scrollview什麼也沒有發生 – Houssam