2011-12-05 25 views
7

Bất kỳ cách nào bạn có thể tô màu cho UIView? Không phải màu nền, nhưng toàn bộ UIView với tất cả các phần phụ của nó.Tô màu UIView với tất cả các phần phụ của nó

ví dụ: - UIView với hoạt ảnh sao quay, tức là hình dạng UIView liên tục thay đổi.

+2

Bạn có thể thêm một subview mới để lấp đầy toàn bộ khung nhìn, thiết lập một màu bg để subview đó và làm cho nó một chút minh bạch .... mặc dù nó sẽ không được 'pha màu' thực sự. –

+0

UIView mục tiêu không phải là hình chữ nhật, nó có hình dạng độc đáo, ví dụ: một ngôi sao. –

+0

Bạn có UIView hình ngôi sao với các bản phụ trong đó? –

Trả lời

7

Cuối cùng tôi đã tạo ra một loại UIView cho phép pha màu của một UIView, mà không cần điền vào hình chữ nhật UIView, ở đây nó là:

Đưa một đại diện hình ảnh của UIView, sau đó màu sắc nó trong UIColor cho , thể loại này được tạo ra để tái tạo một hành vi nổi bật mặc định UIButton , do đó, nó cũng đi kèm với một phương pháp có thể kích hoạt hành vi và để cho danh mục đó xử lý tất cả các phương pháp chạm.

//------------------ .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

Để tô màu UIView, hãy thêm một chế độ xem phụ khác lên trên cùng với cùng một frame và bộ trong suốt alpha.

Ví dụ giả sử loại container của bạn xem được gọi là containerView, bạn sẽ làm như sau:

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

[containerView addSubview:overlayView]; 

này cung cấp cho bạn một cách minh bạch (0.5f) màu đen bán mà sẽ cung cấp một màu như xuất hiện để tất cả các bản xem dưới bên dưới.

+1

Nếu chế độ xem để pha màu có độ trong suốt, màu này sẽ được tô màu đến 'bên dưới' (nghĩa là khu vực hình chữ nhật được nhuộm màu). Bạn có thể giải quyết vấn đề này bằng cách sử dụng lớp mặt nạ, tôi nghĩ vậy. – occulus

Các vấn đề liên quan