Преобразование координат UIView имеет исключение. convertPoint: toView: не работает хорошо - PullRequest
0 голосов
/ 07 сентября 2018

Пытаюсь сделать анимацию просмотра индикатора панели навигации.В макете есть некоторые проблемы.

Ожидает этого:

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}

Apple говорит:

convertPoint: toView: преобразует точку из системы координат получателя в систему координат указанного вида.

Как это исправить?

1 Ответ

0 голосов
/ 10 сентября 2018

На вашем месте я бы попробовал:

- (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);
    }];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...