UISegmentedControl 触摸时的色调颜色

发布于 2024-08-28 22:10:08 字数 887 浏览 1 评论 0原文

我的应用程序中有一个 UISegmentedControl (请参阅下面的代码):

// --------------- SETTING NAVIGATION BAR RIGHT BUTTONS
NSArray *segControlItems = [NSArray arrayWithObjects:[UIImage imageNamed:@"up.png"],[UIImage imageNamed:@"down.png"], nil];
segControl = [[UISegmentedControl alloc] initWithItems:segControlItems];
segControl.segmentedControlStyle = UISegmentedControlStyleBar;
segControl.momentary = YES;
segControl.frame = CGRectMake(25.0, 7, 65.0, 30.0);
segControl.tintColor = [UIColor blackColor];
[segControl addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];

if (current == 0) [segControl setEnabled:NO forSegmentAtIndex:0];
if (current == ([news count]-1)) [segControl setEnabled:NO forSegmentAtIndex:1];
// ---------------

但是当您单击它时我无法让它显示某些内容...

它功能正常,但我希望当您单击它时它会变成灰色(但当你点击时)...这可能吗?

谢谢你,

戈泰。

I have a UISegmentedControl in my app (see code below) :

// --------------- SETTING NAVIGATION BAR RIGHT BUTTONS
NSArray *segControlItems = [NSArray arrayWithObjects:[UIImage imageNamed:@"up.png"],[UIImage imageNamed:@"down.png"], nil];
segControl = [[UISegmentedControl alloc] initWithItems:segControlItems];
segControl.segmentedControlStyle = UISegmentedControlStyleBar;
segControl.momentary = YES;
segControl.frame = CGRectMake(25.0, 7, 65.0, 30.0);
segControl.tintColor = [UIColor blackColor];
[segControl addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];

if (current == 0) [segControl setEnabled:NO forSegmentAtIndex:0];
if (current == ([news count]-1)) [segControl setEnabled:NO forSegmentAtIndex:1];
// ---------------

But I can't make it to show something when you click on it ...

It functionnally works perfectly but I would like it to tint to gray when you click on it (but just when you click) ... would that be possible ?

Thank you,

Gotye.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡淡的优雅 2024-09-04 22:10:08

似乎所选片段的色调颜色比原始色调更深。因此,如果您的色调为黑色,则所选片段的色调颜色不会改变,因为没有比这更暗的颜色了。

我环顾四周,没有找到任何好的方法来控制此控件的所选片段的色调颜色。

Seems as if the selected segment's tint color is made darker than original tint. Therefore if your tint is black then the selected segment's tint color doesn't change since there isn't anything darker than that.

I've looked around and haven't found any nice way to control the selected segment's tint color for this control.

老子叫无熙 2024-09-04 22:10:08

创建自己的分段控件的配方,(我猜)可以根据需要进行配置:

idevrecipes 自定义分段控件

顺便说一句:一个非常非常有趣的网站!

A recipe to create own segmented controls, which (I guess) can be configured as you want:

idevrecipes custom segmented control

BTW: A very very interesting website!

演出会有结束 2024-09-04 22:10:08

试试这个。如果您使用片段标题,它可以工作,如果您使用图像,您可能需要修改它。

segControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segControl addTarget:self action:@selector(changeColors:) forControlEvents:UIControlEventValueChanged];    

-(IBAction)changeColors:(UISegmentedControl *)sc{
    for (int i=0; i<sc.numberOfSegments; i++) {
        // reset all the titles in the segmented control
        [sc setTitle:[NSString stringWithFormat:@"%i", i] forSegmentAtIndex:i];
    }

    if (sc.selectedSegmentIndex < 0 || sc.selectedSegmentIndex >= sc.numberOfSegments) {
        return;
    }

    CGFloat width = sc.frame.size.width / sc.numberOfSegments - 1;
    CGRect rect = CGRectMake(0, 0, width, sc.frame.size.height - 2);

    UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // background gradient
    UIColor *colorOne = [UIColor colorWithWhite:0.6 alpha:1.0]; //top of gradient
    UIColor *colorTwo = [UIColor colorWithWhite:0.4 alpha:1.0]; //bottom of gradient
    NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, (id)colorTwo.CGColor, nil];
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
    CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0,0), CGPointMake(0,rect.size.height), 0);

    // black shadow at the top
    colors = [NSArray arrayWithObjects:(id)[UIColor colorWithWhite:0.0 alpha:0.3].CGColor, (id)[UIColor clearColor].CGColor, nil];
    gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
    CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0,0), CGPointMake(0,2.0), 0);

    UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, sc.frame.size.height - 4)];
    title.text = [sc titleForSegmentAtIndex:sc.selectedSegmentIndex];
    title.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
    title.textAlignment = UITextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    title.backgroundColor = [UIColor clearColor];
    title.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    title.shadowOffset = CGSizeMake(0, -0.5);
    [title.layer renderInContext:ctx];

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [sc setImage:img forSegmentAtIndex:sc.selectedSegmentIndex];
}

Try this. It works if you use segment titles, you may need to modify it if you use images.

segControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segControl addTarget:self action:@selector(changeColors:) forControlEvents:UIControlEventValueChanged];    

-(IBAction)changeColors:(UISegmentedControl *)sc{
    for (int i=0; i<sc.numberOfSegments; i++) {
        // reset all the titles in the segmented control
        [sc setTitle:[NSString stringWithFormat:@"%i", i] forSegmentAtIndex:i];
    }

    if (sc.selectedSegmentIndex < 0 || sc.selectedSegmentIndex >= sc.numberOfSegments) {
        return;
    }

    CGFloat width = sc.frame.size.width / sc.numberOfSegments - 1;
    CGRect rect = CGRectMake(0, 0, width, sc.frame.size.height - 2);

    UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // background gradient
    UIColor *colorOne = [UIColor colorWithWhite:0.6 alpha:1.0]; //top of gradient
    UIColor *colorTwo = [UIColor colorWithWhite:0.4 alpha:1.0]; //bottom of gradient
    NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, (id)colorTwo.CGColor, nil];
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
    CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0,0), CGPointMake(0,rect.size.height), 0);

    // black shadow at the top
    colors = [NSArray arrayWithObjects:(id)[UIColor colorWithWhite:0.0 alpha:0.3].CGColor, (id)[UIColor clearColor].CGColor, nil];
    gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
    CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0,0), CGPointMake(0,2.0), 0);

    UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, sc.frame.size.height - 4)];
    title.text = [sc titleForSegmentAtIndex:sc.selectedSegmentIndex];
    title.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
    title.textAlignment = UITextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    title.backgroundColor = [UIColor clearColor];
    title.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    title.shadowOffset = CGSizeMake(0, -0.5);
    [title.layer renderInContext:ctx];

    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [sc setImage:img forSegmentAtIndex:sc.selectedSegmentIndex];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文