加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – CABasicAnimation和自定义类型

发布时间:2020-12-16 06:56:04 所属栏目:百科 来源:网络整理
导读:我对CoreAnimation不太熟悉,所以我希望我错过了一些非常简单的东西.我想以简单的方式为NSView的自定义属性(NSGradient)设置动画,使用[[view animator] setGradient:gradient] ;.我定义了(id)defaultAnimationForKey:(NSString *)键并返回了一个简单的CABas
我对CoreAnimation不太熟悉,所以我希望我错过了一些非常简单的东西.我想以简单的方式为NSView的自定义属性(NSGradient)设置动画,使用[[view animator] setGradient:gradient] ;.我定义了(id)defaultAnimationForKey:(NSString *)键并返回了一个简单的CABasicAnimation,但是没有执行动画.由于这适用于更简单的类型和NSColor,我猜CABasicAnimation不适用于渐变.很好,但在这种特殊情况下,渐变是微不足道的(总是两个停止),因此我可以轻松编写插值函数.问题:如何定义自定义插值?关于视图,图层和动画,子类化动画类等的代表,我用Google搜索,但我无法弄清楚这些事情.谢谢!

解决方法

当我学习如何使用核心动画时,我以为我记得通过了一些Apple文档,这些动画展示了如何设置无法由定义动画提供的属性描述来处理的动画.一路走来,我偶然发现了Apple的一些示例代码,描述如下:

A single gradient layer is displayed and continuously animated using new random colors.

这可能是您已经以另一种方式处理的特定任务的答案.我在Xcode中的Documentation and API Reference中找到了它,示例代码的名称只是Gradients. (请注意,有一个原始版本1.0和更新版本1.1,今年4月重做,因此应该更容易使用当前工具.

但是,创建无法通过Core Animation本身自动化的自定义动画的更大问题是遵循Apple的Cocoa动画编程指南中的示例,在使用NSAnimation对象一节中.它在Subclassing NSAnimation主题下描述,推荐的方法显示在Smooth Animations标题下.您重写setCurrentProgress:方法,以便每次调用它时首先调用Super,以便NSAnimation更新进度值,即您的自定义动画属性,然后执行动画的下一帧所需的任何更新或绘图.以下是Apple在参考文档中提供的注释和示例代码:

As mentioned in “Setting and Handling Progress Marks,” you can attach a series of progress marks to an NSAnimation object and have the delegate implement the animation:didReachProgressMark: method to redraw an object at each progress mark. However,this is not the best way to animate an object. Unless you set a large number of progress marks (30 per second or more),the animation is probably going to appear jerky.

A better approach is to subclass NSAnimation and override the setCurrentProgress: method,as illustrated in Listing 4. The NSAnimation object invokes this method after each frame to change the progress value. By intercepting this message,you can perform any redrawing or updating you need for that frame. If you do override this method,be sure to invoke the implementation of super so that it can update the current progress.

Listing 4  Overriding the setCurrentProgress: method
- (void)setCurrentProgress:(NSAnimationProgress)progress
{
    // Call super to update the progress value.
    [super setCurrentProgress:progress];

    // Update the window position.
    NSRect theWinFrame = [[NSApp mainWindow] frame];
    NSRect theScreenFrame = [[NSScreen mainScreen] visibleFrame];
    theWinFrame.origin.x = progress *
        (theScreenFrame.size.width - theWinFrame.size.width);
    [[NSApp mainWindow] setFrame:theWinFrame display:YES animate:YES];
}

因此,基本上您定义了一个“进度值”(可能由多个值组成),它定义了自定义动画的状态,并编写了给定当前“进度值”的代码,用于绘制或更改动画处于该特定状态时绘制的内容.然后让NSAnimation使用设置动画的常规方法运行动画,它将执行代码以在适当的时间绘制动画的每个帧.

我希望能回答你想知道的事情.我怀疑我之前没有看过它就能轻松找到这个,因为我最终不得不去我认为可能的地方,并逐页浏览整个主题再次找到它!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读