ios – 为什么我的UISegmentedControl的顶部不可以插拔?

在我手机上玩的时候,我注意到我的UISegmentedControl没有很好的响应.需要2个或更多的尝试才能使我的水龙头注册.所以我决定在模拟器中运行我的应用程序,以更精确地探究错误.通过使用鼠标点击几十次,我确定了UISegmentedControl的前25%没有响应(该部分在下面的屏幕截图中的Photoshop中以红色突出显示).我不知道有什么看不见的UIView可能会阻止它.你知道如何使整个控件可以插拔?
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Uno",@"Dos",nil]];
self.segmentedControl.selectedSegmentIndex = 0;
[self.segmentedControl addTarget:self action:@selector(segmentedControlChanged:) forControlEvents:UIControlEventValueChanged];
self.segmentedControl.height = 32.0;
self.segmentedControl.width = 310.0;
self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
self.segmentedControl.tintColor = [UIColor colorWithWhite:0.9 alpha:1.0];
self.segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

UIView* toolbar = [[UIView alloc] initWithFrame:CGRectMake(0,self.view.width,HEADER_HEIGHT)];
toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = CGRectMake(
        toolbar.bounds.origin.x,toolbar.bounds.origin.y,// * 2 for enough slack when iPad rotates
        toolbar.bounds.size.width * 2,toolbar.bounds.size.height
    );
    gradient.colors = [NSArray arrayWithObjects:
        (id)[[UIColor whiteColor] CGColor],(id)[[UIColor 
            colorWithWhite:0.8
            alpha:1.0
            ] CGColor
        ],nil
];
[toolbar.layer insertSublayer:gradient atIndex:0];
toolbar.backgroundColor = [UIColor navigationBarShadowColor];
[toolbar addSubview:self.segmentedControl];

UIView* border = [[UIView alloc] initWithFrame:CGRectMake(0,HEADER_HEIGHT - 1,toolbar.width,1)];
border.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibletopMargin;
border.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1.0];
border.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[toolbar addSubview:border];

[self.segmentedControl centerInParent];

self.tableView.tableHeaderView = toolbar;

http://scs.veetle.com/soget/session-thumbnails/5363e222d2e10/86a8dd984fcaddee339dd881544ecac7/5363e222d2e10_86a8dd984fcaddee339dd881544ecac7_20140509171623_536d6fd78f503_68_896x672.jpg

解决方法

正如已经在其他答案中写过的那样,UINavigationBar抓住导航栏本身附近的触摸,但不是因为它有一些子视图扩展到边缘:这不是原因.

如果您记录整个视图层次结构,您将看到UINavigationBar不会超出定义的边.

接收触摸的原因是另一个原因:

在UIKit中有很多“特殊情况”,这就是其中之一.

当您点击屏幕时,将启动一个名为“命中测试”的过程.从第一个UIWindow开始,所有视图都被要求回答两个“问题”:点击你的边界点?接触事件的子视图是什么?

这两个方法可以回答这个问题:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;

好的,现在我们可以继续

在水龙头之后,UIApplicationMain开始命中测试过程.命中测试从主UIWindow开始(例如在状态栏窗口和警报视图窗口中执行),并且遍历所有子视图.

此过程执行3次:

>从UIWindow开始两次
>从_UIApplicationHandleEvent开始一次

如果您点击导航栏,您将看到UIWindow上的hitTest将返回UINavigationBar(全部三次)

然而,如果您点击导航栏下方的区域,您将会感到奇怪的是:

>前两个hitTest将返回您的UISegmentedControl
>最后一个hitTest将返回UINavigationBar

为什么这个?
如果你打开UIView子类,覆盖hitTest,你会看到前两次敲击点是正确的.第三次,有些事情改变了点 – 15点(或类似数字)

经过大量的搜索,我发现这里发生了什么:

UIWindow有一个(私有)方法调用

-(CGPoint)warpPoint:(CGPoint)point;

调试它,我看到如果这个方法立即在状态栏的下方,这个方法会改变点击点.
调试更多,我看到堆栈调用使这成为可能,只有3:

[UINavigationBar,_isChargeEnabled]
[UINavigationBar,isEnabled]
[UINavigationBar,_isAlphaHittableAndHasAlphaHittableAncestors]

所以,最后,这个warpPoint方法会检查是否启用了UINavigationBar并启用了hittable,如果是的话,它会“扭曲”.该点在0到15之间的像素扭曲,当您更靠近导航栏时,此“翘曲”会增加.

现在你知道幕后会发生什么,你必须知道如何避免它(如果你愿意的话).

你不能简单地覆盖warpPoint:如果应用程序必须去AppStore:它是一种私有的方法,你的应用程序将被拒绝.

你必须找到另一个系统(像建议,覆盖sendEvent,但我不知道它是否会工作)

因为这个问题是有趣的,我会在明天考虑一个合法的解决方案并更新这个答案(一个好的起点可以是子类化UINavigationBar,覆盖hitTest和pointInside,返回nil / false,如果给同一个事件多次调用,点变化但是我明天必须测试它是否工作)

编辑

好的,我尝试了许多解决方案,但找到合法和稳定的解决方案并不简单.
我已经描述了系统的实际行为,可能因不同版本而异(hitTest调用多于或少于3次,warpPoint扭曲约15px可改变ecc ecc的点).

最稳定的显然是warpPoint的非法覆盖:在UIWindow子类中:

-(CGPoint)warpPoint:(CGPoint)point;
{
    return point;
}

然而,我发现一个这样的方法(在UIWindow子类中)它足够稳定,并且做的诀窍:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    // this method is not safe if you tap the screen two times at the same x position and y position different for 16px,because it moves the point
    if (self.lastPoint.x == point.x)
    {
        // the points are on the same vertical line
        if ((0 < (self.lastPoint.y - point.y)) && ((self.lastPoint.y - point.y) < 16) )
        {
            // there is a differenc of ~15px in the y position?
            // if so,the point has been changed
            point.y = self.lastPoint.y;
        }
    }

    self.lastPoint = point;

    return [super hitTest:point withEvent:event];
}

方法记录最后一个点,并且如果后续点击位于相同的x,并且y最大为16px,则使用上一个点.我测试了很多,看起来很稳定.如果需要,您可以添加更多的控件来仅在特定控制器中启用此行为,或仅在窗口的已定义部分ecc ecc上.如果我找到另一个解决方案,我会更新这个帖子

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...