Why is this button does not respond? [closed]
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 300.0, 43.0, 43.0)];
[imageView setBackgroundColor:[UIColor clearColor]];
[imageView setImage:[UIImage imageNamed:@"test.png"]];
[self.view addSubview:imageView];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView animateWithDuration: 0.2f
delay: 0.0f
options: UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState
animations: ^(void){[imageView setFrame:CGRectMake(0.0, 350.0, 43.0, 43.0)];}
completion: nil];
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(100, 0, 100, 30)];
[button setTitle:@"Button" forState:UIControlStateNormal];
[self.view addSubview:button];
Try amending your animateWithDuration: see below
[UIView animateWithDuration: 0.2f
delay: 0.0f
options: UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState
animations: ^(void){[imageView setFrame:CGRectMake(0.0, 350.0, 43.0, 43.0)];}
completion: nil];
Within the options property I've added the value UIViewAnimationOptionAllowUserInteraction
If you are not able touch the button (if the click is not working) then
write the code
[self.view bringSubviewToFront:button];
at the end of the method.
I think you want to action perform on button so try this code.
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(100, 0, 100, 30)];
[button setTitle:@"Button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
-(IBAction)buttonPressed:(id)sender
{
//put code here you want to perform action on button
}
Make below change in Button definition:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
And you method should be like below:
-(IBAction)buttonHandle:(id)sender
{
}
I hope it will be helpful.
精彩评论