CustomPickerViewController.m
上传用户:jjjjag8
上传日期:2017-04-17
资源大小:1443k
文件大小:4k
源码类别:

iPhone

开发平台:

MultiPlatform

  1. //
  2. //  CustomPickerViewController.m
  3. //  Pickers
  4. //
  5. //  Created by Jeff LaMarche on 7/7/08.
  6. //  Copyright 2008 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "CustomPickerViewController.h"
  9. #import <AudioToolbox/AudioToolbox.h>
  10. @implementation CustomPickerViewController
  11. @synthesize picker;
  12. @synthesize column1;
  13. @synthesize column2;
  14. @synthesize column3;
  15. @synthesize column4;
  16. @synthesize column5;
  17. @synthesize winLabel;
  18. @synthesize button;
  19. -(void)showButton
  20. {
  21. button.hidden = NO;
  22. }
  23. -(void)playWinSound
  24. {
  25. NSString *path = [[NSBundle mainBundle] pathForResource:@"win" ofType:@"wav"];
  26. SystemSoundID soundID;
  27. AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
  28. AudioServicesPlaySystemSound (soundID);
  29. winLabel.text = @"WIN!";
  30. [self performSelector:@selector(showButton) withObject:nil afterDelay:1.5];
  31. }
  32. -(IBAction)spin:(id)sender
  33. {
  34. BOOL win = NO;
  35. int numInRow = 1;
  36. int lastVal = -1;
  37. for (int i = 0; i < 5; i++)
  38. {
  39. int newValue = random() % [self.column1 count];
  40. if (newValue == lastVal)
  41. numInRow++;
  42. else
  43. numInRow = 1;
  44. lastVal = newValue;
  45. [picker selectRow:newValue inComponent:i animated:YES];
  46. [picker reloadComponent:i];
  47. if (numInRow >= 3)
  48. win = YES;
  49. }
  50. button.hidden = YES;
  51. NSString *path = [[NSBundle mainBundle] pathForResource:@"crunch" ofType:@"wav"];
  52. SystemSoundID soundID;
  53. AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
  54. AudioServicesPlaySystemSound (soundID);
  55. if (win)
  56. [self performSelector:@selector(playWinSound) withObject:nil afterDelay:.5];
  57. else
  58. [self performSelector:@selector(showButton) withObject:nil afterDelay:.5];
  59. winLabel.text = @"";
  60. }
  61. - (void)viewDidLoad {
  62. UIImage *seven = [UIImage imageNamed:@"seven.png"];
  63. UIImage *bar = [UIImage imageNamed:@"bar.png"];
  64. UIImage *crown = [UIImage imageNamed:@"crown.png"];
  65. UIImage *cherry = [UIImage imageNamed:@"cherry.png"];
  66. UIImage *lemon = [UIImage imageNamed:@"lemon.png"];
  67. UIImage *apple = [UIImage imageNamed:@"apple.png"];
  68. for (int i = 1; i <= 5; i++)
  69. {
  70. UIImageView *sevenView = [[UIImageView alloc] initWithImage:seven];
  71. UIImageView *barView = [[UIImageView alloc] initWithImage:bar];
  72. UIImageView *crownView = [[UIImageView alloc] initWithImage:crown];
  73. UIImageView *cherryView = [[UIImageView alloc] initWithImage:cherry];
  74. UIImageView *lemonView = [[UIImageView alloc] initWithImage:lemon];
  75. UIImageView *appleView = [[UIImageView alloc] initWithImage:apple];
  76. NSArray *imageViewArray = [[NSArray alloc] initWithObjects: sevenView, barView, crownView, cherryView, lemonView, appleView, nil];
  77. NSString *fieldName = [[NSString alloc] initWithFormat:@"column%d", i];
  78. [self setValue:imageViewArray forKey:fieldName];
  79. [fieldName release];
  80. [imageViewArray release];
  81. [sevenView release];
  82. [barView release];
  83. [crownView release];
  84. [cherryView release];
  85. [lemonView release];
  86. [appleView release];
  87. }
  88. // Seed the random number generator
  89. srandom(time(NULL));
  90. }
  91. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  92. if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  93. // Initialization code
  94. }
  95. return self;
  96. }
  97. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  98. // Return YES for supported orientations
  99. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  100. }
  101. - (void)didReceiveMemoryWarning {
  102. [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
  103. // Release anything that's not essential, such as cached data
  104. }
  105. - (void)dealloc {
  106. [picker release];
  107. [column1 release];
  108. [super dealloc];
  109. }
  110. #pragma mark -
  111. #pragma mark Picker Data Source Methods
  112. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  113. {
  114. return 5;
  115. }
  116. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  117. {
  118. return [self.column1 count];
  119. }
  120. #pragma mark Picker Delegate Methods
  121. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
  122.   forComponent:(NSInteger)component reusingView:(UIView *)view
  123. {
  124. if (component == 0)
  125. return [self.column1 objectAtIndex:row];
  126. else if (component == 1)
  127. return [self.column2 objectAtIndex:row];
  128. else if (component == 2)
  129. return [self.column3 objectAtIndex:row];
  130. else if (component == 3)
  131. return [self.column4 objectAtIndex:row];
  132. return [self.column5 objectAtIndex:row];
  133. }
  134. @end