IOS中一個簡單的粒子效果實現

來源:互聯網
上載者:User

標籤:重繪   ima   alt   weak   splay   功能   copyright   frame   make   

1、展示

2、實現思路

  1> 首先要實現上面的效果,第一步要處理的就是一個簡單的畫板,在View上面用滑鼠滑動的時候畫出線條,這個功能可使用UIBezierPath實現

  2> 關於粒子效果的實現,可以建立一個CALayer,然後用CAReplicatorLayer進行複製layer,從而達到粒子效果

3、代碼實現

DrawView類的封裝與編寫

////  DrawView.m//  06-粒子效果////  Created by xiaomage on 15/6/24.//  Copyright (c) 2015年 xiaomage. All rights reserved.//#import "DrawView.h"@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 = 10;        layer.frame = CGRectMake(0, -1000, wh, wh);                layer.cornerRadius = wh / 2;                layer.backgroundColor = [UIColor blueColor].CGColor;        [_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];            }static int _instansCount = 0;- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    // 擷取touch對象    UITouch *touch = [touches anyObject];        // 擷取當前觸摸點    CGPoint curP = [touch locationInView:self];    // 添加線到某個點    [_path addLineToPoint:curP];        // 重繪    [self setNeedsDisplay];        _instansCount ++;    }// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect {    // Drawing code        [_path stroke];}#pragma mark - 開始動畫- (void)startAnim{    // 建立幀動畫    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];        anim.keyPath = @"position";        anim.path = _path.CGPath;        anim.duration = 3;        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 = 0;    }@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中一個簡單的粒子效果實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.