我添加了一个UIView(其中包含背景的UIImageView,三个说“ Test”的UIButton和最后一个用来消除名为“ Finished”的UIButton)作为我的UIActionSheet的子视图。

为什么这些按钮中的任何一个都不能检测到触摸?我的UIView选中了“启用用户交互”。

我一直在拔头发(当然不是字面上的意思),对此我将不胜感激!

这是我的设置:

最佳答案

如果您只是想模仿UIActionSheet,则只需创建一个名为“ CoolActionSheet”之类的UIVew子类,然后以编程方式在其上放置按钮。然后,当您按下按钮时,它将触发协议中的委托方法,该协议将在您的主视图控制器中实现,因此请执行某些操作。

要显示和隐藏动作选择器,请使用CoolActionSheet类中的UIView动画,如下所示:

-(void)showSheet {
    NSLog(@"Showing sheet...");
    //Set the x/y position of the action sheet to JUST off-screen
    CGFloat xPos = parentView.frame.origin.x;
    CGFloat yPos = parentView.frame.size.height+kActionSheetHeight;

    [self setFrame:CGRectMake(xPos, yPos, kActionSheetWidth, kActionSheetHeight)];

    /*Here is where you would add your other UI objects such as buttons
    and set their @selector to a method in your CoolActionSheet protocol. You could then implement this delegate method in
    your main view controller to carry out a custom action. You might also want to add a background image to the view or something else.
    For example: */
    UIButton *coolButton = [[UIButton alloc] initWithFrame:buttonDimensions];
    [coolButton addTarget:self action:@selector(didDismissActionSheet) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:coolButton];

    [self.parentView addSubview:self.view];

    //Slide the sheet up from the bottom of the screen
    [UIView animateWithDuration:0.2 animations:^(void) {
        //Slide banner in from left to right
        [self setFrame:CGRectMake(0, yPos-kActionSheetHeight, kActionSheetWidth, kActionSheetHeight)];
    }];
}


并隐藏:

-(void)hideSheet {
    NSLog(@"Hiding");
    CGFloat xPos = parentView.frame.origin.x;
    CGFloat yPos = parentView.frame.size.height+kActionSheetHeight;

    [UIView animateWithDuration:0.2 animations:^(void) {
        [self setFrame:CGRectMake(xPos, yPos, 320, 65)];
    }completion:^(BOOL finished) {
        [self removeFromSuperview]; //Clean up
    }];
}


您可能还希望将父视图变灰。同样,在CoolActionSheet.m中:

-(void)shadeParentView {
    UIView *shadedView = [[UIView alloc] initWithFrame:CGRectMake(, 0, 320, 480)];

    [shadedView addGestureRecognizer:gestureRecognizer];
    [shadedView setBackgroundColor:[UIColor blackColor]];
    [shadedView setAlpha:0.0];

    [self addSubview:shadedView];

    [UIView animateWithDuration:0.5 animations:^{
        [shadedView setAlpha:0.5];
    }];


}

您需要将parentView设置为视图控制器的视图。因此,要在主视图控制器中调用操作表,您将:

CoolActionSheet *coolSheet = [[CoolActionSheet alloc] init];
[coolSheet setParentView:self.view];
[coolSheet setSheetDelegate:self]; //set the delegate to implement button press methods in this view controller


这似乎有点麻烦,但是将其分离到另一个这样的视图类中是一个很好的MVC模式。现在,您有了一个自定义类,您可以将其导入任何其他项目中,它将起作用!

我还没有机会测试这个特定的代码,但是整个方法很好。要点:


使用自定义UIView类
当在子视图中按下按钮时,实现委托方法以在主视图控制器中执行任务。
实施良好的MVC结构,以避免产生意粉代码。


让我知道这是否有帮助:)

关于iphone - 为什么UIButton不能在UIActionSheet的UIView中检测到触摸?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9749671/

10-12 00:16
查看更多