Animating Views and Windows

本文介绍如何使用 NSViewAnimation 类来实现视图的位置、大小及透明度的动画效果。通过设置不同属性,可以轻松完成复杂的视图动画,如平移、缩放及淡入淡出等。文中提供了一个具体示例,展示了如何同时改变两个视图对象的位置和透明度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Animating Views and Windows

The NSViewAnimation class is a subclass of NSAnimation that provides a convenient way to animate aspects of your view and window objects, including the following:

  • Change the position of the frame.

  • Change the size of the frame

  • Change the opacity of the object and fade it in or out.

The View Animation Process

You use view animation objects in a slightly different way than you do regular NSAnimation objects. A single view animation object can manage the animation process for multiple views and windows simultaneously. Rather than setting the attributes using methods of the animation object, you instead create a dictionary of animation attributes for each view or window you want to modify. Each dictionary specifies the target of the action (the view or window), and the effects you want to apply to that target. To set other factors, such as the duration and timing curve of the animation, you continue to use the methods of NSAnimation.

An animation attributes dictionary has only one required value: the target object. You add this object to the dictionary using theNSViewAnimationTargetKey key. The presence of this key alone, though, does not change the view or window. To make changes, you must include one or more additional keys to specify the desired behavior.

You can perform multiple actions on a single target object simultaneously, if you choose. For example, you can resize a view, change its position on the screen, and fade it in or out all at once. The following sections show you how to perform each of these actions separately, for simplicity. To perform them both, simply add all of the relevant keys to the attributes dictionary.

Changing the Frame Rectangle

Changing the frame rectangle of a view or window lets you resize and reposition that object relative to its parent. In the case of views, this means changing the position and size of the view in its superview. In the case of windows, it means changing the position and size of the window on the desktop. Table 1 lists the keys and values you would put into the attributes dictionary to change the frame rectangle.

Table 1  Keys for resizing or repositioning a view or window.

Key

Value

Description

NSViewAnimationTargetKey

id

Identifies the NSView or NSWindow object to resize or reposition. This key is required.

NSViewAnimationStartFrameKey

NSValue

Contains the starting frame rectangle of the target object. The NSValue object should contain an encoded NSRect data type. This value is typically equal to the current frame of the view or window. This key is optional and defaults to the current frame rectangle of the target object.

NSViewAnimationEndFrameKey

NSValue

Contains the ending frame rectangle of the target object. The NSValue object should contain an encoded NSRect data type. This key is recommended; if not present, it defaults to the current frame rectangle of the target object.

Fading Objects In and Out

If you want to hide a view or window, rather than have the object suddenly disappear, you can use a view animation to make that object gradually fade away. Similarly, you can use a similar type of view animation to make the object visible again. When fading a view back in, the size of the ending frame rectangle must be non-zero; if it is not, the view remains hidden. Table 2 lists the keys and values you would put into the attributes dictionary to fade an object in or out.

Table 2  Keys for fading a view or window.

Key

Value

Description

NSViewAnimationTargetKey

id

Identifies the NSView or NSWindow object to modify. This key is required.

NSViewAnimationEffectKey

NSString

Contains one of the following string constants: NSViewAnimationFadeInEffect makes the object visible and NSViewAnimationFadeOutEffect hides it. These effects change the opacity of the object over the course of the animation.

A View Animation Example

Listing 1 illustrates the basic use of a view animation object. The action method sets up attribute dictionaries for two different view objects and then runs the animation whenever the action occurs. For the first view object, the animation object shifts the origin of the view by 50 units along each axis. For the second view, the animation object shrinks the frame size to zero while simultaneously fading the view out until it is completely hidden. The animation uses a custom timing curve and duration but uses the default blocking mode, which blocks user input on the main thread until the animation is complete.

Listing 1  Animating two NSView objects

