2
votes

I have looked at UIButton can't be touched while animated with UIView animateWithDuration and the answer doesn't help me.

for (UIButton *button in self.buttonsOutletCollection)

This part is the one that is confusing. My button isn't part of a IBOutletCollection so this doesn't work. I use this to move the button:

- (void) squareOneMover {
    CGRect oldFrame = _squareOne.frame;
    [UIView animateWithDuration:2
                          delay:0.0
                        options:UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         CGRect newFrame = CGRectMake(oldFrame.origin.x , oldFrame.origin.y + 500, oldFrame.size.width, oldFrame.size.height);
                         _squareOne.frame = newFrame;
                     }
                     completion:nil
    ];
}

I don't want to have to move the Button in small amounts because I need five buttons and it would be inefficient.

Can someone provide me with an answer similar to the question above that is for UIButtons not include in any array?

Edit: The UIButton is defined in the interface file and connected to Interface Builder, and is strong and nonamatic

Edit 2: I am now using animateWithDuration:delay:options:animations:completion: but it still doesn't work.

I use this code to detect if the button is being pressed.

- (void)squareOnePressed:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    if ([self.squareOne.layer.presentationLayer hitTest:touchLocation])
    {
        NSLog(@"YO");
    }
}

But when the button is pressed nothing still happens... So how do I program that ^^^^

3
where do you define and how do you define the UIButton? Your question is too broad. - carlodurso
So did you add the option of UIViewAnimationOptionAllowUserInteraction in animateWithDuration:delay:options:animations:completion:? - rdelmar
Is the UIButton connected to Interface Builder? If so, you need to make it weak. - ray
@ray It is connected but it has to stay strong because I use constraints written programatically. self.squareOne = [[UIButton alloc] init]; creates an error when the button is weak - Minestrone-Soup
You shouldn't be calling alloc init on self.squareOne if you created that button in IB -- it's already instantiated, why are you creating a new one? - rdelmar

3 Answers

4
votes

Try calling animateWithDuration:delay:options:animations:completion: with UIViewAnimationOptionAllowUserInteraction in the options parameter.

Add an IBAction to your view controller and connect your button to that. Put your NSLog in the IBAction method. (Do you know how to do this?)

0
votes

Why is your method named squareOneTouched:? Is your IBAction triggering that?

If you're subclassing a UIViewController, you should just override the touchesBegan: method.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    if ([self.squareOne.layer.presentationLayer hitTest:touchLocation])
    {
        // This button was hit whilst moving - do something with it here
        NSLog(@"YO");
    }
}
0
votes

I found the problem. The constraints are preventing me from interacting with the button. When I remove the programmatically written constraints from the button, it works fine.