iOS:CAShapeLayer绘制非直视图像并为其形状设置动画

iOS:CAShapeLayer绘制非直视图像并为其形状设置动画,第1张

概述我一直在阅读有关CAShapeLayer的文档,但我仍然不太明白. 根据我的理解,Layer始终是扁平的,其大小始终是矩形. 另一方面,CAShapeLayer允许您定义不仅仅是矩形的图层.它可以是圆形,三角形等,只要您将它与UIBezierPaths一起使用即可. 我的理解是在这里吗? 我想到的是,例如,一个网球从屏幕边缘反d(很容易),但我想展示一个不使用图像动画的小动画 – 我希望它显示一点 我一直在阅读有关CAShapeLayer的文档,但我仍然不太明白.
根据我的理解,Layer始终是扁平的,其大小始终是矩形.

另一方面,CAShapeLayer允许您定义不仅仅是矩形的图层.它可以是圆形,三角形等,只要您将它与UIBezIErPaths一起使用即可.

我的理解是在这里吗?

我想到的是,例如,一个网球从屏幕边缘反d(很容易),但我想展示一个不使用图像动画的小动画 – 我希望它显示一点“挤压“喜欢动画,因为它会碰到屏幕的边缘然后反d.我没有使用网球图像.只是一个黄色的圆圈.

我是否正确使用CAShapeLayer完成此 *** 作?如果是这样,请你提供一个小例子吗?谢谢.

解决方法 虽然您确实使用路径来定义图层的形状,但它仍然使用用于定义框架/边界的矩形创建.这是一个让你入门的例子:

TennisBall.h:

#import <UIKit/UIKit.h>@interface TennisBall : UIVIEw- (voID)bounce;@end

TennisBall.m:

#import <QuartzCore/QuartzCore.h>#import "TennisBall.h"@implementation TennisBall+ (Class)layerClass{    return [CAShapeLayer class];}- (ID)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        [self setupLayer];    }    return self;}/* Create the tennis ball */- (voID)setupLayer{    CAShapeLayer *layer = (CAShapeLayer *)self.layer;    layer.strokecolor = [[UIcolor blackcolor] CGcolor];    layer.fillcolor = [[UIcolor yellowcolor] CGcolor];    layer.linewidth = 1.5;    layer.path = [self defaultPath];}/* Animate the tennis ball "bouncing" off of the sIDe of the screen */- (voID)bounce{    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];    animation.duration = 0.2;    animation.timingFunction = [camediatimingFunction functionWithname:kcamediatimingFunctionEaseInEaSEOut];    animation.fromValue = (__brIDge ID)[self defaultPath];    animation.tovalue = (__brIDge ID)[self compressedPath];    animation.autoreverses = YES;    [self.layer addAnimation:animation forKey:@"animatePath"];}/* A path representing the tennis ball in the default state */- (CGPathref)defaultPath{    return [[UIBezIErPath bezIErPathWithovalInRect:self.frame] CGPath];}/* A path representing the tennis ball is the compressed state (during the bounce) */- (CGPathref)compressedPath{    CGRect newFrame = CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.wIDth * 0.85,self.frame.size.height);    return [[UIBezIErPath bezIErPathWithovalInRect:newFrame] CGPath];}@end

现在,当你想要使用它时:

TennisBall *ball = [[TennisBall alloc] initWithFrame:CGRectMake(10,10,100,100)];[self.vIEw addSubvIEw:ball];[ball bounce];

请注意,这需要进行扩展,以便您可以从不同角度“反d”等,但它应该让您指向正确的方向!

总结

以上是内存溢出为你收集整理的iOS:CAShapeLayer绘制非直视图像并为其形状设置动画全部内容,希望文章能够帮你解决iOS:CAShapeLayer绘制非直视图像并为其形状设置动画所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1097689.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存