- (IBAction)startAnimations:(id)sender
{
    // firstView, secondView are outlets
    NSViewAnimation *theAnim;
    NSRect firstViewFrame;
    NSRect newViewFrame;
    NSMutableDictionary* firstViewDict;
    NSMutableDictionary* secondViewDict;
 
    {
        // Create the attributes dictionary for the first view.
        firstViewDict = [NSMutableDictionary dictionaryWithCapacity:3];
        firstViewFrame = [firstView frame];
 
        // Specify which view to modify.
        [firstViewDict setObject:firstView forKey:NSViewAnimationTargetKey];
 
        // Specify the starting position of the view.
        [firstViewDict setObject:[NSValue valueWithRect:firstViewFrame]
                 forKey:NSViewAnimationStartFrameKey];
 
        // Change the ending position of the view.
        newViewFrame = firstViewFrame;
        newViewFrame.origin.x += 50;
        newViewFrame.origin.y += 50;
        [firstViewDict setObject:[NSValue valueWithRect:newViewFrame]
                 forKey:NSViewAnimationEndFrameKey];
    }
 
    {
        // Create the attributes dictionary for the second view.
        secondViewDict = [NSMutableDictionary dictionaryWithCapacity:3];
 
        // Set the target object to the second view.
        [secondViewDict setObject:secondView forKey:NSViewAnimationTargetKey];
 
        // Shrink the view from its current size to nothing.
        NSRect viewZeroSize = [secondView frame];
        viewZeroSize.size.width = 0;
        viewZeroSize.size.height = 0;
        [secondViewDict setObject:[NSValue valueWithRect:viewZeroSize]
                 forKey:NSViewAnimationEndFrameKey];
 
        // Set this view to fade out
        [secondViewDict setObject:NSViewAnimationFadeOutEffect
                forKey:NSViewAnimationEffectKey];
    }
 
    // Create the view animation object.
    theAnim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray
                arrayWithObjects:firstViewDict, secondViewDict, nil]];
 
    // Set some additional attributes for the animation.
    [theAnim setDuration:1.5];    // One and a half seconds.
    [theAnim setAnimationCurve:NSAnimationEaseIn];
 
    // Run the animation.
    [theAnim startAnimation];
 
    // The animation has finished, so go ahead and release it.
    [theAnim release];
}
资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 IT 领域,文档格式转换是常见需求,尤其在处理多种文件类型时。本文将聚焦于利用 Java 技术栈,尤其是 Apache POI 和 iTextPDF 库,实现 doc、xls(涵盖 Excel 2003 及 Excel 2007+)以及 txt、图片等格式文件向 PDF 的转换,并实现在线浏览功能。 先从 Apache POI 说起,它是一个强大的 Java 库,专注于处理 Microsoft Office 格式文件,比如 doc 和 xls。Apache POI 提供了 HSSF 和 XSSF 两个 API,其中 HSSF 用于读写老版本的 BIFF8 格式(Excel 97-2003),XSSF 则针对新的 XML 格式(Excel 2007+)。这两个 API 均具备读取和写入工作表、单元格、公式、样式等功能。读取 Excel 文件时,可通过创建 HSSFWorkbook 或 XSSFWorkbook 对象来打开相应格式的文件,进而遍历工作簿中的每个 Sheet,获取行和列数据。写入 Excel 文件时,创建新的 Workbook 对象,添加 Sheet、Row 和 Cell,即可构建新 Excel 文件。 再看 iTextPDF,它是一个用于生成和修改 PDF 文档的 Java 库,拥有丰富的 API。创建 PDF 文档时,借助 Document 对象,可定义页面尺寸、边距等属性来定制 PDF 外观。添加内容方面,可使用 Paragraph、List、Table 等元素将文本、列表和表格加入 PDF,图片可通过 Image 类加载插入。iTextPDF 支持多种字体和样式,可设置文本颜色、大小、样式等。此外,iTextPDF 的 TextRenderer 类能将 HTML、
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值