雪花直播ios
2016-03-15 09:16:00 weixin_33946020 阅读数 9
662664-577ea0176dccf5e1.PNG
雪花效果
    //create particle emitter layer
    CAEmitterLayer *emitter = [CAEmitterLayer layer];
    emitter.frame = CGRectMake(0, -50, self.view.bounds.size.width, 50);
    [self.view.layer addSublayer:emitter];
    
    emitter.emitterShape = kCAEmitterLayerRectangle;
    //configure emitter
//    emitter.renderMode = kCAEmitterLayerAdditive;
    emitter.emitterPosition = CGPointMake(emitter.frame.size.width / 2.0, emitter.frame.size.height / 2.0);
    emitter.emitterSize = emitter.frame.size;
    
    //create a particle template
    CAEmitterCell *cell = [[CAEmitterCell alloc] init];
    cell.contents = (__bridge id)[UIImage imageNamed:@"snow"].CGImage;
    cell.contentsScale = 7;
    
    cell.birthRate = 100;
    cell.lifetime = 5.0;
    cell.lifetimeRange = 5;
    
    cell.redRange = 0.1;
    cell.greenRange = 0.1;
    cell.blueRange = 0.1;
    
    cell.alphaRange = 0.8;
    cell.alphaSpeed = -0.10;
    
    cell.yAcceleration = 30;
    cell.xAcceleration = 20;
    cell.velocity = 20;
    cell.velocityRange = 200;
    cell.emissionRange = M_PI_2;
    cell.emissionLongitude = - M_PI;
    
    //add particle template to emitter
    emitter.emitterCells = @[cell];
2012-12-27 09:00:31 h3c4lenovo 阅读数 4137
    UIImageView *peach2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flower2"]];//声明一个UIImageView对象,用来添加图片
    peach2.alpha = 0.8;//设置该view的alpha为0.5,半透明的
    int xx = round(random()%2000);//随机得到该图片的x坐标
    int yx = round(random()%2000);//这个是该图片移动的最后坐标x轴的
    int sx = random()%50+10;//这个是定义雪花图片的大小
    int spx = random()%5;//这个是速度
    peach2.frame = CGRectMake(-xx, -10, sx, sx);//雪花开始的大小和位置
    [view_bottom addSubview:peach2];//添加该view
    [UIView animateWithDuration:10*spx
                     animations:^{
                         peach2.frame = CGRectMake(yx, self.view.frame.size.height, sx, sx);//设定该雪花最后的消失坐标
                     } completion:^(BOOL finished) {
                         [peach2 removeFromSuperview];
                     }];

2016-06-27 10:47:00 weixin_34037977 阅读数 3
定时器雪花效果

#####1.定时器一个是NSTimer

#import "HYLSnowflake.h"
static CGFloat snowY=0;
@implementation HYLSnowflake
#pragma mark -drawRect
- (void)drawRect:(CGRect)rect {
    UIImage *image=[UIImage imageNamed:@"雪花"];
    [image drawAtPoint:CGPointMake(0, snowY)];
}

-(void)awakeFromNib{
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];

}

-(void) timeChange{
    snowY+=10;
    if (snowY>self.bounds.size.height) {
        snowY=0;
    }
    [self setNeedsDisplay];
}
@end

#####2.另一个是CADisplayLink

