我試圖用UIPickerView建立「老虎機」遊戲。我使用UIImageView填充它。問題在於,每次旋轉後,拾取器視圖中的隨機圖像都會消失,並隨機再次顯示。它發生在使用ios 5.1.1的設備上,但是在使用ios4.2.1和模擬器的設備上它完美地工作。現在我使用的是CircularPickerView,但在使用UIPickerView之前,問題是一樣的。 下面是截圖:http://axgrzesiek.w.interii.pl/licznik.PNGUIPickerView消失在選取器內的圖像
GraViewController.h:
#import <UIKit/UIKit.h>
#import "CircularPickerView.h"
@interface GraViewController : UIViewController
<UIPickerViewDataSource, UIPickerViewDelegate> {
CircularPickerView *picker;
UILabel *winLabel;
NSArray *column1;
NSArray *column2;
NSArray *column3;
}
@property(nonatomic, retain) IBOutlet CircularPickerView *picker;
@property(nonatomic, retain) IBOutlet UILabel *winLabel;
@property(nonatomic, retain) NSArray *column1;
@property(nonatomic, retain) NSArray *column2;
@property(nonatomic, retain) NSArray *column3;
-(IBAction)spin;
@end
GraViewController.m:
- (IBAction)spin {
BOOL win = NO;
int numInRow = 1;
int lastVal = -1;
for (int i = 0; i < 3; i++) {
int newValue = arc4random() % [self.column1 count];
if (newValue == lastVal)
numInRow++;
else
numInRow = 1;
lastVal = newValue;
//if (newValue < [self.column1 count] || newValue >= (2 * [self.column1 count])) {
//newValue = newValue % [self.column1 count];
//newValue += [self.column1 count];
[self.picker selectRow:newValue inComponent:i animated:YES];
//}
NSLog(@"kol:%d %d",i, newValue);
//[picker reloadComponent:i];
if (numInRow >= 3)
win = YES;
}
if (win)
winLabel.text = @"WIN!";
else
winLabel.text = @"";
//[picker reloadAllComponents];
//[self performSelector:@selector(moveIntoPosition) withObject:nil afterDelay:0.5f];
- (void)viewDidLoad {
NSString *[email protected]"Gra";
[self setTitle:title];
UIImage *seven = [UIImage imageNamed:@"jeden.png"];
UIImage *bar = [UIImage imageNamed:@"dwa.png"];
UIImage *crown = [UIImage imageNamed:@"trzy.png"];
UIImage *cherry = [UIImage imageNamed:@"cztery.png"];
UIImage *lemon = [UIImage imageNamed:@"piec.png"];
UIImage *apple = [UIImage imageNamed:@"szesc.png"];
for (int i = 1; i <= 3; i++) {
UIImageView *sevenView = [[UIImageView alloc] initWithImage:seven];
UIImageView *barView = [[UIImageView alloc] initWithImage:bar];
UIImageView *crownView = [[UIImageView alloc] initWithImage:crown];
UIImageView *cherryView = [[UIImageView alloc] initWithImage:cherry];
UIImageView *lemonView = [[UIImageView alloc] initWithImage:lemon];
UIImageView *appleView = [[UIImageView alloc] initWithImage:apple];
NSArray *imageViewArray = [[NSArray alloc] initWithObjects:
sevenView, barView, crownView, cherryView, lemonView, appleView, nil];
NSString *fieldName = [[NSString alloc] initWithFormat:@"column%d", i];
[self setValue:imageViewArray forKey:fieldName];
//column1=[[NSArray alloc]initWithArray:imageViewArray];
//column2=[[NSArray alloc]initWithArray:imageViewArray];
//column3=[[NSArray alloc]initWithArray:imageViewArray];
[fieldName release];
[imageViewArray release];
[sevenView release];
[barView release];
[crownView release];
[cherryView release];
[lemonView release];
[appleView release];
//[picker selectRow: [self.column1 count] * (48/2) inComponent:i-1 animated:NO];
}
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
NSString *arrayName = [[NSString alloc] initWithFormat:@"column%d", component+1];
NSArray *array = [self valueForKey:arrayName];
[arrayName release];
return [array objectAtIndex:row];
}
僅發佈與您的問題相關的代碼。 – Vladimir
我不能,因爲我不知道問題究竟在哪裏 – Wojczitsu
但它絕對不是**在註釋掉的負載視圖方法,例如。幫助其他人來幫助你 - 清除代碼:) – Vladimir