2011-12-05 23 views
7

¿De qué forma puede teñir una UIView? No es el color de fondo, sino todo el UIView con todas sus subvistas.Tinte una UIView con todas sus subvistas

e.g - Una UIView con una animación de una estrella girando, es decir, la forma de UIView está en constante cambio.

+2

Se podría añadir una nueva subvista para llenar todo el marco de vista, establecer un color de BG a la vista secundaria y que sea ligeramente transparente .... aunque no será 'teñido' realmente. –

+0

el objetivo UIView no es un rectángulo, tiene una forma única, por ejemplo, una estrella. –

+0

Tiene una UIView en forma de estrella con subvistas en ella? –

Respuesta

7

el tiempo me creó una categoría UIView que permite teñido de un UIView, sin llenar el rectángulo UIView, aquí está:

Toma una representación de la imagen de la UIView, a continuación, los colores en el dado UIColor , esta categoría se creó para replicar un comportamiento de resaltado predeterminado UIButton , por lo que también viene con un método que puede activar ese comportamiento y dejar que la categoría maneje todos los métodos táctiles.

//------------------ .h Interface file ------- // 

// 
// UIView UIView_Tint.h 
// BabyQA 
// 
// Created by yogev shelly on 8/10/12. 
// Copyright (c) 2012 __MyCompanyName__. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

#define tintColorClassicUIButton [UIColor colorWithWhite:0.0 alpha:0.5] 

@interface UIView(Tint) 

//proprties should not be used, use the methods decalred bellow 
@property (nonatomic,retain) UIColor* tintColor; 
@property(nonatomic,retain) UIImageView* tintImageView; 
@property(nonatomic,assign) BOOL tintOnTouchActive; 

-(void)tintToColor:(UIColor*)color; 
-(void)clearTint; 
-(void)enableTintOnTouchWithColor:(UIColor*)color; 
-(void)disableTintOnTouch; 

-(UIImage *)imageRepresentation; 
-(UIImage*)imageRepresentationWithTintColor:(UIColor*)color; 

@end 



//------------------ .m Implementation file ------- // 


// UIView UIView_Tint.m 
// BabyQA 
// 
// Created by yogev shelly on 8/10/12. 
// Copyright (c) 2012 __MyCompanyName__. All rights not reserved - go wild! 
// 

#import "UIView Tint.h" 
#import <objc/runtime.h> 
#import <QuartzCore/QuartzCore.h> 

static char const * const tintImageViewKey = "tintImageView"; 
static char const * const tintColorKey = "tintColorKey"; 
static char const * const tintOnTouchActiveKey = "tintOnTouchActive"; 



@implementation UIView (Tint) 
@dynamic tintImageView; 
@dynamic tintColor; 
@dynamic tintOnTouchActive; 

-(void)enableTintOnTouchWithColor:(UIColor*)color 
{ 
    self.tintColor = color; 
    self.tintOnTouchActive = TRUE; 
} 

-(void)disableTintOnTouch 
{ 
    self.tintOnTouchActive = FALSE; 

} 


-(void)tintToColor:(UIColor*)color 
{ 

    if(![self.tintColor isEqual:color] || !self.tintImageView) 
    { 
     self.tintColor = color; 
     UIImage* tintImage = [self imageRepresentationWithTintColor:self.tintColor]; 
     self.tintImageView = [[[UIImageView alloc] initWithImage:tintImage] autorelease]; 
    } 

    [self addSubview:self.tintImageView]; 
} 

-(void)clearTint 
{ 
    [self.tintImageView removeFromSuperview]; 
} 

-(void)clearTintWithSecondsDelay:(float)delay 
{ 
    [self performSelector:@selector(clearTint) withObject:self afterDelay:delay]; 
} 


#pragma mark - TouchToggling 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesBegan:touches withEvent:event]; 
    if(self.tintOnTouchActive) 
     [self tintToColor:self.tintColor]; 
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesEnded:touches withEvent:event]; 
    if(self.tintOnTouchActive) 
     [self clearTintWithSecondsDelay:0.05]; 
} 

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesCancelled:touches withEvent:event]; 
    if(self.tintOnTouchActive) 
     [self clearTintWithSecondsDelay:0.05]; 
} 

#pragma mark - TintingPart 

-(UIImage *)imageRepresentation 
{ 

    UIGraphicsBeginImageContext(self.bounds.size); 
    [self.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return img;  
} 

-(UIImage*)imageRepresentationWithTintColor:(UIColor*)color 
{ 
    UIImage* viewImage = [self imageRepresentation]; 
    viewImage = [self tintedImage:viewImage UsingColor:color]; 
    return viewImage; 
} 

-(UIImage *)tintedImage:(UIImage*)image UsingColor:(UIColor *)tintColor { 
    UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]); 
    CGRect drawRect = CGRectMake(0, 0, image.size.width, image.size.height); 
    [image drawInRect:drawRect]; 
    [tintColor set]; 
    UIRectFillUsingBlendMode(drawRect, kCGBlendModeSourceAtop); 
    UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return tintedImage; 
} 


#pragma mark - Dynamic setters/getters for Associative References 

-(void)setTintImageView:(UIImageView *)tintImageView 
{ 
    objc_setAssociatedObject(self, tintImageViewKey, tintImageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 
} 

-(UIImageView*)tintImageView 
{ 
    return objc_getAssociatedObject(self , tintImageViewKey); 
} 


-(UIColor*)tintColor 
{ 
    return objc_getAssociatedObject(self , tintColorKey); 
} 

-(void)setTintColor:(UIColor *)tintColor 
{ 
    objc_setAssociatedObject(self, tintColorKey, tintColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 
} 

-(BOOL)tintOnTouchActive 
{ 
    return [objc_getAssociatedObject(self, tintOnTouchActiveKey) boolValue]; 
} 

-(void)setTintOnTouchActive:(BOOL)tintOnTouchActive 
{ 
    objc_setAssociatedObject(self, tintOnTouchActiveKey, [NSNumber numberWithBool:tintOnTouchActive], OBJC_ASSOCIATION_RETAIN_NONATOMIC); 
} 

@end 
4

Para matizar un UIView, agregue otra subvista encima con el mismo frame y un conjunto de transparencia alfa.

Por ejemplo digamos vista de su tipo de contenedor se llama containerView, que haría de la siguiente manera:

CGRect overlayFrame = containerView.frame; 
UIView *overlayView = [UIView alloc] initWithFrame:overlayFrame]; 
overlayView.alpha = 0.5f; 
overlayView.color = [UIColor blackColor]; 

[containerView addSubview:overlayView]; 

Esto le da una (0.5f) tinte negro semi transparente que le dará un tinte como el aspecto de todas las subvistas debajo.

+1

Si la vista del tinte tiene algo de transparencia, se tiñerá hasta el "debajo" (es decir, se teñirá un área rectangular). Podrías evitar esto usando una capa de máscara, creo. – occulus

Cuestiones relacionadas