UIView坐标转换有一个例外。 convertPoint:toView:效果不佳

时间:2018-09-07 20:01:57

标签: ios objective-c uiview

我尝试制作导航栏指示器视图动画。布局有问题。

期望:

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}

苹果说:

  

convertPoint:toView:从接收者的坐标转换一个点   系统到指定视图的位置。

如何解决?

1 个答案:

答案 0 :(得分:1)

如果我是你,我会尝试:

- (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);
    }];
}