老虎机的制作

简介: 用UIPickerView制作一个老虎机的效果 ViewController.h: #import @interface DXWViewController :UIViewController @property (retain,nonatomi...

用UIPickerView制作一个老虎机的效果

ViewController.h:

#import <UIKit/UIKit.h>

@interface DXWViewController :UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>

@property (retain,nonatomic) IBOutlet UIPickerView *picker;

- (IBAction)click:(id)sender;

- (IBAction)OK:(id)sender;

@property(nonatomic,retain)NSArray *images;

@property(nonatomic,retain)NSArray *column1;

@property(nonatomic,retain)NSArray *column2;

@property(nonatomic,retain)NSArray *column3;

@property(nonatomic,retain)NSArray *column4;

@property(nonatomic,retain)NSArray *column5;

@end

ViewController.m:

#import"DXWViewController.h"

@interfaceDXWViewController ()

@end

@implementation DXWViewController

- (void)viewDidLoad

{

    [superviewDidLoad];

    UIImage *image1 = [UIImageimageNamed:@"apple.png"];

    UIImage *image2 = [UIImage imageNamed:@"bar.png"];

    UIImage *image3 = [UIImage imageNamed:@"cherry.png"];

    UIImage *image4 = [UIImage imageNamed:@"crown.png"];

    UIImage *image5 = [UIImage imageNamed:@"lemon.png"];

    UIImage *image6 = [UIImage imageNamed:@"seven.png"];

    self.images =@[image1,image2,image3,image4,image5,image6];

    

    //创建30ImageView

    for (int i=0; i<5; i++) {

       UIImageView *imageView1 = [[UIImageViewalloc] initWithImage:image1];

       UIImageView *imageView2 = [[UIImageViewalloc] initWithImage:image2];

       UIImageView *imageView3 = [[UIImageViewalloc] initWithImage:image3];

       UIImageView *imageView4 = [[UIImageViewalloc] initWithImage:image4];

       UIImageView *imageView5 = [[UIImageViewalloc] initWithImage:image5];

       UIImageView *imageView6 = [[UIImageViewalloc] initWithImage:image6];

        NSArray *arr = @[imageView1,imageView2,imageView3,imageView4,imageView5,imageView6];

       NSString *str = [NSString stringWithFormat:@"column%d",i+1];

       //OC特有方法,对一个可能存在可能不存在的变量赋值,本来是一个变量,可以转化成字符串,这样就可以改变字符串了

        [selfsetValue:arr forKey:str]; //KVC

        [imageView1 release];

        [imageView2 release];

        [imageView3 release];

        [imageView4 release];

        [imageView5 release];

        [imageView6 release];

    }

    srandom(time(NULL));

    //默认选择在第5000

    for (int i=0; i<5; i++) {

        [self.pickerselectRow:5000inComponent:i animated:NO];

    }


}


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    return5;

}


-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

    return [self.imagescount]*10000;

}


-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

{

    //这种写法非常消耗内存

    //    UIImageView *imageView = [[UIImage alloc] initWithImage:[self.images objectAtIndex:row]];

    //    return imageView;

    NSString * str = [NSString stringWithFormat:@"column%d",component+1];

    NSArray *arr = [selfvalueForKey:str];

    return [arrobjectAtIndex:row%6];

    

}


- (void)dealloc {

    [_pickerrelease];

    [_imagesrelease];

    [_column1release];

    [_column2release];

    [_column3release];

    [_column4release];

    [_column5release];

    [superdealloc];

}

//退出

- (IBAction)click:(id)sender {

//    NSLog(@"%@",srandom(time(nil)));

    exit(0);

}

//确定

- (IBAction)OK:(id)sender {

    int a[5];

    bool f=false;

    for (int i=0; i<5; i++) {

        int row = random() % [self.column1count];

        int n = random() % 35;

        int j = row *n;

        [self.pickerselectRow:j inComponent:i animated:YES];

        a[i]=j%6;

    }

    int sum=0;

    for (int i=0; i<5; i++)

    {

        sum = 1;

        if (f==false)

        { 

            for (int j=i+1; j<5; j++)

            {

                if (a[i] == a[j])

                {

                    sum++;

                }

                if (sum>=3)

                {

                    f=true;

                    break;

                }

            }

        }

        else

        {

            break;

        }

    }

    if (f) {

       UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"我中了"message:@"中了500" delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:nil];

        [a show];

        }

}

@end

效果图:


音频文件的添加:

1.首先导入framework框架,AudioToolbox.framework

2.导入头文件

#import <AudioToolbox/AudioToolbox.h>

3.完善接口函数

-(void)playMusic:(NSString*)s

{

    //通过NSBundle来获得音频文件的url地址

    NSURL *url = [[NSBundle mainBundle] URLForResource:s withExtension:@"wav"];

    SystemSoundID winSound;//整形类型的id

    //用音频服务,为音频文件绑定soundID

    AudioServicesCreateSystemSoundID((CFURLRef)url, &winSound);

    //通过绑定的soundId来播放音乐

    AudioServicesPlayAlertSound(winSound);

}

4.调用

[self playMusic:@"win"];

相关文章
|
8月前
|
前端开发 JavaScript 编译器
聊聊制作网站那些事!
聊聊制作网站那些事!
46 0
|
4月前
在微信小游戏制作工具中制作一个下雨的效果
在微信小游戏制作工具中制作一个下雨的效果
27 0
|
4月前
微信小游戏制作工具中关于背景图的设置
微信小游戏制作工具中关于背景图的设置
97 0
|
4月前
水晶头制作
水晶头制作。
123 44
|
11月前
|
容器
如何在微信小游戏制作工具中快速的制作一个血条?
如图,添加了两个血条的图片,一个当作背景,一个当作前景,并且把这两个图片放到了一个“血条1”容器中,另外增加了一个“打你丫的”按钮,用来模拟攻击,点击“打你丫的”按钮,血条就会掉血。
149 0
|
11月前
|
搜索推荐 开发工具 Android开发
更“大”的微信小游戏制作工具
为什么这么久都不开发新功能了呢?因为人手和资源有限。这个可能跟很多人想的不一样,这个不是微信的产品吗?腾讯公司会没人没资源?大公司的实际情况可能还真跟很多人想的不一样。微信小游戏制作工具背后的团队不大,甚至比你想要的还要小,而且团队除了需要负责这个工具的维护之外,还有很多其它的事情需要处理。
180 0
|
传感器 IDE JavaScript
如何制作一个水平仪|学习笔记
快速学习如何制作一个水平仪。
682 0
如何制作一个水平仪|学习笔记
|
JavaScript 安全 Windows