iOS动画——辉光效果(Label)



     一,  效果图如下:




     二, 代码如下

     

    FBGlowLabel.h

    

#import <UIKit/UIKit.h>

@interface FBGlowLabel : UILabel

/**
 *  Glow size, default is 0.f.
 */
@property (nonatomic) CGFloat glowSize;

/**
 *  Glow color, default is clear color.
 */
@property (nonatomic, strong) UIColor *glowColor;

/**
 *  Inner glow size, default is 0.f.
 */
@property (nonatomic) CGFloat innerGlowSize;

/**
 *  Inner glow color, default is clear color.
 */
@property (nonatomic, strong) UIColor *innerGlowColor;


@end

FBGlowLabel.m


#import "FBGlowLabel.h"

@implementation FBGlowLabel

- (id)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setup];
    }
    
    return self;
}

- (id)initWithCoder:(NSCoder *)coder {
    
    if (self = [super initWithCoder:coder]) {
        
        [self setup];
    }
    
    return self;
}

- (void)setup {
    
    self.glowSize       = 0.0f;
    self.glowColor      = [UIColor clearColor];
    
    self.innerGlowSize  = 0.0f;
    self.innerGlowColor = [UIColor clearColor];
}

- (void)drawTextInRectForIOS7:(CGRect)rect {
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
    
    [super drawTextInRect:rect];
    UIImage *textImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGContextSaveGState(ctx);
    
    if (_glowSize > 0) {
        
        CGContextSetShadow(ctx, CGSizeZero, _glowSize);
        CGContextSetShadowWithColor(ctx, CGSizeZero, _glowSize, _glowColor.CGColor);
    }
    
    [textImage drawAtPoint:rect.origin];
    CGContextRestoreGState(ctx);
    
    if (_innerGlowSize > 0) {

        UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
        CGContextRef ctx2 = UIGraphicsGetCurrentContext();
        CGContextSaveGState(ctx2);
        CGContextSetFillColorWithColor(ctx2, [UIColor blackColor].CGColor);
        CGContextFillRect(ctx2, rect);
        CGContextTranslateCTM(ctx2, 0.0, rect.size.height);
        CGContextScaleCTM(ctx2, 1.0, -1.0);
        CGContextClipToMask(ctx2, rect, textImage.CGImage);
        CGContextClearRect(ctx2, rect);
        CGContextRestoreGState(ctx2);
        
        UIImage *inverted = UIGraphicsGetImageFromCurrentImageContext();
        CGContextClearRect(ctx2, rect);
        
        CGContextSaveGState(ctx2);
        CGContextSetFillColorWithColor(ctx2, _innerGlowColor.CGColor);
        CGContextSetShadowWithColor(ctx2, CGSizeZero, _innerGlowSize, _innerGlowColor.CGColor);
        [inverted drawAtPoint:CGPointZero];
        CGContextTranslateCTM(ctx2, 0.0, rect.size.height);
        CGContextScaleCTM(ctx2, 1.0, -1.0);
        CGContextClipToMask(ctx2, rect, inverted.CGImage);
        CGContextClearRect(ctx2, rect);
        CGContextRestoreGState(ctx2);
        
        UIImage *innerShadow = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        [innerShadow drawAtPoint:rect.origin];
    }
}