@implementation HYLSnowflake
#pragma mark -drawRect
- (void)drawRect:(CGRect)rect {
    UIImage *image=[UIImage imageNamed:@"雪花"];
    [image drawAtPoint:CGPointMake(0, snowY)];
}
//-(void)awakeFromNib{
//    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
//
//}
#pragma mark - awakeFromNib
-(void) awakeFromNib{
    //屏幕刷新时调用,屏幕一般一秒刷新60次
    CADisplayLink *link=[CADisplayLink displayLinkWithTarget:self selector:@selector(timeChange)];
    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
-(void) timeChange{
    if (count==30) {
        snowY+=10;
        count=0;
    }else{
        count++;
    }

    if (snowY>self.bounds.size.height) {
        snowY=0;
    }
    //注意:这个方法并不会马上调用drawRect,其实这个方法只是给当前控件添加刷新的标记,等下一次屏幕刷新的时候才会调用drawRect
    [self setNeedsDisplay];
}
@end
  • NSTimer易出现卡顿情况
  • 第二种不会,因为频率与屏幕刷新时间相同,所以不会出现卡顿

#####4.效果图片

  • 1.

#####5.源代码地址

转载于:https://my.oschina.net/HYLApple/blog/701247

2016-07-31 00:33:00 weixin_34033624 阅读数 12

源码地址:https://github.com/chenfanfang/CollectionsOfExample

本文主要介绍CADisplayLink和- (void)drawRect:(CGRect)rect的配合使用,不建议大家这么处理,因为很耗性能(因为创建了太多的控件),建议大家使用粒子发射器。

首先先附上几张效果图

1594675-ecc72f808a29e6d4.gif
雪花飘落效果.gif

普通图片

1594675-2d9db30db562c766.png
雪花效果图1.png

1594675-e80924b0e5667e20.png
雪花效果图2.png

雪花效果的思路:

雪花效果最主要的思路就是在于循环产生带雪花图片的imageView, 产生的雪花的imageview的 x、y、宽、下落的速度都是随机的,这个可以用随机数来产生数据。

附上源码:

FFSnowflakesFallingView.h文件内容

//
//  FFSnowflakesFallingView.h
//  CollectionsOfExample
//
//  Created by mac on 16/7/30.
//  Copyright © 2016年 chenfanfang. All rights reserved.
//

#import <UIKit/UIKit.h>

/**
*  雪花飘落效果的view
*/
@interface FFSnowflakesFallingView : UIView

/**
*  快速创建一个雪花飘落效果的view
*
*  @param bgImageName   背景图片的名称
*  @param snowImageName 雪花图片的名称
*  @param frame     frame
*
*  @return 实例化的 雪花飘落效果的view
*/
+ (instancetype)snowflakesFallingViewWithBackgroundImageName:(NSString *)bgImageName snowImageName:(NSString *)snowImageName frame:(CGRect)frame;

/** 开始下雪 */
- (void)beginSnow;

@end

FFSnowflakesFallingView.m 文件内容

//
//  FFSnowflakesFallingView.m
//  CollectionsOfExample
//
//  Created by mac on 16/7/30.
//  Copyright © 2016年 chenfanfang. All rights reserved.
//

#import "FFSnowflakesFallingView.h"

@interface FFSnowflakesFallingView ()

/** 背景图片imageView */
@property (nonatomic, strong) UIImageView *bgImageView;

/** 雪花图片的名称 */
@property (nonatomic, copy) NSString *snowImgName;

@end

@implementation FFSnowflakesFallingView

+ (instancetype)snowflakesFallingViewWithBackgroundImageName:(NSString *)bgImageName snowImageName:(NSString *)snowImageName frame:(CGRect)frame {
    FFSnowflakesFallingView *view = [[FFSnowflakesFallingView alloc] initWithFrame:frame];
    view.bgImageView.image = [UIImage imageNamed:bgImageName];
    view.snowImgName = snowImageName;
    
    return view;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        self.clipsToBounds = YES;
        
        //添加背景图片的imageView
        self.bgImageView = [[UIImageView alloc] init];
        self.bgImageView.frame = self.bounds;
        self.bgImageView.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:self.bgImageView];
    }
    return self;
}

/** 开始下雪 */
- (void)beginSnow {
    //启动定时器,使得一直调用setNeedsDisplay从而被动调用 - (void)drawRect:(CGRect)rect
    //不能手动调用 - (void)drawRect:(CGRect)rect
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];
    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)drawRect:(CGRect)rect {
    //控制雪花最多的个数
    if (self.subviews.count > 250) {
        return;
    }
    
    //雪花的宽度
    int width = arc4random() % 20;
    while (width < 5) {
        width = arc4random() % 20;
    }
    //雪花的速度
    int speed = arc4random() % 15;
    while (speed < 5) {
        speed = arc4random() % 15;
    }
    

    //雪花起点y
    int startY = - (arc4random() % 100);
    //雪花起点x
    int startX = arc4random() % (int)[UIScreen mainScreen].bounds.size.width;
    //雪花终点x
    int endX = arc4random() % (int)[UIScreen mainScreen].bounds.size.width;
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.snowImgName]];
    imageView.frame = CGRectMake(startX, startY, width, width);
    
    [self addSubview:imageView];
    
    [UIView animateWithDuration:speed animations:^{
        //设置雪花最终的frame
        imageView.frame = CGRectMake(endX, [UIScreen mainScreen].bounds.size.height, width, width);
        //设置雪花的旋转
        imageView.transform = CGAffineTransformRotate(imageView.transform, M_PI );
        //设置雪花透明度,使得雪花快落地的时候像快消失的一样
        imageView.alpha = 0.3;
    } completion:^(BOOL finished) {
        //完成动画,就将雪花的imageView给移除掉
        [imageView removeFromSuperview];
    }]; 
}

@end

控制器的.m文件的内容

//
//  FFSnowflakesFallingVC.m
//  CollectionsOfExample
//
//  Created by mac on 16/7/30.
//  Copyright © 2016年 chenfanfang. All rights reserved.
//

#import "FFSnowflakesFallingVC.h"

//view
#import "FFSnowflakesFallingView.h"

@interface FFSnowflakesFallingVC ()

@end

@implementation FFSnowflakesFallingVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"雪花飘落效果";
    
    //创建雪花飘落效果的view
    FFSnowflakesFallingView *snowflakesFallingView = [FFSnowflakesFallingView snowflakesFallingViewWithBackgroundImageName:@"snow_background" snowImageName:@"snow" frame:self.view.bounds];
    //开始下雪
    [snowflakesFallingView beginSnow];
    
    [self.view addSubview:snowflakesFallingView];
}

@end

最后附上两张图片素材


1594675-8e2988c2674329fb.png
snow.png
1594675-45b3514b4acaf31c.png
snow_background.png

ios 雪花下坠效果

阅读数 511

iOS雪花飘落效果

阅读数 3

iOS雪花飘落效果

阅读数 4

没有更多推荐了,返回首页