简体   繁体   中英

Detect which UIButton is pressed - UIImageView

I am writing a simple application that consists of 2 UIImageViews and 2 UIButtons. The UIImageViews are placed on top of the UIButtons.

I have 1 UIImage that appears on a random UIImageView and disappears after 2 seconds. The user then has to tap on the button they think the image appeared on.

I shuffle the UIImageView array and use the first element (index 0) for the image to be displayed on. When shuffling I keep track of which element (ie from which index. lets say from index "n") was placed on index 0 of the array.

However, when a button is pressed i compare the id of the button pressed with the id of the button with index n. (because that is the index of the random UIImageView). But for some reason it is not working. :(

Here is my code: (i didn't upload the .h file. it just contains declarations.) The code below doesn't produce any error/warning messages. It just doesn't output the result I want.

@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.
}

您可以让每个按钮调用不同的方法作为选择器,或者为每个按钮分配不同的标记并在共享方法中检查它。

Add all imageviews in an array and also all buttons in an array.

when setting image to random image view store its index and use the index later on to get the corresponding button that should have been clicked.

Pseudo code

NSArray a = [NSArray arrayWithObjects:imgVie1,ImgView2...,nil]
// create second array with buttons 

int randomIndex = arc4random() % a.count;
UIImageView *imgView = [a objectAtIndex:randomIndex];
//set image and hide later

UIButton *correctButton = [secondarray objectAtIndex:randomIndex];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM