该app为应用的功能为制作一场雪景

现版本 SDK 8.4 Xcode

纲要:
- UIImageView 的运用
- onTimer 代码运用
- onAnimation 代码运用

运行Xcode 选择 Create a new Xcode project ->Single View Application 命名 SnowFall

(1)  在xCode打开 ViewController.h 文件

(红色为所添加的代码)

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

UIImage *flakeImage;

}

@property(nonatomic,retain)UIImage *flakeImage;

-(void)onTimer;

-(void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;

@end

(2)  在xCode打开 ViewController.m 文件

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize flakeImage;

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

// RGB和alpha值的范围是0~1 Alpha透明度

// 把背景颜色设置为冷色

self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1.0];

// 导出雪花图片

flakeImage = [UIImage imageNamed:@"flake.png"];

// 每秒二十次的调用onTimer事件

[NSTimer scheduledTimerWithTimeInterval:(0.05) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

}

-(void)onTimer

{

//建立一个ImageView 放置雪花图片 flake image

UIImageView *flakeView = [[UIImageView alloc]initWithImage:flakeImage];

//随即生成参数坐标

int startX = round(random()%320);

int endX = round(random()%320);

double scale = 1/round(random()%100)+1.0;

double speed = 1/round(random()%100)+1.0;

//设置雪花图片出现的坐标和透明度 即控制UIView的大小和该UIView在superview中的相对位置、透明度  基准为左上角

flakeView.frame = CGRectMake(startX, -100.0, 25.0*scale, 25.0*scale);

flakeView.alpha = 0.25;

//将flakeView添加进主视图

[self.view addSubview:flakeView];

//在 Objective-C 和 Core Foundation 对象之间进行转换时,就需要使用 Bridge cast(待详细研究)

[UIView beginAnimations:nil context:(__bridge void *)(flakeView)];

//动画时常

[UIView setAnimationDuration:5*speed];

//动画结束 图片位置

flakeView.frame = CGRectMake(endX, 500.0, 25.0*scale, 25.0*scale);

//*1*动画结束时调用 清理flakeView

[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];

[UIView setAnimationDelegate:self];

[UIView commitAnimations];

}

-(void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{

UIImageView *flakeView = (__bridge UIImageView *)(context);

[flakeView removeFromSuperview];

//   NSLog(NSString stringWithFormat:@"[flakeView retainCount] = %d", [flakeView retainCount]]);

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

(3) 导入下面图片文件

下载下面图片,放入 SnowFall 文件夹内并命名为下面名称
flake.png

选择: File -> Save

最后在 xCode 选择 Build and then Running

(4)模拟器效果图

保留*1*处代码即动画结束后清理flake view效果为

不保留*1*处代码,效果为

本文源于网上博客教程,经过本人修改和测试。原blog地址 http://blog.sina.com.cn/s/blog_5fae23350100e1uk.html

六、雪花《苹果iOS实例编程入门教程》的更多相关文章

  1. 五、点数器《苹果iOS实例编程入门教程》

    该app为应用的功能为一个简单的数数程序 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Applic ...

  2. 四、卫星定位《苹果iOS实例编程入门教程》

    该app为应用的功能为用iPhone 显示你现在的位置 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View ...

  3. 三、图像移动《苹果iOS实例编程入门教程》

    该app为应用的功能为动态移动动画 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Applicati ...

  4. 二 、打开地图《苹果iOS实例编程入门教程》

    该app为应用的功能为给你的iPhone打开google地图有效地址连接 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Si ...

  5. 一、午夜倒数《苹果iOS实例编程入门教程》

    该app为应用的功能为计算离午夜12:00点的剩余时间 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View ...

  6. 七、考反映小游戏《苹果iOS实例编程入门教程》

    该app为应用的功能为一个简单的考反应游戏 纲要:-UIButton, UILabel, UIImageView 的运用:-利用rendom增加游戏可玩性: 游戏说明: 在按下开始游戏后,分为三盏的指 ...

  7. 【C语言C++编程学习笔记】基础语法,第一个简单的实例编程入门教程!

    C语言/C++编程学习:一个简单的实例 让我们来看一个简单的C语言程序.从下面的程序可以看出编写C语言程序的一些基本特征.   如果你能知道该程序将会在显示器上显示一些内容,那说明你还是知道一些的! ...

  8. VS2010/MFC编程入门教程之目录和总结

    鸡啄米的这套VS2010/MFC编程入门教程到此就全部完成了,虽然有些内容还未涉及到,但帮助大家进行VS2010/MFC的入门学习业已足够.以此教程的知识为基础,学习VS2010/MFC较为深入的内容 ...

  9. (转)VS2010-MFC编程入门教程之目录和总结

     目前该教程可以到鸡啄米编程课堂去学习,阅读体验更好,更适合在线学习. 原文目录及链接: 一.VS2010/MFC编程入门教程之目录 第一部分:VS2010/MFC开发环境 VS2010/MFC编程入 ...

随机推荐

  1. wp8 入门到精通 ImageCompress 图片压缩

    //实例化选择器 PhotoChooserTask photoChooserTask = new PhotoChooserTask(); BitmapImage bimg; int newPixelW ...

  2. 遍历List过程中删除元素的正确做法(转)

    遍历List过程中删除元素的正确做法   public class ListRemoveTest {     3 public static void main(String[] args) { 4 ...

  3. Android开发工具之Dash

    作为一名死coder,每天最常见的动作就是查看各种API文档,你一定也有过同时打开N个窗口(HTML.PDF.CHM),不停的在编辑器与文档之间切换的感受吧?怎么说呢,其实我很讨厌这种枯燥无味的动作, ...

  4. Linux学习笔记(19) Linux服务管理

    1. 服务的分类 Linux服务可分为RPM包默认安装的服务和源码包安装的服务.前者可细分为独立的服务(直接作用于内存中)和基于xinetd服务.xinetd本身是独立的服务,其唯一的功能是管理其他服 ...

  5. json对象的解析

    json对象数据: { "status": "200", "code": "", "msg": &q ...

  6. JQuery 定时器 (Jquery Timer 插件)

      jQuery Timers插件地址: http://plugins.jquery.com/project/timers JQuery Timers应用知识提供了三个函式1. everyTime(时 ...

  7. JAVA Day2

          标识符(类名:变量.属性.方法名: )      组成:类名开头不能是数字,只能有字母数字_$组成.         命名规范: 类名每一个单词首字母大写(HelloWorld大驼峰法则) ...

  8. 插入视频(youtube)

    iframe 如果没有flash播放器,会自动使用html5播放器 <iframe width="420" height="315" src=" ...

  9. C#,往线程里传参数的方法总结

    Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托.   Thread (ThreadStart) 初始化 ...

  10. Web Service \restful web services\WCF Service\ServiceStack

    http://www.cnblogs.com/jfzhu/p/4025448.html http://www.cnblogs.com/jfzhu/p/4022139.html#3043243