- (void)drawTextInRectForIOS6:(CGRect)rect {
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    
    if (self.glowSize > 0) {
        
        CGContextSetShadow(ctx, CGSizeZero, _glowSize);
        CGContextSetShadowWithColor(ctx, CGSizeZero, _glowSize, _glowColor.CGColor);
    }
    
    [super drawTextInRect:rect];
    CGContextRestoreGState(ctx);
    
    if (_innerGlowSize > 0) {

        UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
        
        CGContextRef ctx2 = UIGraphicsGetCurrentContext();
        [super drawTextInRect:rect];
        
        UIImage *textImage = UIGraphicsGetImageFromCurrentImageContext();
        CGContextClearRect(ctx2, rect);
        
        CGContextSaveGState(ctx2);
        CGContextSetFillColorWithColor(ctx2, [UIColor blackColor].CGColor);
        CGContextFillRect(ctx2, rect);
        CGContextTranslateCTM(ctx2, 0.0, rect.size.height);
        CGContextScaleCTM(ctx2, 1.0, -1.0);
        CGContextClipToMask(ctx2, rect, textImage.CGImage);
        CGContextClearRect(ctx2, rect);
        CGContextRestoreGState(ctx2);
        
        UIImage *inverted = UIGraphicsGetImageFromCurrentImageContext();
        CGContextClearRect(ctx2, rect);
        
        CGContextSaveGState(ctx2);
        CGContextSetFillColorWithColor(ctx2, _innerGlowColor.CGColor);
        CGContextSetShadowWithColor(ctx2, CGSizeZero, _innerGlowSize, _innerGlowColor.CGColor);
        [inverted drawAtPoint:CGPointZero];
        CGContextTranslateCTM(ctx2, 0.0, rect.size.height);
        CGContextScaleCTM(ctx2, 1.0, -1.0);
        CGContextClipToMask(ctx2, rect, inverted.CGImage);
        CGContextClearRect(ctx2, rect);
        CGContextRestoreGState(ctx2);
        
        UIImage *innerShadow = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        [innerShadow drawAtPoint:rect.origin];
    }
}

- (void)drawTextInRect:(CGRect)rect {
    
    if (self.text == nil || self.text.length == 0) {
        
        return;
    }
    
    if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending) {
        
        [self drawTextInRectForIOS7:rect];
        
    } else {
        
        [self drawTextInRectForIOS6:rect];
    }
}

