iPhone-在哪里放置调节CALayer动画的代码? [英] iphone - Where to put code regulating CALayer animation?

查看:125
本文介绍了iPhone-在哪里放置调节CALayer动画的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法在我的自定义图层PatternLayer的drawInContext方法中绘制了一条条.但是,我想为三件事做动画,并且对如何放置自定义动画规则代码感到困惑.这是我的自定义层的界面:

I have managed to draw a bar in the drawInContext method of my custom layer, PatternLayer. However, I would like to animate three things and am confused about where I can place the custom animation regulation code. Here is the interface of my custom layer:

@interface PatternLayer : CALayer

@property (nonatomic) CGFloat percentage;
@property (nonatomic) CGFloat shineProgress;
@property (nonatomic, strong) UIColor* barColor;

@property (nonatomic) CGFloat strokeWidth;
@property (nonatomic, strong) UIColor* strokeColor;

简而言之,drawInContext函数使用百分比来计算要填充多少条的剪切点,barColor用作填充颜色,而shineProgress是沿着条放置高光的位置,用于渐变.

In a nutshell, the drawInContext function uses the percentage to calculate the clipping point for how much of the bar to fill, the barColor is used as the fill color, and the shineProgress is the location along the bar to place the highlight, for gradient purposes.

在动画的一部分中,一个Action触发ViewController更新百分比和barColor.

In one part of the animation, an Action triggers the ViewController to update the percentage and barColor.

在第二个动画中,shineProgress连续从0到1.0循环到0到1.0 ...无穷无尽.

In the second animation, the shineProgress continually cycles from 0 to 1.0 to 0 to 1.0 ... ad infinitum, with no end.

我阅读的所有示例都在声明CABasicAnimation或CAKeypointAnimation,但是没有人指定将对象放置在何处.我有一个带有一个PatternView的ViewController,而PatternView有一层PatternLayer.根据我对UIAction等的了解,我的用户交互代码进入了View Controller,但是我不确定如何构建动画,在哪里放置循环shineProgress的代码,等等.我需要吗使相同的两个属性(百分比,barColor)也暴露在PatternView中,以便ViewController可以访问它们?然后,如何将访问信息传达给该层?如果尝试访问百分比之类的自定义属性,我将无法访问,因为PatternView中的self.layer是通用的CALayer *.它确实指向了PatternLayer,但是我似乎无法通过强制转换来说明这一点.

All of the examples I have been reading have been declaring CABasicAnimation or CAKeypointAnimation, but nobody ever specifies where to put that object. I have a ViewController with one PatternView, and the PatternView has one layer, the PatternLayer. From what I understand about UIActions and the such, my user interaction code goes into the View Controller, but then I'm not sure how to go about building an Animation, where to put the code that cycles the shineProgress, etc. Do I need to make the same two properties (percentage, barColor) exposed in the PatternView as well, so that the ViewController can access them? Then how do the accesses get communicated to the layer? If I try to access one of the custom properties like percentage, I can't, because self.layer in PatternView is a generic CALayer*. It really points to a PatternLayer, but I can't seem to tell it that by casting.

从本质上讲,我阅读了所有这些教科书,这些教科书讨论了如何编写层代码,drawInContext,initWithLayer,needsDisplayForKey,actionForKey等,但是我不知道如何在更高级别触发这些功能,例如传入百分比&图层的barColor值,循环shineProgress等.如何运行永恒的动画?如何将参数从视图传递到图层?任何对此的指导将不胜感激.我真的不想像许多网络示例一样添加子层---只需要在我的视图上附加一层即可.

In essence, I read all these textbooks that discuss how to write the layer code, drawInContext, initWithLayer, needsDisplayForKey, actionForKey, etc., but I have no clue how to trigger those functions at a higher level, such as passing in the percentage & barColor values to the layer, cycling the shineProgress, etc. How do I get the eternal animation running? How do I pass parameters to the layer from the view? Any guidance on that would be much appreciated. I don't really want to add Sublayers like a lot of web examples --- just work with the one layer attached to my view.

谢谢!

感谢您的帮助.

推荐答案

这不是确切的答案,但由于没有其他人在回答,因此我将其发布.我相信您只需阅读 GitHub上的类似控件.

It's not an exact answer, but I'll post it since nobody else is answering. I'm sure you can learn how to do it by simply reading the code of a similar control on Github.

基本上,drawInContext:不会设置任何动画,它仅知道如何根据图层属性的当前值绘制图层. CABasicAnimation更改图层属性的值(根据其属性fromValuetoValue),然后调用drawInContext:.它做了很多次(基于durationtimingFunction),从而产生了动画的错觉.

Basically, drawInContext: doesn't animate anything, it only knows how to draw the layer based on the current values of the layer's properties. CABasicAnimation changes values of the layer's properties (according to its properties fromValue and toValue) and then calls drawInContext:. It does so many times (it's based on duration and timingFunction), which creates the illusion of an animation.

希望这将帮助您走上正确的轨道.

Hope this will help you to get on the right track.

这篇关于iPhone-在哪里放置调节CALayer动画的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