3

我创建了一个类似面板的跳板,其中包含如下按钮:

-(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 中的按钮。

4

5 回答 5

0

我认为您的问题只是您UIControlEventUIButton. 从 更改UIControlEventTouchDownUIControlEventTouchUpInside。如果您在UIButton内部连接 ​​a Interface Builder,它将始终将其设置为此UIControlEvent

在此处输入图像描述

另一个原因:

否则,您将UIScrollView控制交互,因为它需要知道您是否要滚动它。如果你把它们UIButtons放在你的里面UIScrollView,它们将是可以访问的,但总是有一点延迟。这就是为什么您应该更改以下设置的原因UIScrollView

您可以禁用delays content touches并且UIButtons可以立即访问

在此处输入图像描述

于 2015-01-30T12:23:57.377 回答
0

如果你尝试:

[button setExclusiveTouch:YES];

当按钮位于同时处理触摸事件的视图中时,它们往往会异常工作。

于 2012-04-25T13:24:20.650 回答
0

您可能需要userInteractionEnabled在包含视图scrollViewContent上设置按钮以接收触摸事件:

[scrollViewContent setUserInteractionEnabled:YES];
于 2012-04-24T21:31:16.317 回答
0

代替:

[button addTarget:self 
           action:@selector(sortByButtonTap:) 
 forControlEvents:UIControlEventTouchDown];

尝试:

[button addTarget:self 
           action:@selector(sortByButtonTap:) 
 forControlEvents:UIControlEventTouchUpInside];
于 2012-04-24T21:32:07.987 回答
0

按照此代码进行查询,

在滚动视图中添加 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;

您可以手动放置这三种按钮的方法。

如果您仍有疑问,请告诉我。

于 2012-10-06T07:03:57.803 回答