2010-04-10 4 views
2

J'ai un UISegmentedControl dans mon application (voir le code ci-dessous):couleur de teinte UISegmentedControl sur le toucher

// --------------- 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]; 
// --------------- 

Mais je ne peux pas le faire pour montrer quelque chose quand vous cliquez dessus ...

Cela fonctionne fonctionnellement parfaitement mais je voudrais qu'il teinte en gris quand tu cliques dessus (mais juste quand tu cliques) ... cela serait-il possible?

Merci,

Gotye.

Répondre

2

Semble que la couleur de teinte du segment sélectionné est plus sombre que la teinte d'origine. Par conséquent, si votre teinte est noire, la couleur de teinte du segment sélectionné ne change pas car il n'y a rien de plus sombre.

J'ai regardé autour de moi et je n'ai trouvé aucun moyen sympa de contrôler la couleur de teinte du segment sélectionné pour ce contrôle.

0

Essayez ceci. Cela fonctionne si vous utilisez des titres de segment, vous devrez peut-être le modifier si vous utilisez des 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]; 
} 
Questions connexes