iOS开发中生成随机数

本文介绍如何使用Objective-C结合随机数生成函数arc4random()在iOS应用中实现流星雨视觉效果。通过定时器控制流星出现频率,并利用随机数调整流星位置与速度,营造真实感。

有时候我们需要在程序中生成随机数,但是在Objective-c中并没有提供相应的函数,好在C中提供了rand()、srand()、random()、arc4random()几个函数。那么怎么使用呢?下面将简单介绍:

1、 获取一个随机整数范围在:[0,100)包括0,不包括100

int x = arc4random() % 100;

2、 获取一个随机数范围在:[500,1000),包括500,包括1000

int y = (arc4random() % 501) + 500;

3、 获取一个随机整数,范围在[from,to),包括from,包括to

-(int)getRandomNumber:(int)from to:(int)to

{

    return (int)(from + (arc4random() % (to – from + 1)));

}

以上转载来自于http://www.cnblogs.com/daguo/archive/2013/05/30/3107824.html,尊重原创

以下,根据产生的随机数,制作雪花飘落/星星陨落的效果:

//
//  ViewController.m
//  MeteorShower
//
//  Created by HZhenF on 2017/5/6.
//  Copyright © 2017年 Huangzhengfeng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong) NSMutableArray *arrM;

@property(nonatomic,strong) NSTimer *timer;

@property(nonatomic,strong) UIAlertController *alertC;

@property(nonatomic,assign) BOOL onClick;

@end

@implementation ViewController

-(UIAlertController *)alertC
{
    if (!_alertC) {
        _alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"点击屏幕暂停/开始流星陨落" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        [_alertC addAction:okAction];
    }
    return _alertC;
}

-(NSTimer *)timer
{
    if (!_timer) {
        _timer =  [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(snowShow) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    }
    return _timer;
}


-(NSMutableArray *)arrM
{
    if (!_arrM) {
        _arrM = [NSMutableArray array];
    }
    return _arrM;
}

-(BOOL)prefersStatusBarHidden
{
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];


    self.view.backgroundColor = [UIColor blackColor];

   //主动调用一次
    [self timer];

}


-(void)snowShow
{
    UIImageView *imageView = nil;
    int x = (int)self.view.bounds.size.width;
    //下落速度
    int speed = (arc4random() % 2) + 3;
    /**如果有数组里面有UIImageView,直接取来用就行**/
    if (self.arrM.count > 0) {
       imageView = [self.arrM firstObject];
       [self.arrM removeObject:imageView];
    }
    /**如果数组为空,那么创建UIImageView**/
    else
    {
        imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"start"]];
        [self.view addSubview:imageView];
    }
    imageView.frame = CGRectMake(arc4random() % x , -50, 24, 24);
    [UIView animateWithDuration:speed animations:^{
        imageView.alpha = 0.2;
        imageView.frame = CGRectMake(arc4random() % x, self.view.bounds.size.height/4, 24, 24);
    } completion:^(BOOL finished) {
        imageView.alpha = 1;
            [UIView animateWithDuration:speed animations:^{
                    imageView.alpha = 0.2;
                    imageView.frame = CGRectMake(CGRectGetMinX(imageView.frame), self.view.bounds.size.height -24, 24, 24);
                } completion:^(BOOL finished) {
                    imageView.alpha = 1;
                    [self.arrM addObject:imageView];
        }];
    }];
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (!self.onClick) {
        [self.timer invalidate];
        self.timer = nil;
        self.onClick = YES;
        [self presentViewController:self.alertC animated:YES completion:^{

        }];

    }
    else
    {
        self.onClick = NO;
        [self timer];
        [self presentViewController:self.alertC animated:YES completion:^{

        }];
    }
}





@end

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值