Objective c 为UIView及其所有子视图着色
你可以用什么方法给UIView着色?不是背景色,而是整个UIView及其所有子视图Objective c 为UIView及其所有子视图着色,objective-c,ios,cocoa-touch,uiview,Objective C,Ios,Cocoa Touch,Uiview,你可以用什么方法给UIView着色?不是背景色,而是整个UIView及其所有子视图 e、 g-具有恒星旋转动画的UIView,即UIView形状不断变化。要着色UIView,请在其顶部添加另一个子视图,该子视图具有相同的帧和alpha透明度集 例如,假设您的容器类型视图名为containerView,您可以执行以下操作: CGRect overlayFrame = containerView.frame; UIView *overlayView = [UIView alloc] initWith
e、 g-具有恒星旋转动画的UIView,即UIView形状不断变化。要着色
UIView
,请在其顶部添加另一个子视图,该子视图具有相同的帧和alpha透明度集
例如,假设您的容器类型视图名为containerView
,您可以执行以下操作:
CGRect overlayFrame = containerView.frame;
UIView *overlayView = [UIView alloc] initWithFrame:overlayFrame];
overlayView.alpha = 0.5f;
overlayView.color = [UIColor blackColor];
[containerView addSubview:overlayView];
这将为您提供半透明(0.5f)黑色色调,使下面的所有子视图都具有类似色调的外观。最终,我创建了一个UIView类别,可以对UIView进行着色,
不填充UIView矩形,如下所示:
获取UIView的图像表示形式,然后在
给定UIColor,创建此类别是为了复制UIButton
默认高亮显示行为,因此它还附带了一个可以激活该行为的方法
并让类别处理所有触摸方法
/-------------.h接口文件--------//
//
//UIView UIView\U色调h
//婴儿卡
//
//由约格夫·雪莉于2012年8月10日创作。
//版权所有(c)2012年\uuuu MyCompanyName\uuuuu。版权所有。
//
#进口
#定义TintColorClassUIButton[UIColorWithWhite:0.0 alpha:0.5]
@界面视图(色调)
//不应使用属性,请使用以下方法
@属性(非原子,保留)UIColor*tintColor;
@属性(非原子,保留)UIImageView*tintImageView;
@属性(非原子,赋值)BOOL-tintonotuch-active;
-(空白)色调颜色:(UIColor*)颜色;
-(无效)透明色调;
-(void)启用TintOnTouchWithColor:(UIColor*)颜色;
-(无效)禁止触碰;
-(UIImage*)图像表示;
-(UIImage*)带有TintColor的图像表示:(UIColor*)颜色;
@结束
//------------------.m实现文件--------//
//UIView UIView\U Tint.m
//婴儿卡
//
//由约格夫·雪莉于2012年8月10日创作。
//版权所有(c)2012年\uuuu MyCompanyName\uuuuu。版权所有-尽情享受吧!
//
#导入“UIView Tint.h”
#进口
#进口
静态字符常量*常量tintImageViewKey=“tintImageView”;
静态字符常量*const tintColorKey=“tintColorKey”;
静态字符常量*常量tintOnTouchActiveKey=“tintOnTouchActive”;
@实现UIView(色调)
@动态图像视图;
@动态着色;
@动力触控;
-(void)启用TintOnTouchWithColor:(UIColor*)颜色
{
self.tintColor=颜色;
self.tintOnTouchActive=TRUE;
}
-(无效)禁用TintOnTouch
{
self.tintOnTouchActive=FALSE;
}
-(空白)色调颜色:(UIColor*)颜色
{
如果(![self.tintColor isEqual:color]| |!self.tintImageView)
{
self.tintColor=颜色;
UIImage*tintImage=[self-imageRepresentationWithTintColor:self.tintColor];
self.tintImageView=[[UIImageView alloc]initWithImage:tintImage]autorelease];
}
[self addSubview:self.tintmageview];
}
-(无效)透明色调
{
[self.tinImageView从SuperView移除];
}
-(无效)ClearTintWithSecondDelay:(浮动)延迟
{
[self-performSelector:@selector(clearTint)with object:self-afterDelay:delay];
}
#pragma标记-触摸切换
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件
{
[超级触摸开始:触摸事件:事件];
if(自激活)
[self-tintToColor:self.tintColor];
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event
{
[超级触控:触控事件:事件];
if(自激活)
[秒延迟自清除:0.05];
}
-(无效)触控取消:(NSSet*)触控事件:(UIEvent*)事件
{
[超级触控取消:触控事件:事件];
if(自激活)
[秒延迟自清除:0.05];
}
#pragma标记-着色部分
-(UIImage*)图像表示法
{
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderContext:UIGraphicsGetCurrentContext()];
UIImage*img=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
返回img;
}
-(UIImage*)带有TintColor的图像表示:(UIColor*)颜色
{
UIImage*viewImage=[自映像表示];
viewImage=[自着色图像:viewImage UsingColor:color];
返回视图图像;
}
-(UIImage*)着色图像:(UIImage*)图像使用颜色:(UIColor*)着色{
UIGraphicsBeginImageContextWithOptions(image.size,NO,[[UIScreen mainScreen]比例]);
CGRect drawRect=CGRectMake(0,0,image.size.width,image.size.height);
[图像drawInRect:drawRect];
[色彩设置];
UIRectFillUsingBlendMode(drawRect、KCGBlendModeSourceTop);
UIImage*TinteImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
返回着色图像;
}
#pragma标记-关联引用的动态setter/getter
-(void)setTintImageView:(UIImageView*)tintImageView
{
objc_setAssociatedObject(self、tintImageViewKey、tintImageView、objc_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UIImageView*)tintImageView
{
返回objc_getAssociatedObject(self,tintImageViewKey);
}
-(UIColor*)淡色
{
返回objc_getAssociatedObject(self,tintColorKey);
}
-(void)setTintColor:(UIColor*)tintColor
{
objc_setAssociatedObject(self、tintColorKey、tintColor、objc_ASSOCIATION_RETAIN_NONATOMIC);
}
-(BOOL)TintonTouch活动
{
返回[objc_getAssociatedObject(self,tintOnTouchActiveKey)布尔值];
}
-(无效)设置tintOnTouchActive:(BOOL)tintOnTouchActive
{
objc_setAssociatedObject(self,tintOnTouchActiveKey,[NSNumber Number WithBool:tintOnTouchActive],objc_ASSOCIATION_RETAIN_NONATOMIC);
}
@结束
您可以添加一个新的子视图来填充整个图幅,为该子视图设置背景颜色并使其略微透明。。。。虽然它实际上不会“着色”。目标UIView不是矩形,它有一个独特的形状,例如星形。您有一个星形UIView,其中包含子视图?仅举一个例子。。。它可以是任何非矩形形状。我想我可以从视图中创建一个图像,完全着色,然后在UIView上混合,但是我
//------------------ .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