用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];
//创建30个ImageView
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"];
转载于:https://blog.51cto.com/dingxiaowei/1366432