1、效果图展示

2、实现思路

  1> 首先要实现上面的效果,第一步要处理的就是一个简单的画板,在View上面用鼠标滑动的时候画出线条,这个功能可使用UIBezierPath实现

  2> 关于粒子效果的实现,可以创建一个CALayer,然后用CAReplicatorLayer进行复制layer,从而达到粒子效果

3、代码实现

DrawView类的封装与编写

//
// DrawView.m
// 06-粒子效果
//
// Created by xgao on 16/11/24.
// Copyright (c) 2016年 xgao. All rights reserved.
// #import "DrawView.h" // 复制的实例个数
static int _instansCount = ; @interface DrawView () @property (nonatomic, strong) UIBezierPath *path; @property (nonatomic, weak) CALayer *dotLayer; @property (nonatomic, weak) CAReplicatorLayer *repL; @end @implementation DrawView #pragma mark - 懒加载点层
- (CALayer *)dotLayer{ if (_dotLayer == nil) {
// 创建图层
CALayer *layer = [CALayer layer]; CGFloat wh = ;
layer.frame = CGRectMake(, -, wh, wh);
layer.cornerRadius = wh / ;
layer.backgroundColor = [UIColor blueColor].CGColor;
// 将layer添加到CAReplicatorLayer中
[_repL addSublayer:layer]; _dotLayer = layer;
}
return _dotLayer;
} - (UIBezierPath *)path{ if (_path == nil) {
_path = [UIBezierPath bezierPath];
} return _path;
} #pragma mark - 开始点击调用 // 鼠标开始点击
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ // 获取touch对象
UITouch *touch = [touches anyObject];
// 获取当前触摸点
CGPoint curP = [touch locationInView:self]; // 设置起点
[self.path moveToPoint:curP];
} // 鼠标按住移动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ // 获取touch对象
UITouch *touch = [touches anyObject];
// 获取当前触摸点
CGPoint curP = [touch locationInView:self]; // 添加线到某个点
[_path addLineToPoint:curP]; // 重绘
[self setNeedsDisplay]; _instansCount ++;
} - (void)drawRect:(CGRect)rect { // 进行路径线条的绘制
[_path stroke];
} #pragma mark - 开始动画
- (void)startAnim{ // 创建帧动画
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"position";
anim.path = _path.CGPath;
anim.duration = ;
anim.repeatCount = MAXFLOAT;
[self.dotLayer addAnimation:anim forKey:nil]; // 注意:如果复制的子层有动画,先添加动画,再复制
// 复制子层
_repL.instanceCount = _instansCount; // 延迟图层动画
_repL.instanceDelay = 0.2; } #pragma mark - 加载完xib调用,创建复制层
- (void)awakeFromNib{ // 创建复制层
CAReplicatorLayer *repL = [CAReplicatorLayer layer];
repL.frame = self.bounds;
[self.layer addSublayer:repL]; _repL = repL;
} #pragma mark - 重绘
- (void)reDraw{ // 清空绘图信息
_path = nil;
[self setNeedsDisplay]; // 把图层移除父控件,复制层也会移除。
[_dotLayer removeFromSuperlayer];
_dotLayer = nil; // 清空子层总数
_instansCount = ;
}
@end
DrawView的使用
// 点击开始动画
- (IBAction)startAnim:(id)sender { DrawView *view = (DrawView *)self.view;
[view startAnim]; } // 重置按钮
- (IBAction)reDraw:(id)sender { DrawView *view = (DrawView *)self.view;
[view reDraw];
}

IOS中一个简单的粒子效果实现的更多相关文章

  1. iOS中XMPP简单聊天实现 好友和聊天

    版权声明本文由陈怀哲首发自简书:http://www.jianshu.com/users/9f2e536b78fd/latest_articles;微信公众号:陈怀哲(chenhuaizhe2016) ...

  2. 如何让IOS中的文本实现3D效果

    本转载至 http://bbs.aliyun.com/read/181991.html?spm=5176.7114037.1996646101.25.p0So7c&pos=9       zh ...

  3. Css实现一个简单的幻灯片效果页面

    使用animation动画实现一个简单的幻灯片效果. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2 ...

  4. python中一个简单的webserver

     python中一个简单的webserver 2013-02-24 15:37:49 分类: Python/Ruby 支持多线程的webserver   1 2 3 4 5 6 7 8 9 10 11 ...

  5. CSS布局中一个简单的应用BFC的例子

    什么是BFC BFC(Block Formatting Context),简单讲,它是提供了一个独立布局的环境,每个BFC都遵守同一套布局规则.例如,在同一个BFC内,盒子会一个挨着一个的排,相邻盒子 ...

  6. 使用CSS实现一个简单的幻灯片效果

    方法一: 简单的CSS代码实现幻灯片效果 方法二: 使用CSS3 Animation来制作幻灯片 方法一: 简单的CSS代码实现幻灯片效果 话不多说,直接上代码 <!DOCTYPE html&g ...

  7. iOS中 超简单抽屉效果(MMDrawerController)的实现

    ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...

  8. 谈谈iOS中粘性动画以及果冻效果的实现

    在最近做个一个自定义PageControl——KYAnimatedPageControl中,我实现了CALayer的形变动画以及CALayer的弹性动画,效果先过目: https://github.c ...

  9. 转:谈谈iOS中粘性动画以及果冻效果的实现

    在最近做个一个自定义PageControl——KYAnimatedPageControl中,我实现了CALayer的形变动画以及CALayer的弹性动画,效果先过目: 先做个提纲: 第一个分享的主题是 ...

随机推荐

  1. sqoop的安装与使用

    1.什么是Sqoop Sqoop即 SQL to Hadoop ,是一款方便的在传统型数据库与Hadoop之间进行数据迁移的工具.充分利用MapReduce并行特点以批处理的方式加快传输数据.发展至今 ...

  2. C 高级编程3 静态库与动态库

    http://blog.csdn.net/Lux_Veritas/article/details/11934083http://www.cnblogs.com/catch/p/3857964.html ...

  3. C#_自动化测试3_controll IE

    目前市面上有很多Web UI自动化测试框架,比如WatiN, Selinimu,WebDriver,还有VS2010中的Coded UI等等.  这些框架都可以操作Web中的控件,模拟用户输入,点击等 ...

  4. [转]AFNetWorking使用笔记

    转载自:http://blog.sina.com.cn/s/blog_719d537e01017x82.html AFNetwork是一个轻量级的网络请求api类库.是以NSURLConnection ...

  5. Eclipse的java代码出错:The import org.apache cannot be resolved

    Eclipse中,折腾java代码. 把之前在android中的代码拿过来使用. 结果出现The import org.apache cannot be resolved的错误: [解决过程] 1.这 ...

  6. python-其他常用模块

    本节大纲: 模块介绍 time &datetime模块 random shutil shelve xml处理 yaml处理 configparser hashlib subprocess lo ...

  7. 大文件读取方法(C#)

    之前都是用StreamReader.ReadLine方法逐行读取文件,自从.NET4有了File.ReadLines这一利器,就再也不用为大文件发愁了. File.ReadLines在整个文件读取到内 ...

  8. cocos2d-x调度器原理

    程序运行后每达到一帧的时间间隔就会执行一次mainLoop void CCDisplayLinkDirector::mainLoop(void) { //判断是否需要释放CCDirector,通常游戏 ...

  9. iOS 延时加载

    这里列举了四种线程延时加载的方法, 1.performSelector方法 此方法必须在主线程中执行,并不是阻塞当前的线程 [self performSelector:@selector(delayM ...

  10. sql 自定义函数--固定格式字符转时间类型

    遇到一个德国的客户,他们的时间格式是JJJJ-TT-DD HH:MM:SS,程序按照这个格式将时间插入数据库,但是在sql自带的转换函数convert.cast过程中报错,网上搜了下都说用conver ...