OStack程序员社区-中国程序员成长平台

标题: ios - 在点击点放置一个 UIView 以启动弹出窗口 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:02
标题: ios - 在点击点放置一个 UIView 以启动弹出窗口

在我认为解决从 UITableViewRowAction 启动弹出框问题的一种相当聪明的方法中,我想在目标操作上放置一个清晰的 UIView并将其用于 sourceRect 以锚定 UIPopoverPresentationController。在这种情况下,目标操作是下面屏幕截图中显示的橙色“Acct”按钮。

我在整个 tableview 上使用了一个清晰的 UIView (self.clearTopView) 并用一个框架(我认为)来自 View 中的点击位置(point)和一个CGSize(tappedRectSize)。然后我将它作为 subview 添加到 self.clearTopView。我现在把它涂成黑色,这样我就可以找到它了。

如下图所示,部分方案正在按计划进行。 tappedViewOverlay View 被添加,并适当着色。 popoverPresentationController (thisPPC) 按预期从该 View 启动。

enter image description here

问题是 View 始终位于屏幕的左上角,而不是所需的位置,即点击发生的位置。以下是相关代码:

    else if ([[segue identifier] isEqualToString"AccountPopSegue"])
    {

        UITapGestureRecognizer *tapGestureRecognizer;

        CGPoint point = [tapGestureRecognizer locationInView:self.clearTopView];

        NSString *pointString = NSStringFromCGPoint(point);

        NSLog(@"point is located at %@",pointString);


        CGSize tappedRectSize= CGSizeMake(60,60);
        CGRect tappedRect = {point,tappedRectSize};

        NSString * tappedRectString = NSStringFromCGRect(tappedRect);

        NSLog(@"tappedRect = %@",tappedRectString);


        tappedViewOverlay = [[UIView alloc]initWithFrame:tappedRect];
        tappedViewOverlay.backgroundColor = [UIColor blackColor];

        [self.clearTopView addSubview:tappedViewOverlay];

        [self.clearTopView setUserInteractionEnabled:NO];


        NSString *tappedOverlayFrame = NSStringFromCGRect(tappedViewOverlay.frame);
        NSLog(@"tappedViewOverlay.frame = %@",tappedOverlayFrame);


        UIViewController *controller = segue.destinationViewController;
        controller.popoverPresentationController.delegate = self;
        controller.preferredContentSize = CGSizeMake(320, 186);
        UIPopoverPresentationController *thisPPC = controller.popoverPresentationController;


        thisNavController = (UINavigationController *)segue.destinationViewController;
        AccountChangeVC *acCVC = (AccountChangeVC *)thisNavController.topViewController;
        acCVC.delegate = self;

        thisPPC.sourceView = self.clearTopView;
        thisPPC.sourceRect = tappedViewOverlay.bounds;

谁能告诉我哪里出错了?

编辑

CoderWang 指出了我的代码中的一个疏忽,所以我已经纠正了它:

    else if ([[segue identifier] isEqualToString"AccountPopSegue"])
    {

        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]init];

        [self.clearTopView addGestureRecognizer:tapGestureRecognizer];



        CGPoint point = [tapGestureRecognizer locationInView:self.clearTopView];

        NSString *pointString = NSStringFromCGPoint(point);

        NSLog(@"point is located at %@",pointString);


        CGSize tappedRectSize= CGSizeMake(60,60);

        CGRect tappedRect = {point,tappedRectSize};

        NSString * tappedRectString = NSStringFromCGRect(tappedRect);

        NSLog(@"tappedRect = %@",tappedRectString);


        tappedViewOverlay = [[UIView alloc]initWithFrame:tappedRect];

        tappedViewOverlay.backgroundColor = [UIColor blackColor];

        [self.clearTopView addSubview:tappedViewOverlay];

        [self.clearTopView setUserInteractionEnabled:NO];


        NSString *tappedOverlayFrame = NSStringFromCGRect(tappedViewOverlay.frame);
        NSLog(@"tappedViewOverlay.frame = %@",tappedOverlayFrame);


        UIViewController *controller = segue.destinationViewController;
        controller.popoverPresentationController.delegate = self;
        controller.preferredContentSize = CGSizeMake(320, 186);
        UIPopoverPresentationController *thisPPC = controller.popoverPresentationController;


        thisNavController = (UINavigationController *)segue.destinationViewController;
        AccountChangeVC *acCVC = (AccountChangeVC *)thisNavController.topViewController;
        acCVC.delegate = self;

        thisPPC.sourceView = self.clearTopView;
        thisPPC.sourceRect = tappedViewOverlay.bounds;

此更改(tapGestureRecognizer 的初始化和对 self.clearTopView 的添加)在黑色 tappedViewOverlay View 的位置上产生了轻微的变化.您可以在以下屏幕截图中看到它隐藏在“返回”导航按钮后面:

enter image description here



Best Answer-推荐答案


问题是“tappedRect”不正确,不知 Prop 体原因。 但是您可以尝试以下方法将“Acct”按钮转换为 self.clearTopView 的矩形:

CGRect tappedRect = [Acct.superview convertRect:acct.frame toView:self.clearTopView];

或转换为窗口(因为 self.clearTopView 在整个 tableview 上):

CGRect tappedRect = [Acct.superview convertRect:acct.frame toView:nil];

关于ios - 在点击点放置一个 UIView 以启动弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38490169/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4