2

在 iphone 外部单击时,我们如何以编程方式关闭或隐藏下拉列表?

-(void)RecommendDropDownSelect
{
dropDown=[[DropDownView alloc]initWithArrayData:arr_RecommendName  cellHeight:30 heightTableView:100 paddingTop:-100 paddingLeft:10 paddingRight:20 refView:txt_Recommend animation:BLENDIN openAnimationDuration:0.5 closeAnimationDuration:0.5];
dropDown.delegate=self;
[scr_View addSubview:dropDown.view];
[dropDown openAnimation];


btn_RecommendDropDown.enabled=NO;
}

-(void)dropDownCellSelected:(NSInteger)returnIndex
{
btn_RecommendDropDown.enabled=YES;
txt_Recommend.text=[arr_RecommendName objectAtIndex:returnIndex];

}
4

3 回答 3

2

除了 subclassingUIView和 override touchesBegan,如果你正在使用,使用UITapGestureRecognizer似乎更容易UIViewController

首先,为您的视图设置一个点击手势:

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideDropDown)];
[gestureRecognizer setCancelsTouchesInView:NO];
[self.view addGestureRecognizer:gestureRecognizer];

然后实现隐藏下拉菜单的方法:

- (void)hideDropDown
{
    if ((dropDown != nil) && (dropDown.view != nil))
    {
        [drdropDown.view removeFromSuperview];
    }
}
于 2013-01-31T10:40:16.707 回答
1
 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
         UITouch *touch = [touches  anyObject];
           if (touch.view==self.view) {
             [dropDown removeFromSuperView];
          }
      }

或者

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches  anyObject];
      if (touch.view==self.view) {
           dropDown.alpha=0;
        }
    }
于 2013-01-31T10:33:10.310 回答
0

Try this:

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
   if(dropDown)
   {
      [drdropDown.view removeFromSuperView];
   }
}
于 2013-01-31T10:30:35.767 回答