@end




     三,使用方式

   

 FBGlowLabel *glowLabel    = [[FBGlowLabel alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:glowLabel];
    
    glowLabel.text            = @"红红火火";
    glowLabel.textAlignment   = NSTextAlignmentCenter;
    glowLabel.backgroundColor = [UIColor clearColor];
    glowLabel.font            = [UIFont fontWithName:@"Heiti SC" size:40.f];
    glowLabel.textColor       = [[UIColor redColor] colorWithAlphaComponent:0.95f];
    
    glowLabel.glowSize       = 6;
    glowLabel.glowColor      = [UIColor cyanColor];
    
    glowLabel.innerGlowSize  = 3;
    glowLabel.innerGlowColor = [[UIColor blackColor] colorWithAlphaComponent:0.25f];
    
    
    
    FBGlowLabel *glowLabel2    = [[FBGlowLabel alloc] initWithFrame:CGRectMake(50, 500, 300, 40)];
    [self.view addSubview:glowLabel2];
    
    glowLabel2.text            = @"iOS开发 刘成利";
    glowLabel2.textAlignment   = NSTextAlignmentCenter;
    glowLabel2.backgroundColor = [UIColor clearColor];
    glowLabel2.font            = [UIFont fontWithName:@"Heiti SC" size:40.f];
    glowLabel2.textColor       = [[UIColor cyanColor] colorWithAlphaComponent:0.95f];
    
    glowLabel2.glowSize       = 6;
    glowLabel2.glowColor      = [UIColor cyanColor];
    
    glowLabel2.innerGlowSize  = 3;
    glowLabel2.innerGlowColor = [[UIColor blackColor] colorWithAlphaComponent:0.25f];

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 4292-webgl threejs 是一个开源的 JavaScript 库,被广泛应用于 3D 图形的开发,其最大的特点是可以利用 WebGL 技术来让 3D 图形运行在 Web 上。在智慧城市建设中,道路流光效果和建筑模型辉光效果对于增强城市的科技感和时尚感是非常重要的。通过利用 4292-webgl threejs 技术,可以很方便地实现这些效果,让城市更加具有现代感和独特性。 在道路流光效果方面,可以通过使用 4292-webgl threejs 来模拟城市夜景的灯光效果,让道路上的流行车灯光流动起来,从而营造出繁华热闹的城市夜景。 在建筑模型辉光效果方面,通过使用 4292-webgl threejs,可以实现建筑物的多彩辉光效果,让城市更加鲜艳美丽,吸引游客和市民的目光。 此外,特效demo源码的使用可以让开发人员更加高效地完成上述效果的实现,加速智慧城市的建设和发展。 总之,利用 4292-webgl threejs 技术可以让城市更加现代化和美丽化,为智慧城市建设提供了强有力的技术支持。 ### 回答2: 智慧城市是一个新的城市发展模式,借助先进科技手段实现各类城市运行数据的收集、分析和处理,以促进城市治理水平的提升,提高城市居民生活质量。 在智慧城市建设中,道路流光效果和建筑模型辉光效果是比较常见的特效应用。通过对这些特效的实现,可以让城市在视觉上呈现出更具科技感和现代感的感觉。 其中,WebGL和threejs是比较常用的技术工具。WebGL是一种基于Web的3D图形库,通过它可以利用GPU的并行计算能力来实现高性能的图形渲染,包括模型渲染、纹理贴图、光照等。而threejs是基于WebGL的图形库,它提供了更高层次的封装,提供了一系列的3D渲染算法和工具类,可以方便地构建3D场景。 在实现道路流光效果时,我们可以利用threejs提供的ShaderMaterial来实现。ShaderMaterial是一种基于WebGL的材质类型,可以自定义渲染管线中的顶点、像素和片段着色器,并将其与threejs的对象进行关联。 而在实现建筑模型的辉光效果时,则可以利用threejs的BloomPass特效实现。BloomPass是threejs中的一种后处理特效,通过对明亮区域进行高斯模糊,增强高光和光晕效果,实现了辉光效果。 当然,在实现这些特效时,也需要掌握一定的HTML、CSS和JS编程技术,才能完整地展示出智慧城市的效果。此外,为了让广大开发人员更方便地掌握三维图形编程技术,我们还可以通过源码分享的方式来促进交流和共享。 ### 回答3: 现代城市的发展已经不单单是一个单纯的建筑和交通设施的堆砌,更加注重城市的文化和氛围。智慧城市在其中的作用逐渐凸显。在智慧城市的建设中,道路流光和建筑模型的辉光效果是一种可以提升城市氛围的创新做法。这种效果通常是通过webGL和threejs技术来实现的。 webGL是一种基于OpenGL的高性能图形渲染API,旨在为Web进行硬件加速的3D和2D图形渲染,是目前Web引擎标准中的一部分。而threejs是一个JavaScript 3D库。该库使您能够轻松地创建并显示动画化的 3D 图形。threejs作为webGL API的一个包装器,提供了更高层次的抽象,简化了3D绘图的代码实现。 大多数WebGL开发者都应该很熟悉three.js和它的许多出色的功能和演示。对于JavaScript开发人员和设计师,使用three.js代替WebGL API能简化应用程序的堆栈。 在道路流光效果中,通过threejs中的粒子系统,可以快捷地实现流光的效果。这种效果将道路上原本普通的灯光变成了流动的流光线,给持续的夜晚增添了一个梦幻般的氛围。 而在建筑模型中,可以通过三维建模工具,如SketchUp、3DS Max等制作城市建筑物的3D模型。然后通过threejs的灯光工具和材质特效工具,为建筑物添加光效,达到辉光效果。这种效果不仅仅美化了建筑,更可以为城市地标建筑物创造出一个高端、独特的形象。 当然,在threejs中,还有许多不同的特效demo可以使用。这些demo代码可以在github上找到,并在不同的场景中使用。比如裂纹特效、雾化特效、雨雪天特效等等。 总之,webGL和threejs提供的技术,为智慧城市的建设提供了新的思路和工具。道路流光和建筑模型的辉光效果可以提高城市的文化氛围,特效demo则可以让城市更加生动多彩。 我们期待未来这些特效技术和城市的智慧化建设可以越来越广泛的应用到各个领域。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值