gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:32:24 35 4
gpt4 key购买 nike

我尝试制作导航栏指示器 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/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com