我在向UIButtons
添加一组UIScrollView
时遇到问题。添加按钮似乎滚动命令没有传递到滚动视图,因为按钮覆盖整个表面。我已经阅读了关于此的各种帖子,但仍然无法弄明白。我对iPhone编程很陌生,所以我可能会错过一些明显的东西。我尝试了canCancelContentTouches
和delaysContentTouches
到 TRUE 和 FALSE 等内容。
以下是我用来将按钮添加到UIScrollView
的代码。 UIScrollView
是在IB中创建的,并传递给函数:
-(void)drawCharacters:(NSMutableArray *)chars:(UIScrollView *)target_view {
//NSLog(@"CLEARING PREVIOUS BUTTONS");
//clear previous buttons for parts/kanji
if(target_view == viewParts){
for (UIButton *btn in parts_buttons) {
[btn removeFromSuperview];
}
[parts_buttons removeAllObjects];
} else if(target_view == viewKanji){
for (UIButton *btn in kanji_buttons) {
[btn removeFromSuperview];
}
[kanji_buttons removeAllObjects];
}
//display options
int chars_per_line = 9; //change this to change the number of chars displayed on each line
if(target_view == viewKanji){
chars_per_line = 8;
}
int char_gap = 0; //change this to change the margin between chars
int char_dimensions = (320/chars_per_line)-(char_gap*2);
//set starting x, y coords
int x = 0, y = 0;
//increment y coord
y += char_gap;
//NSLog(@"ABOUT TO DRAW FIRST BUTTON");
for(NSMutableArray *char_arr in chars){
//increment x coord
x += char_gap;
//draw at x and y
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(x, y, char_dimensions, char_dimensions); // position in the parent view and set the size of the button
[myButton setTitle:[char_arr objectAtIndex:0] forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0] forState:UIControlStateDisabled];
myButton.titleLabel.font = [UIFont boldSystemFontOfSize:22];
if(target_view == viewKanji){
myButton.titleLabel.font = [UIFont boldSystemFontOfSize:30];
}
// add targets and actions
if(target_view == viewParts){
[myButton addTarget:self action:@selector(partSelected:) forControlEvents:UIControlEventTouchUpInside];
} else if(target_view == viewKanji){
[myButton addTarget:self action:@selector(kanjiSelected:) forControlEvents:UIControlEventTouchUpInside];
[myButton setTag:(int)[char_arr objectAtIndex:1]];
}
//if the part isnt in the current list of kanji, disable and dim it
if(target_view == viewParts){
if([kanji count] > 0){
[myButton setEnabled:NO];
}
bool do_break = NO;
for(NSMutableArray *arr in kanji){
//NSLog([NSString stringWithFormat:@"CHECKING PARTS AGAINST %d KANJI", [kanji count]]);
for(NSString *str in [[arr objectAtIndex:2] componentsSeparatedByString:@" "]){
if(([myButton.titleLabel.text isEqualToString:str])){
//NSLog(@"--------------MATCH!!!-----------------");
[myButton setEnabled:YES];
for(NSString *str1 in parts_selected){
if(([myButton.titleLabel.text isEqualToString:str1])){
[myButton setEnabled:NO];
break;
}
}
do_break = YES;
break;
}
if(do_break) break;
}
if(do_break) break;
}
}
// add to a view
[target_view addSubview:myButton];
//update coords of next button
x += char_dimensions+ char_gap;
if (x > (320-char_dimensions)) {
x = 0;
y += char_dimensions + (char_gap*2);
}
//add button to global array to be removed from view next update
if(target_view == viewParts){
[parts_buttons addObject:myButton];
} else if(target_view == viewKanji){
[kanji_buttons addObject:myButton];
}
}
//NSLog(@"FINISHED DRAWING ALL BUTTONS");
}
对此的任何帮助将不胜感激。我只需要修复此问题即可完成我的应用。
答案 0 :(得分:0)
如果按钮覆盖整个表面,你不能指望scrollview接收事件。(它们将成为响应者链中的第一个)。快速修复将使按钮略小于滚动视图。
在第一次看到这个问题时,我想你想要在scrollview的superview中添加按钮 - 但后来我意识到你真的想要滚动视图中的按钮。