我创建了一个类似跳板的面板,其中包含按钮,如下所示:
-(void)configurePanel{
self.criteriaPanel.scrollEnabled=YES;
self.criteriaPanel=[[UIScrollView alloc] initWithFrame:CGRectMake(
0.0f,
0.0f,
[UIScreen mainScreen].bounds.size.width,
(BUTTON_HEIGHT+(3*BUTTON_Y_OFFSET))
)];
UIView *scrollViewContent=[[UIView alloc]init];
NSArray *criteriaTypeButtonTitles=[NSArray arrayWithObjects:
@"Option1",
@"Option2",
@"Option3",
@"Option4",
@"Option5",
@"Option6",
nil
];
for (int i=0; i<[criteriaTypeButtonTitles count]; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag=i;
button.frame = CGRectMake(
(((BUTTON_X_OFFSET*i)+(BUTTON_WIDTH*i))+BUTTON_X_OFFSET)
, BUTTON_Y_OFFSET
, BUTTON_WIDTH
, BUTTON_HEIGHT
);
[button setTitle:[criteriaTypeButtonTitles objectAtIndex:i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
button.titleLabel.font=[UIFont systemFontOfSize:13.0f];
[button setBackgroundColor:[UIColor grayColor]];
[button.layer setCornerRadius:10.0f];
[button.layer setBorderWidth:1.0f];
[button addTarget:self action:@selector(sortByButtonTap:) forControlEvents:UIControlEventTouchDown];
[scrollViewContent addSubview:button];
}
//based upon # of buttons
self.criteriaPanel.contentSize=CGSizeMake(
(([criteriaTypeButtonTitles count]*BUTTON_WIDTH)+([criteriaTypeButtonTitles count]*BUTTON_X_OFFSET)),
(BUTTON_HEIGHT+(2*BUTTON_Y_OFFSET)));
[self.criteriaPanel addSubview:scrollViewContent];
[self.view addSubview:self.criteriaPanel];
}
面板正确显示并滚动,但从不调用按钮的点击事件(sortByButtonTap :)。我怀疑这与滚动视图中包含的视图中包含的按钮有关。在阅读了许多其他问题和文档后,我仍然无法弄清楚解决方案应该是什么。
编辑: 我试验了直接将按钮添加到UIScrollView(self.criteriaPanel),然后按钮点击调用sortByButtonTap:所以这是按钮在scrollViewContent中的事情。
答案 0 :(得分:0)
您可能需要在包含视图userInteractionEnabled
上设置scrollViewContent
,以便按钮接收触摸事件:
[scrollViewContent setUserInteractionEnabled:YES];
答案 1 :(得分:0)
而不是:
[button addTarget:self
action:@selector(sortByButtonTap:)
forControlEvents:UIControlEventTouchDown];
尝试:
[button addTarget:self
action:@selector(sortByButtonTap:)
forControlEvents:UIControlEventTouchUpInside];
答案 2 :(得分:0)
如果你尝试怎么办?
[button setExclusiveTouch:YES];
当按钮位于同时处理触摸事件的视图中时,按钮往往会异常工作。
答案 3 :(得分:0)
请按照以下代码查询
在Scrollview中添加UIButton
scrollview.pagingEnabled = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake();
[button setTitle:@"X" forState:UIControlStateNormal];
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(btnCloseClicked:withEvent:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(btnCloseDraged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:self action:@selector(btnCloseDragedEnd:withEvent:) forControlEvents:UIControlEventTouchDragExit];
[scrollview addSubview:button];
scrollview.userInteractionEnabled = YES;
你可以手动设置这三种按钮方法。
如果您仍有疑问,请告诉我。
答案 4 :(得分:0)
我认为您的问题只是您将错误的UIControlEvent
设置为UIButton
。从UIControlEventTouchDown
更改为UIControlEventTouchUpInside
。如果您在UIButton
内连接Interface Builder
,则会始终将其设置为此UIControlEvent
:
另一个原因:
否则您的UIScrollView
会控制互动,因为它需要知道您是否要滚动它。如果您将UIButtons
放在UIScrollView
内,则可以访问它们,但总是会有一点延迟。这就是为什么您应该更改UIScrollView
:
您可以停用delays content touches
并立即访问UIButtons