繁体   English   中英

如何在滚动视图中滚动时更改多个 UILabel 文本颜色 Objective-c iOS

[英]How to change multiple UILabel text colour while scrolling inside scrollview Objective-c iOS

嗨,我是 iOS 开发的新手..任何人都可以帮助我..我根据数组计数在 UIScrollview 中添加了多个 UILabel..例如,如果数组计数为 3 意味着..然后在滚动视图中添加 3 个视图和 UILabel 在每个看法..

所以现在 3 个视图有 3 个不同的 UILabels..

但是现在我想根据要求在不同视图中更改 UILabel 文本的颜色……但是我无法更新颜色……

它仅针对最后一个索引更改 UILabel 文本的颜色..

我在 ScrollViewDidScroll:(UIScrollView *)scrollView 中编写了代码

任何建议..

self.robotScrollView.contentSize = CGSizeMake(robotCounts*Robot_ScrollView_Width, kRobotSrollViewH);
for (int i=0; i<robotCounts; i++) {
    self.robotLabel = [[UILabel alloc] initWithFrame:CGRectMake(Robot_ScrollView_Width*i, 0 , Robot_ScrollView_Width, kRobotSrollViewH)];
    self.robotLabel.textAlignment = NSTextAlignmentCenter;
    self.robotLabel.backgroundColor = [UIColor clearColor];
    self.robotLabel.textColor = [UIColor blackColor];
    [self.robotScrollView addSubview:self.robotLabel];
    if (kRemoteManager.robotsArray.count == 0) {
        self.robotLabel.text = @"";
        break;
    }
    DeviceBase *robot = kRemoteManager.robotsArray[i];
    self.robotLabel.text = [NSString stringWithFormat:@"%@",robot.dName];

}
self.robotScrollView.contentOffset = CGPointMake(Robot_ScrollView_Width*currentRobotIndex, 0);

在下面的 scrollviewdidscroll 方法中更改 UIlabeltext 颜色

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.scrollView)
    {
        self.pageControl.currentPage = (scrollView.contentOffset.x+(scrollView.width/2)) / scrollView.width;
        kRemoteManager.currentIndex = self.pageControl.currentPage;
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotify_Steward_ReloadData object:nil];
        NSMutableArray *viewRemoteIDarray = [NSMutableArray array];
        CardRemoteModel *model =  kRemoteManager.cardRemoteArray[self.pageControl.currentPage];
            if (![viewRemoteIDarray containsObject:@(model.remoteID)])
            {
                if (model.remoteType == kooKong_remoteType_AirCleaner || model.remoteType == kooKong_remoteType_AC)
                {
                    self.robotLabel.textColor = [UIColor whiteColor];
                }
            else
                {
                self.robotLabel.textColor = [UIColor blackColor];
                }
            }
    } else if (scrollView == self.robotScrollView) {
        kRemoteManager.currentRobotIndex = (scrollView.contentOffset.x+(scrollView.width/2)) / scrollView.width;
        self.leftBtn.hidden = NO;
        self.rightBtn.hidden = NO;
        if (kRemoteManager.currentRobotIndex == kRemoteManager.robotsArray.count-1) {
            self.rightBtn.hidden = YES;
        }
        if (kRemoteManager.currentRobotIndex == 0){
            self.leftBtn.hidden = YES;
        }
    }
}

有三个标签,但只有一个标签属性。 分配的最后一个是您以后可以访问的唯一一个。 一种解决方案是在视图控制器中保留一组标签。

@property(nonatomic, strong) NSMutableArray *labels;

在发布的方法中...

self.labels = [NSMutableArray array];
for (int i=0; i<robotCounts; i++) {
    UILabel *robotLabel = [[UILabel alloc] initWithFrame:CGRectMake(Robot_ScrollView_Width*i, 0 , Robot_ScrollView_Width, kRobotSrollViewH)];

    [self.labels addObject:robotLabel];                 // <--- new

    robotLabel.textAlignment = NSTextAlignmentCenter;
    robotLabel.backgroundColor = [UIColor clearColor];
    robotLabel.textColor = [UIColor blackColor];
    [self.robotScrollView addSubview:robotLabel];
    if (kRemoteManager.robotsArray.count == 0) {
        robotLabel.text = @"当前无酷控机器人";
        break;
    }
    DeviceBase *robot = kRemoteManager.robotsArray[i];
    robotLabel.text = [NSString stringWithFormat:@"%@",robot.dName];
}

要更改所有颜色:

- (void)setLabelColors:(UIColor *)color {
    for (UILabel *label in self.labels) {
        label.textColor = color;
    }
}

另一个想法是给每个标签一个标签,并在需要时找到它们。

for (int i=0; i<robotCounts; i++) {
    self.robotLabel = [[UILabel alloc] initWithFrame:CGRectMake(Robot_ScrollView_Width*i, 0 , Robot_ScrollView_Width, kRobotSrollViewH)];
    self.robotLabel.tag = i+1;
    // the remainder of this loop as you have it

要更改所有颜色...

- (void)setLabelColors:(UIColor *)color {
    for (int i=0; i<robotCounts; i++) {
        UILabel *label = (UILabel *)[self.robotScrollView viewWithTag:i+1];
        label.textColor = color;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM