草庐IT

iphone - 动画绘制线条

coder 2023-09-21 原文

我正在尝试通过以下方式为线条的绘制设置动画:

.h

CAShapeLayer *rootLayer;
CAShapeLayer *lineLayer;
CGMutablePathRef path;

.m

 path = CGPathCreateMutable();
 CGPathMoveToPoint(path, nil, self.frame.size.width/2-100, 260);
 CGPathAddLineToPoint(path, nil, self.frame.size.width/2+100.0, 260);    
 CGPathCloseSubpath(path);

 self.rootLayer = [CALayer layer];
 rootLayer.frame = self.bounds;
[self.layer addSublayer:rootLayer];

 self.lineLayer = [CAShapeLayer layer];
[lineLayer setPath:path];
[lineLayer setFillColor:[UIColor redColor].CGColor];
[lineLayer setStrokeColor:[UIColor blueColor].CGColor];
[lineLayer setLineWidth:1.5];
[lineLayer setFillRule:kCAFillRuleNonZero];
[rootLayer addSublayer:lineLayer];

[self performSelector:@selector(startTotalLine) withObject:nil afterDelay:1.5];

- (void)startTotalLine
{    
      CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"animatePath"];
      [animation setDuration:3.5];
       animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
      [animation setAutoreverses:NO];
      [animation setFromValue:(id)path];
      [animation setToValue:(id)path];
      [lineLayer addAnimation:animation forKey:@"animatePath"];    
}

在调用 startTotalLine 方法之前绘制的线条。 此外,startTotalLine 方法不会影响该行。

我希望它为从右到左的线条图设置动画。

最佳答案

我会用动画属性来做。

为了实现这一点,我将创建一个自定义的 CALayer 类 - 我们称它为 LineLayer。定义一个 startPoint 属性和一个 length 属性。然后我会将 length 属性配置为“可动画化”。

其代码如下所示:

// LineLayer.h
@interface LineLayer: CALayer

@property (nonatomic, assign) int length;
// This was omitted from the SO code snippet.
@property (nonatomic, assign) CGPoint startPoint;

@end

// LineLayer.m
@implementation LineLayer

@synthesize length = _length;
// This was omitted from the SO code snippet.
@synthesize startPoint= _startPoint;

- (id) initWithLayer:(id)layer 
{
    if(self = [super initWithLayer:layer]) 
    {
        if([layer isKindOfClass:[LineLayer class]]) 
        {
            // This bit is required for when we CA is interpolating the values.
            LineLayer *other = (LineLayer*)layer;
            self.length = other.length;
            self.startPoint = other.startPoint; // This was omitted.
        }
    }
    return self;
}

+ (BOOL)needsDisplayForKey:(NSString *)key 
{
    if ([key isEqualToString:@"length"]) {
        return YES;
    }
    return [super needsDisplayForKey:key];
}

- (void) setLength:(int)newLength
{
    if (newLength < 0) {
        return; // Fail early.
    }
    _length = newLength; 
    [self setNeedsDisplay]; 
} 

/*
    This should have been drawInContext:(CGContextRef)context
    - (void) drawRect:(CGRect) rect 
*/
- (void) drawInContext:(CGContextRef)context
{
    //...Do your regular drawing here.

    // This was omitted from the SO code snippet.
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetLineWidth(context, 2);
    CGContextMoveToPoint(context, _startPoint.x, _startPoint.y);
    CGContextAddLineToPoint(context, _startPoint.x + _length, _startPoint.y);
    CGContextStrokePath(context);
}

@end

然后在您的 View Controller 中,您可以像这样使用 LineLayer:

- (void)viewDidLoad
{
    [super viewDidLoad];

    LineLayer *lineLayer = [LineLayer new];

    // This was omitted from the SO code snippet.
    lineLayer.frame = CGRectMake(0, 0, 320, 480);
    [lineLayer setNeedsDisplay];
    // ---

    lineLayer.startPoint = CGPointMake(0, 100);
    lineLayer.length = 0;

    [self.view.layer addSublayer:lineLayer];

    // Now animate the changes to the length property
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"length"];
    anim.duration = 5; // Change should table about 5 mins.
    anim.fromValue = [NSNumber numberWithInt:0];
    anim.toValue = [NSNumber numberWithInt:200];

    [lineLayer addAnimation:anim forKey:@"animateLength"];
    lineLayer.length = 200;
    //Do clean up below...
}

快乐编码:)

关于iphone - 动画绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10603391/

有关iphone - 动画绘制线条的更多相关文章

  1. Unity 3D 制作开关门动画,旋转门制作,推拉门制作,门把手动画制作 - 2

    Unity自动旋转动画1.开门需要门把手先动,门再动2.关门需要门先动,门把手再动3.中途播放过程中不可以再次进行操作觉得太复杂?查看我的文章开关门简易进阶版效果:如果这个门可以直接打开的话,就不需要放置"门把手"如果门把手还有钥匙需要旋转,那就可以把钥匙放在门把手的"门把手",理论上是可以无限套娃的可调整参数有:角度,反向,轴向,速度运行时点击Test进行测试自己写的代码比较垃圾,命名与结构比较拉,高手轻点喷,新手有类似的需求可以拿去做参考上代码usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;u

  2. LVGL V8动画 - 2

    动画/*INITIALIZEANANIMATION 初始化一个动画*-----------------------*/lv_anim_ta;lv_anim_init(&a);/*MANDATORYSETTINGS 必选设置*------------------*//*Setthe"animator"function 设置“动画”功能*/lv_anim_set_exec_cb(&a,(lv_anim_exec_xcb_t)lv_obj_set_x);/*Setthe"animator"function*/lv_anim_set_var(&a,obj);/*Lengthoftheanim

  3. ruby - Carrierwave + MiniMagick - 如何将动画 GIF 压缩到第一帧? - 2

    有人知道如何使用Carrierwave+MiniMagick将动画GIF压缩到第一帧吗? 最佳答案 我认为MiniMagick有一些变化,因为我只花了三个小时试图找出为什么Andrey的代码对我不起作用。我收到以下错误:ActiveRecord::RecordInvalid(Validationfailed:ImageFailedtomanipulatewithMiniMagick,maybeitisnotanimage?OriginalError:Command("mogrify-scene/var/folders/0o/0oqN

  4. ruby - 在 Ruby 中绘制点和矩形 - 2

    我正在寻找一种简单的方法来绘制大约10个点和矩形,以便能够查看我的算法哪里出了问题。我查看了gnuplot,但似乎绘制矩形特别糟糕。 最佳答案 SVG(MDNTutorial)是一种非常简单的基于文本(XML)的格式,您可以使用Ruby轻松生成它,而无需任何SVG库,并可以在任何现代Web浏览器中查看。这是一个示例:通过字符串插值的SVG点points=(0..5).map{[rand(100)-50,rand(100)-50]}puts#{points.map{|x,y|""}.join("\n")}ENDSVG输出:http:/

  5. iphone - 扩展 restful_authentication/AuthLogic 以支持匿名 iPhone 的延迟登录的最佳方法是什么? - 2

    我正在构建一个与RubyonRails后端对话的iPhone应用程序。RubyonRails应用程序还将为Web用户提供服务。restful_authentication插件是提供快速和可定制的用户身份验证的绝佳方式。但是,我希望iPhone应用程序的用户在新列中存储一个由手机的唯一标识符([[UIDevicedevice]uniqueIdentifier])自动创建的帐户。稍后,当用户准备好创建用户名/密码时,帐户将更新为包含用户名和密码,iPhone唯一标识符保持不变。用户在设置用户名/密码之前不能访问该网站。然而,他们可以使用iPhone应用程序,因为该应用程序可以使用它的标识符

  6. iphone - 设计和 Rails 3 中的 http 身份验证 - 2

    我有一个使用deviseonrails3的应用程序。我想启用http身份验证,以便我可以从iPhone应用程序向我的网络应用程序进行身份验证。如何从我的iPhone应用程序进行身份验证以进行设计?这安全吗?还是我应该进行不同的身份验证? 最佳答案 从设计的角度来看,您有3个选择:1)使用基本的http身份验证:您的iPhone应用程序有一个secretkey-这是在您的iPhone应用程序代码中烘焙的-用于对网络应用程序的每个请求进行身份验证。Google搜索:“设计基本的http身份验证”2)您可以通过在您的iPhone应用程序中

  7. ruby-on-rails - 如何在 Ajax 请求处理期间显示动画图标 - Rails 3 - 2

    我正在尝试为每个ajax请求显示一个加载指示器,我在Rails3应用程序中工作。HTML:"loading-indicator",:style=>"display:none")%>CSS:#loading-indicator{position:absolute;left:10px;top:10px;}loading.js:我放在assest/javascripts/$(document).ready(function(){$(document).ajaxSend(function(event,request,settings){$('#loading-indicator').show(

  8. iphone - 在没有 Mac 的情况下开发 iPhone 应用程序? - 2

    这个问题在这里已经有了答案:关闭13年前。PossibleDuplicates:HowcanIdevelopforiPhoneusingaWindowsdevelopmentmachine?我想为我妻子的手机构建一个iPhone应用程序,但我对购买Mac作为一次性工作的开发平台不感兴趣。应用程序:应该在iPhone上独立运行(即没有网络连接)完全可以接受使用iPhoneJavascript库之一创建的GUI会做一些数据库IO来读取和更新数据没有商业值(value),永远不会被任何人使用这是我的想法:越狱iPhone在iPhone上安装Ruby+Sinatra使用Sinatra编写应用程

  9. iphone - iPhone 原生应用的测试驱动设计 - 2

    我正在试验iPhoneSDK并在Nic博士的rbiPhoneTest项目中做一些TDD。我想知道有多少人(如果有的话)成功地使用了这个或任何其他iPhone/Cocoa测试框架?更重要的是,我想知道如何最好地断言专有的二进制请求/响应协议(protocol)。这个想法是通过网络发送二进制请求并接收二进制响应。请求和响应是使用byteand'ing和or'ing创建的。我正在使用黄金副本模式来测试我的请求。这是我到目前为止所拥有的。不要笑,因为我是ObjectiveC和Ruby的新手:requireFile.dirname(__FILE__)+'/test_helper'require'

  10. iphone - 如何从视频中提取方向信息? - 2

    在网络上浏览了大量文档后,iPhone似乎总是以480x360的纵横比拍摄视频,并在视频rails上应用变换矩阵。(480x360可能会改变,但对于给定设备而言始终相同)这是一种在iOS项目中修改ffmpeg源代码并访问矩阵http://www.seqoy.com/correct-orientation-for-iphone-recorded-movies-with-ffmpeg/的方法这是在iOS-4中查找转换矩阵的更清晰的方法Howtodetect(iPhoneSDK)ifavideofilewasrecordedinportraitorientation,orlandscape.

随机推荐