代码之家  ›  专栏  ›  技术社区  ›  dengApro

UIView坐标转换有一个异常。convertPoint:toView:效果不好

  •  1
  • dengApro  · 技术社区  · 6 年前

    期望:

    1

    我的解决方案是将导航栏底部的位置转换为 self.navigationItem.titleView

    导航栏指示器视图应属于视图控制器。不是导航控制器。所以指示器视图打开了 self.navigationItem.titleView .

    而它的位置是导航栏的底部。Y位置正好是点(0,64)。

    所以是一种方法 convertPoint: toView: 代码如下:

    - (void)setupTitlesView
    {
        UIView *titlesView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 100, 35)];
        self.navigationItem.titleView = titlesView;
    
        CGPoint new_point = [self.view convertPoint: CGPointMake(0, 64) toView: self.navigationItem.titleView];
        NSLog(@"\n\n%@\n\n", NSStringFromCGPoint(new_point));
    
        UIView *indicatorView = [[UIView alloc] initWithFrame: CGRectMake(0, new_point.y - 2, 0, 2)];
        indicatorView.tag = -1;
        self.indicatorView = indicatorView;
    
        NSArray *titles = @[@"商品", @"详情"];
    
        CGFloat width = titlesView.frame.size.width / titles.count;
        CGFloat height = titlesView.frame.size.height;
        for (NSInteger i = 0; i<titles.count; i++) {
            UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(i*width, 0, width, height)];
            button.tag = i;
            [button setTitle:titles[i] forState:UIControlStateNormal];
            // color , font ...
            [titlesView addSubview:button];
    
            // default click
            if (i == 0) {
                button.enabled = NO;
                self.selectedButton = button;
    
                [button.titleLabel sizeToFit];
                CGRect tempFrame = self.indicatorView.frame;
                tempFrame.size.width = button.titleLabel.frame.size.width;
                self.indicatorView.frame = tempFrame;
                CGPoint tempCenter = self.indicatorView.center;
                tempCenter.x = button.center.x;
                self.indicatorView.center = tempCenter;
            }
        }
        [titlesView addSubview: indicatorView];  
    }
    

    结果打印:

    {0, 64}

    苹果说:

    反对的论点顶点:视图:从坐标转换点

    如何修复?

    1 回复  |  直到 6 年前
        1
  •  1
  •   无夜之星辰    6 年前

    如果我是你,我会尝试:

    - (void)setupTitlesView
    {
        UIView *titlesView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 100, 35)];
        self.navigationItem.titleView = titlesView;
    
        UIView *indicatorView = [[UIView alloc] initWithFrame: CGRectMake(0, titlesView.frame.size.height - 2, 50, 2)];
        indicatorView.tag = -1;
        self.indicatorView = indicatorView;
        self.indicatorView.backgroundColor = [UIColor blueColor];
    
        NSArray *titles = @[@"商品", @"详情"];
    
        CGFloat width = titlesView.frame.size.width / titles.count;
        CGFloat height = titlesView.frame.size.height;
        for (NSInteger i = 0; i<titles.count; i++) {
            UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(i*width, 0, width, height)];
            button.tag = i+1000;
            [button setTitle:titles[i] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
            [titlesView addSubview:button];
    
            // default click
            if (i == 0) {
                button.selected = YES;
            }
        }
        [titlesView addSubview: indicatorView];
    }
    
    - (void)buttonClicked:(UIButton *)sender
    {
        for (int i = 0; i < 2; i++) {
            UIButton *button = [self.navigationItem.titleView viewWithTag:(1000+i)];
            button.selected = (button.tag == sender.tag);
        }
        [UIView animateWithDuration:0.3 animations:^{
            self.indicatorView.center = CGPointMake(sender.center.x, self.indicatorView.center.y);
        }];
    }