`
jsntghf
  • 浏览: 2511959 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

作图功能

    博客分类:
  • iOS
阅读更多

AnimatedPathViewController.h

 

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface AnimatedPathViewController : UIViewController {
	CALayer *_animationLayer;
    CAShapeLayer *_pathLayer;
    CALayer *_penLayer;
}

@property (nonatomic, retain) CALayer *animationLayer;
@property (nonatomic, retain) CAShapeLayer *pathLayer;
@property (nonatomic, retain) CALayer *penLayer;

- (IBAction) replayButtonTapped:(id)sender;
- (IBAction) drawingTypeSelectorTapped:(id)sender;

@end

 

AnimatedPathViewController.m

 

#import <QuartzCore/QuartzCore.h>
#import <CoreText/CoreText.h>
#import "AnimatedPathViewController.h"

@implementation AnimatedPathViewController

@synthesize animationLayer = _animationLayer;
@synthesize pathLayer = _pathLayer;
@synthesize penLayer = _penLayer;

- (void) setupDrawingLayer {
    if (self.pathLayer != nil) {
        [self.penLayer removeFromSuperlayer];
        [self.pathLayer removeFromSuperlayer];
        self.pathLayer = nil;
        self.penLayer = nil;
    }
    
    CGRect pathRect = CGRectInset(self.animationLayer.bounds, 100.0f, 100.0f);
    CGPoint bottomLeft 	= CGPointMake(CGRectGetMinX(pathRect), CGRectGetMinY(pathRect));
    CGPoint topLeft		= CGPointMake(CGRectGetMinX(pathRect), CGRectGetMinY(pathRect) + CGRectGetHeight(pathRect) * 2.0f / 3.0f);
    CGPoint bottomRight = CGPointMake(CGRectGetMaxX(pathRect), CGRectGetMinY(pathRect));
    CGPoint topRight	= CGPointMake(CGRectGetMaxX(pathRect), CGRectGetMinY(pathRect) + CGRectGetHeight(pathRect) * 2.0f / 3.0f);
    CGPoint roofTip		= CGPointMake(CGRectGetMidX(pathRect), CGRectGetMaxY(pathRect));
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:bottomLeft];
    [path addLineToPoint:topLeft];
    [path addLineToPoint:roofTip];
    [path addLineToPoint:topRight];
    [path addLineToPoint:topLeft];
    [path addLineToPoint:bottomRight];
    [path addLineToPoint:topRight];
    [path addLineToPoint:bottomLeft];
    [path addLineToPoint:bottomRight];
    
    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.frame = self.animationLayer.bounds;
    pathLayer.bounds = pathRect;
    pathLayer.geometryFlipped = YES;
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [[UIColor blackColor] CGColor];
    pathLayer.fillColor = nil;
    pathLayer.lineWidth = 10.0f;
    pathLayer.lineJoin = kCALineJoinBevel;
    
    [self.animationLayer addSublayer:pathLayer];
    
    self.pathLayer = pathLayer;
    
    UIImage *penImage = [UIImage imageNamed:@"pen.png"];
    CALayer *penLayer = [CALayer layer];
    penLayer.contents = (id)penImage.CGImage;
    penLayer.anchorPoint = CGPointZero;
    penLayer.frame = CGRectMake(0.0f, 0.0f, penImage.size.width, penImage.size.height);
    [pathLayer addSublayer:penLayer];
    
    self.penLayer = penLayer;    
}

- (void) setupTextLayer {
    if (self.pathLayer != nil) {
        [self.penLayer removeFromSuperlayer];
        [self.pathLayer removeFromSuperlayer];
        self.pathLayer = nil;
        self.penLayer = nil;
    }
    
    CGMutablePathRef letters = CGPathCreateMutable();
    
    CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 72.0f, NULL);
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                           (id)font, kCTFontAttributeName,
                           nil];
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"高海峰"
                                                                     attributes:attrs];
    CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attrString);
	CFArrayRef runArray = CTLineGetGlyphRuns(line);
    
    for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++) {
        CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
        CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
        
        for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++) {
            CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1);
            CGGlyph glyph;
            CGPoint position;
            CTRunGetGlyphs(run, thisGlyphRange, &glyph);
            CTRunGetPositions(run, thisGlyphRange, &position);
            
            {
                CGPathRef letter = CTFontCreatePathForGlyph(runFont, glyph, NULL);
                CGAffineTransform t = CGAffineTransformMakeTranslation(position.x, position.y);
                CGPathAddPath(letters, &t, letter);
                CGPathRelease(letter);
            }
        }
    }
    CFRelease(line);
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointZero];
    [path appendPath:[UIBezierPath bezierPathWithCGPath:letters]];
    
    CGPathRelease(letters);
    CFRelease(font);
    
    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.frame = self.animationLayer.bounds;
	pathLayer.bounds = CGPathGetBoundingBox(path.CGPath);
    
    pathLayer.geometryFlipped = YES;
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [[UIColor blackColor] CGColor];
    pathLayer.fillColor = nil;
    pathLayer.lineWidth = 3.0f;
    pathLayer.lineJoin = kCALineJoinBevel;
    
    [self.animationLayer addSublayer:pathLayer];
    
    self.pathLayer = pathLayer;
    
    UIImage *penImage = [UIImage imageNamed:@"pen.png"];
    CALayer *penLayer = [CALayer layer];
    penLayer.contents = (id)penImage.CGImage;
    penLayer.anchorPoint = CGPointZero;
    penLayer.frame = CGRectMake(0.0f, 0.0f, penImage.size.width, penImage.size.height);
    [pathLayer addSublayer:penLayer];
    
    self.penLayer = penLayer;
}

- (void) startAnimation {
    [self.pathLayer removeAllAnimations];
    [self.penLayer removeAllAnimations];
    
    self.penLayer.hidden = NO;
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 10.0;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [self.pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
    
    CAKeyframeAnimation *penAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    penAnimation.duration = 10.0;
    penAnimation.path = self.pathLayer.path;
    penAnimation.calculationMode = kCAAnimationPaced;
    penAnimation.delegate = self;
    [self.penLayer addAnimation:penAnimation forKey:@"position"];
}

- (void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    self.penLayer.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.animationLayer = [CALayer layer];
    self.animationLayer.frame = CGRectMake(20.0f, 64.0f, 
                                           CGRectGetWidth(self.view.layer.bounds) - 40.0f, 
                                           CGRectGetHeight(self.view.layer.bounds) - 84.0f);
    [self.view.layer addSublayer:self.animationLayer];
    
    [self setupDrawingLayer];
    [self startAnimation];
}

- (void)dealloc {
    self.animationLayer = nil;
    self.pathLayer = nil;
    self.penLayer = nil;
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    self.animationLayer.frame = CGRectMake(20.0f, 64.0f, 
                                           CGRectGetWidth(self.view.layer.bounds) - 40.0f, 
                                           CGRectGetHeight(self.view.layer.bounds) - 84.0f);
    self.pathLayer.frame = self.animationLayer.bounds;
    self.penLayer.frame = self.penLayer.bounds;
}

- (IBAction) replayButtonTapped:(id)sender {
    [self startAnimation];
}

- (IBAction) drawingTypeSelectorTapped:(id)sender {
    UISegmentedControl *drawingTypeSelector = (UISegmentedControl *)sender;
    switch (drawingTypeSelector.selectedSegmentIndex) {
        case 0:
            [self setupDrawingLayer];
            [self startAnimation];
            break;
        case 1:
            [self setupTextLayer];
            [self startAnimation];
            break;
    }
}

@end

 

效果图:

分享到:
评论

相关推荐

    MATLAB的二维作图功能分析.pdf

    MATLAB作为一种高级技术计算语言和交互式环境,提供了一系列作图功能,本文将分析和总结MATLAB的二维作图功能。 MATLAB中的二维作图主要依靠plot命令来实现,该命令提供了多种格式来绘制图形。例如,使用plot(Y)时...

    MATLAB作图功能在高数数学教学中的应用分析.pdf

    MATLAB是一种高性能的数值计算和可视化软件,广泛应用于理工科教学和科研领域,特别是在数学教学中,其强大的作图功能为理解和掌握数学概念提供了直观的支持。以下将详细介绍MATLAB在高等数学教学中的应用,以及其...

    实验数学四MATLAB的作图功能PPT学习教案.pptx

    实验数学四MATLAB的作图功能是学习MATLAB的重要部分,它涵盖了多种二维图形的绘制方法,包括条形图、直方图、线性图形、极坐标图形等。以下是对这些知识点的详细解释: 1. **二维绘图函数**: - `bar`:用于绘制...

    实验数学四MATLAB的作图功能.pptx

    MATLAB是一种强大的数值计算和数据分析软件,其绘图功能是其核心特性之一,广泛应用于实验数学、科学研究和工程计算等领域。以下将详细讲解MATLAB的二维绘图功能及其相关知识点。 1. **二维绘图函数**: - `bar`:...

    Mathematica的综合、作图与拟合功能

    Mathematica 软件系统是一款功能强大的计算机软件系统,主要功能包括数学符号运算、强大的作图功能和丰富的子程序软件包。用户可以使用 Mathematica 软件系统进行数学运算、数据可视化和参数拟合等操作。 ...

    R语言作图大全

    《R语言作图大全》是一本介绍R语言中作图功能的电子书籍。R语言作为一种统计分析工具,其作图功能非常强大,适用于数据可视化、统计图形的绘制和探索性数据分析。这本书的作者谢益辉在2010年8月13日声明本书采用...

    MFC 程序设计作图范例

    本教程"МFC 程序设计作图范例"着重讲解如何利用MFC来实现图形界面的开发,特别是作图功能。 1. MFC的基本架构:MFC基于事件驱动模型,包含了视图(View)、文档(Document)、框架(Frame)等核心组件。视图负责...

    科研作图-好看的科研作图代码demo(matlab)论文作图

    MATLAB的软件/插件标签表明,可能还包含了MATLAB的一些特定工具箱或者函数库,比如图像处理工具箱、信号处理工具箱等,用于扩展MATLAB的作图功能,处理更复杂的数据类型和场景。 至于"毕业设计"标签,意味着这些...

    屏幕作图源码vc++

    屏幕作图源码VC++是基于C++编程语言实现的在计算机屏幕上绘制图形的程序。...这个压缩包文件可能包含的就是这样的一段实现屏幕作图功能的C++代码,你可以通过阅读和分析代码来进一步学习和实践这些技术。

    Matlab用于几何作图

    MATLAB是一种强大的数值计算和数据可视化软件,尤其在数学建模和数学实验中,它的作图功能被广泛应用。MATLAB的绘图主要是基于点的坐标,通过连接这些点来形成曲线或曲面,使得复杂的数学模型得以直观地展现。 在三...

    MATLAB作图MATLAB作图.doc

    MATLAB作图功能详解 MATLAB是一款功能强大且广泛应用于科学计算、数据分析和可视化的软件。它提供了强大的作图功能,能够生成多种类型的图形,包括二维图形、三维图形、曲面图形等。本文将对MATLAB的作图功能进行...

    Maple中作图

    首先,二维函数作图是Maple作图功能中的核心部分。在Maple中,使用plot命令可以绘制一元函数在指定区间上的二维图形。plot命令的基本用法非常简单,如果省略范围和选项,系统会自动选取最佳设置。例如,plot(sin(x)/...

    Matlab作图方法的基本讲解 PPT

    MATLAB是一款强大的数学计算和数据分析软件,其作图功能强大且易于使用,能够帮助用户直观地理解和展示数据。本篇主要介绍MATLAB绘图的基本操作,涵盖二维和三维数据曲线图、图形修饰以及图像处理等方面。 首先,...

    简易作图器1.1

    【简易作图器1.1】是一款基于平面作图功能的软件,专为用户提供便捷的图形绘制体验。作为改进版,它在原有的基础上可能增加了更多功能或者优化了用户体验,但遗憾的是,此版本并未包含源代码,因此用户无法查看或...

    matlab的作图经典中的经典

    以上仅是MATLAB作图功能的一部分,实际上MATLAB还支持更多高级特性,如图例、颜色映射、轴设置、网格线、图层控制等,能够满足各种复杂的图形需求。熟练掌握这些作图技巧,能帮助用户更直观地理解和分析数据,是...

    matlab作图_matalb;作图_

    总之,MATLAB的作图功能强大,能够满足各种数据可视化需求。无论是简单的曲线绘制,还是复杂的三维图像,MATLAB都能轻松应对。通过熟练掌握这些知识点,你将能够更加有效地利用MATLAB进行数据分析和科学研究。

    Origin数据处理与科学作图完美版资料.ppt

    Origin数据处理与科学作图完美版资料.ppt是 OriginLab公司出品的一款专业的数据处理和科学作图软件,提供了强大的数据分析和科学作图功能。该软件具备了多种数据处理和分析工具,包括回归分析、拟合、数据处理、光谱...

    matlab的二三维作图代码..rar

    综上所述,MATLAB的二三维作图功能强大且灵活,适用于科研、工程和教学等多个领域。掌握这些基本的绘图方法和定制技巧,将极大地提升数据可视化的效率和质量。通过提供的"matlab的二三维作图代码"压缩包,你可以找到...

    最新的好用的txt作图软件

    这样的特性对于个人用户和小型团队来说非常友好,他们可以在不花费任何费用的情况下,享受到高效且易于操作的作图功能。 【标签】:“txt 作图软件”表明了这个软件的核心功能,即它专注于处理txt文件中的数据,并...

    数学建模 MATLAB作图

    MATLAB提供了强大的作图功能,使得用户能够方便地创建各种类型的图形,从简单的曲线到复杂的三维图像。 首先,MATLAB的基础绘图是通过描点和连线的方式完成的。在画出一个曲线图形之前,你需要确定图形上一系列点的...

Global site tag (gtag.js) - Google Analytics