简体   繁体   中英

I can't touch buttons during uiview animation in ios

Here is my part of code

- (void)moveToRightViewAnimationMethod{

[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{

    //exampleView.userInteractionEnabled = YES;
    exampleViewHorizantalConstraint.constant = -(screenSizeWidth/2);
    [exampleView layoutIfNeeded];

} completion:^(BOOL finished) {

    [self moveToLeftViewAnimationMethod];

}];

}

- (void)moveToLeftViewAnimationMethod{

[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{

    //exampleView.userInteractionEnabled = YES;
    exampleViewHorizantalConstraint.constant = (screenSizeWidth/2);
    [exampleView layoutIfNeeded];

} completion:^(BOOL finished) {

    [self moveToRightViewAnimationMethod];

}];

}

i have 8 buttons in my exampleView and i am trying to repeatly animation but when i click my buttons, i can't get events. I also tried this part of code

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
for (UIButton *button in self.buttonsOutletCollection)
{
    if ([button.layer.presentationLayer hitTest:touchLocation])
    {
        // This button was hit whilst moving - do something with it here
        break;
    }
}
}

What should i do for solve this? Thank you for your interest and answer. :)

I just tried your code and I was able to handle the IBAction from a button inside a subview being animated. Here is my code, which is a version of your code with slight variations in the animation block:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}

- (IBAction)buttonAction:(id)sender {
    NSLog(@"Button action triggered!");
}

- (IBAction)startAnimationAction:(id)sender {
[self moveToRightViewAnimationMethod];
}

- (void)moveToRightViewAnimationMethod{
// Layout if needed before animating.
    [self.view layoutIfNeeded];
// Change constraint constant outside animation block.
    self.centerYConstraint.constant -= 20;
    [UIView animateWithDuration:2.0f delay:0    options:UIViewAnimationOptionAllowUserInteraction animations:^{
    [self.view layoutIfNeeded];
} completion:^(BOOL finished) {
    [self moveToLeftViewAnimationMethod];
}];
}

- (void)moveToLeftViewAnimationMethod{
[self.view layoutIfNeeded];
self.centerYConstraint.constant += 20;
[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
    [self.view layoutIfNeeded];
} completion:^(BOOL finished) {
    [self moveToRightViewAnimationMethod];
}];
}
@end

Red square: Subview to be animated by changing its center Y constraint. 红场:要动画的子视图。

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