我正在编写一个由 2UIImageViews
和 2组成的简单应用程序UIButtons
。被UIImageViews
放置在UIButtons
.
我有 1UIImage
随机出现UIImageView
并在 2 秒后消失。然后用户必须点击他们认为出现图像的按钮。
我打乱UIImageView
数组并使用第一个元素(索引 0)作为要显示的图像。改组时,我会跟踪哪个元素(即来自哪个索引。假设来自索引“n”)被放置在数组的索引 0 上。
但是,当按下按钮时,我将按下按钮的 id 与索引为 n 的按钮的 id 进行比较。(因为那是 random 的索引UIImageView
)。但由于某种原因,它不起作用。:(
这是我的代码:(我没有上传 .h 文件。它只包含声明。)下面的代码不会产生任何错误/警告消息。它只是没有输出我想要的结果。
@interface ViewController ()
@property (strong, nonatomic) NSMutableArray* tempButton;
@property (strong, nonatomic) NSMutableArray *tempViews;
@property (strong, nonatomic) UIImage *myImage;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_myImage = [UIImage imageNamed:@"hello"];
_tempButton = [[NSMutableArray alloc] initWithObjects:_button1, _button2, nil];
_tempViews = [[NSMutableArray alloc]initWithObjects:_image1, _image2, nil];
[self displayonView];
}
-(void)displayToFindSymbol{
[_toFindImage setImage:_myImage];
_toFindImage.contentMode = UIViewContentModeScaleAspectFit;
}
- (IBAction)pressed:(id)sender {
UIButton *result = (UIButton *)sender;
if (result == _tempButton[_correctButton]) {
NSLog (@"Correct");
}
else if (result != _tempButton[_correctButton]){
NSLog (@"Incorrect");
}
}
-(NSMutableArray *)shuffleViews{
NSUInteger count = _tempViews.count;
int n;
for (int i=count-1; i>=0; --i) {
n = arc4random() % (i + 1);
[_tempViews exchangeObjectAtIndex:n withObjectAtIndex:i];
}
_correctButton = n;
return _tempViews;
}
-(void)displayonView{
NSMutableArray *tempArray;
tempArray = [self shuffleViews]; // shuffle views
_correctImage = [tempArray objectAtIndex:0];
_correctImage.contentMode = UIViewContentModeScaleAspectFit;
[_correctImage setImage:_myImage];
NSTimer* myTImer;
myTImer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(hideLabel) userInfo:nil repeats:NO];
}
-(void) hideLabel{
_correctImage.hidden = YES;
for (UIButton *each in self.tempButton) {
each.enabled = YES;
}
[self displayToFindSymbol];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}