ios - UIView 坐标转换异常。转换点 : toView: not work well

标签 ios objective-c uiview

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

期望这样:

1

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

导航栏指示器 View 应该属于 View Controller 。不是导航 Controller 。所以指标 View 在 self.navigationItem.titleView 上。

而它的位置是导航栏的底部。 Y位置正好是Point(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: Converts a point from the receiver’s coordinate system to that of the specified view.

如何解决?

最佳答案

如果我是你,我会尝试:

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

关于ios - UIView 坐标转换异常。转换点 : toView: not work well,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52228969/

相关文章:

iphone - 如何绘制带阴影但不旋转阴影的旋转 UIView

ios - 是否可以让可变高度的 UIView mask 层不拉伸(stretch),也许只是位于顶部? (例如, "torn paper"样式边缘?)

iOS Swift 3 - 面板点击后删除覆盖 UIView

ios - 在后台运行 URL 请求

ios - 尝试构建我的 xcode 项目时出现 GTMSessionFetcherService 链接器错误

ios - 隐藏动画期间可见的 UIStackView subview 内容

ios - iOS14.2 SwiftUI PageTabView 会多次调用 ChildView onAppear 方法

objective-c - Objective C 加密 CFB 模式

ios - 应用商店审核中的 IAP 错误,无法重现

ios - 在发薪日应用程序中为货币添加逗号