mac_ScreenSaver_第1个屏幕保护程序

本文详细介绍了如何使用Mac OS X的屏幕保护程序框架创建自定义屏幕保护程序。通过Xcode的屏幕保护模板,我们可以轻松创建项目。文章涵盖了基本的设置、贝塞尔路径绘制、用户偏好设置以及通过配置表单添加自定义选项。通过使用OpenGL进行绘图,教程还展示了如何为屏幕保护程序添加更复杂的功能,并处理可能的符号冲突问题,以确保兼容PowerPC和Intel Macs。
摘要由CSDN通过智能技术生成

原文链接:http://cocoadevcentral.com/articles/000088.php

官方文档:https://developer.apple.com/library/mac/documentation/UserExperience/Reference/ScreenSaver/Classes/ScreenSaverView_Class/index.html


written by Brian Christensen

Writing a screen saver module is surprisingly simple using Mac OS X's screen saver framework. The ScreenSaverView class provides us with an interface for animating the screen saver, drawing the preview view in the System Preferences pane, and displaying a configuration sheet.

I originally wrote this article in 2001 when it was still necessary to jump through a few configuration hoops just to create a new screen saver project. An entire section was dedicated to "Preparing the Project." Thankfully, these steps are no longer required. Ever since the advent of Xcode Apple has included a default screen saver project template to make our lives significantly easier.

Having said that, this is not just a mere recycling of the old article. There is a considerable amount of fresh material which I hope will improve upon the original article.

Part 1: basics on set up, simple bezier path drawing, and user preferences using a configure sheet
Part 2: advanced topics such as setting up and using OpenGL for drawing

Create a New Project
1 of 9

First, we'll need to create a new project. Launch Xcode and choose File > New Project. The window depicted below will appear:

New Project

From the list of choices you are presented with at this point select Screen Saver located under theStandard Apple Plug-ins category. Proceed to name the project MyScreenSaver (or any other name you prefer) and save it in your desired location. Once you have completed these steps, the project window will open.

Xcode Project Window

Xcode automatically creates two files for us: MyScreenSaverView.h and MyScreenSaverView.m. Take a few minutes to poke around MyScreenSaverView.m and take note of the various methods that are provided for our use.

I have summarized some of these methods below:

ScreenSaverView: Key Methods
-initWithFrame:
 isPreview:
initializes the view with the given frame rect
-setAnimationTimeInterval: sets the animation rate in seconds
-startAnimation: invoked by the screen saver engine when animation should begin
-stopAnimation: invoked by the screen saver engine when animation should stop
-drawRect: draws the view
-animateOneFrame advances the animation by one frame at the rate set by the preceding method (this can be used for drawing instead of drawRect:)
-hasConfigureSheet returns true if the module has a configure sheet
-configureSheet returns the module's associated configure sheet
Implement the Animation Method
2 of 9

We want to draw a bunch of shapes of random type, size, color, and location. This drawing magic must occur either in the drawRect: or animateOneFrame method. Since animateOneFrame is slightly easier to use and better suits our purpose, this is the method we will be using.

Open the "MyScreenSaverView.m" file to edit it. Scroll to the animateOneFrame method and add the following code:

MyScreenSaverView.m
- (void) animateOneFrame{ NSBezierPath *path; NSRect rect; NSSize size; NSColor *color; float red, green, blue, alpha; int shapeType;

First of all, we want to obtain the screen's boundaries for later use. To do this, we use the boundsmethod. This returns an NSRect from which we will obtain the size property.

size = [self bounds].size;

Since we want our shapes to have random sizes, we can use the specially suppliedSSRandomFloatBetween() function for this. For those who are wondering, it is automatically seeded by the screen saver framework, so there is no need to manually deal with that.

// Calculate random width and height rect.size = NSMakeSize( SSRandomFloatBetween( size.width / 100.0, size.width / 10.0 ), SSRandomFloatBetween( size.height / 100.0, size.height / 10.0 ));

Now we want to calculate a random origin point for our shape. To do this, we use the handySSRandomPointForSizeWithinRect() function, which will do all the work for us.

// Calculate random origin point rect.origin = SSRandomPointForSizeWithinRect( rect.size, [self bounds] );

Create Rectangles and Ovals
3 of 9
Using NSBezierPath

In simple terms, an NSBezierPath object lets you draw paths consisting of straight and curved line segments. When put together these can form shapes such as rectangles, ovals, arcs, and glyphs. We will be using the first three in our code.

NSBezierPath Shape Examples

We want to randomly decide whether to create our NSBezierPath object as a rectangle, oval, or arc. In order to provide for greater flexibibility should you choose to invent additional shape types at a later time, we will utilize a switch statement.

// Decide what kind of shape to draw shapeType = SSRandomIntBetween( 0, 2 ); switch (shapeType) { case 0: // rect path = [NSBezierPath bezierPathWithRect:rect]; break; case 1: // oval path = [NSBezierPath bezierPathWithOvalInRect:rect]; break;
Create Arcs
4 of 9

The arc case is a bit more complicated, so we will go through it step-by-step. An arc consists of a center, a radius, a starting angle, an ending angle, and a direction. First, we will select random starting and ending angles between 0 and 360 degrees. (Naturally, as you can see in the code below, it wouldn't make sense for the ending angle to start before the starting angle. Hence, it will not necessarily fall between 0 and 360 degrees. It is actually chosen to be somewhere between the starting angle value and the starting angle value plus 360.)

case 2: // arc default: { float startAngle, endAngle, radius; NSPoint point; startAngle = SSRandomFloatBetween( 0.0, 360.0 ); endAngle = SSRandomFloatBetween( startAngle, 360.0 + startAngle );

Similarly, we now choose a random radius. Since we already did a calculation earlier to determine a random rectangle size, we will simply use either the width or the height of that size, whichever is the smallest. In addition, we also calculate a center point using previously generated origin and size values.

// Use the smallest value for the radius (either width or height) radius = rect.size.width <= rect.size.height ? rect.size.width / 2 : rect.size.height / 2; // Calculate our center point point = NSMakePoint( rect.origin.x + rect.size.width / 2, rect.origin.y + rect.size.height / 2 );

Finally, we are ready to construct our path. Unlike the above rectangle and oval cases, here we must first create an empty bezier path and then append the arc path to it.

// Construct the path path = [NSBezierPath bezierPath]; [path appendBezierPathWithArcWithCenter: point radius: radius startAngle: startAngle endAngle: endAngle clockwise: SSRandomIntBe
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值