objective-c - 用它的所有 subview 给 UIView 着色

标签 objective-c ios cocoa-touch uiview

有什么方法可以为 UIView 着色?不是背景颜色,而是整个 UIView 及其所有 subview 。

例如 - 带有星星旋转动画的 UIView,即 UIView 形状不断变化。

最佳答案

最后我创建了一个 UIView 类别,它可以对 UIView 进行着色, 不填充 UIView 矩形,这里是:

Takes an image representation of the UIView, then colors it in the given UIColor, this category was created to replicate a UIButton default highlight behavior, so it also comes with a method that can activate that behavior and let the category handle all touch methods.

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

关于objective-c - 用它的所有 subview 给 UIView 着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8389193/

相关文章:

iphone - 将应用更改为仅 iPhone 部署

ios - 删除自定义标签栏上的行

objective-c - 无需采取任何行动的 Cocoa 消息

ios - SwiftUI 文本在设置为抵抗时被截断?

objective-c - 如何使用 OCMock for iOS 模拟私有(private)属性?

ios - 是否可以让 UIButton 由文本组成,但最后有图像?

ios - 在 Firebase 中访问用户 ID 中的子 ID

jquery - 100% DIV 不拉伸(stretch) iphone 3gs 和 4

objective-c - Obj-C、UITableView UI 改进以处理大量记录,也就是点击下一步获取更多行?

ios - 在 Mapbox (iOS) 上将方向箭头添加到当前位置