Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 views
in Technique[技术] by (71.8m points)

iphone - UIButton inside UIScrollView doesn't fire on tap

UIButton inside UIScrollView doesn't fire on tap. Please help resolve it.

// Add a button
UIButton *btn1 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
btn1.frame = CGRectMake(0, 0, 26, 18); 
btn1.bounds = CGRectMake(0, 0, 30.0, 30.0); 
[btn1 addTarget:self action:@selector(buttonClick1:) 
                     forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:btn1];

- (void)buttonClick1:(id)sender {
    int dd = 4; 
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found this stuff:

But the scrollview will block the touches from the button unless you set userInteractionEnabled and exclusiveTouch properties to NO on the scrollview. But that would defeat the purpose of having a scrollview inside a button I think.

(See Can we put a UIButton in a UIScrollView and vice versa in iPhone)

But how to do it?

Say you have a UIScrollView *sv ... then

sv.userInteractionEnabled = YES;
sv.exclusiveTouch = YES;

I would also add

sv.canCancelContentTouches = YES;
sv.delaysContentTouches = YES;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...