2

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 ^^^^

Community
  • 1
  • 1
Minestrone-Soup
  • 399
  • 1
  • 16
  • where do you define and how do you define the UIButton? Your question is too broad. – carlodurso Oct 23 '14 at 00:19
  • So did you add the option of UIViewAnimationOptionAllowUserInteraction in animateWithDuration:delay:options:animations:completion:? – rdelmar Oct 23 '14 at 00:20
  • Is the `UIButton` connected to Interface Builder? If so, you need to make it `weak`. – ray Oct 23 '14 at 00:46
  • @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 Oct 23 '14 at 00:59
  • 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 Oct 23 '14 at 01:15
  • @rdelmar I do this because I do this: `[self.view addSubview:self.squareOne];` And if I don't, the constraints get screwed up – Minestrone-Soup Oct 23 '14 at 01:32
  • But why are you adding it as a subview of self.view? Didn't you already make it a subview of self.view in IB? – rdelmar Oct 23 '14 at 04:28
  • @rdelmar The answer why is here: http://stackoverflow.com/questions/26499193/positioning-uibuttons-at-top-of-screen – Minestrone-Soup Oct 23 '14 at 22:10

3 Answers3

4

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?)

Daniel T.
  • 32,821
  • 6
  • 50
  • 72
0

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");
    }
}
ray
  • 1,966
  • 4
  • 24
  • 39
  • I just named it `squareOneTouched` because I need 5 of these pieces of code, and I worried it would get confusing. Also, when I use this, it still doesn't work – Minestrone-Soup Oct 23 '14 at 01:03
  • Then you should add an `if` statement for each square you have inside the method above. Did you import `QuartzCore` as the other thread suggested? – ray Oct 23 '14 at 01:08
  • Yes, in both files. I can't do multiple if statements though. This is because I actually want the method to be of type `BOOL` and `return YES;` if the condition is true. I am just using `void` and `NSLog` to see if I can detect the button press – Minestrone-Soup Oct 23 '14 at 01:13
0

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.

Minestrone-Soup
  • 399
  • 1
  • 16
  • 1
    If you had constraints on your buttons, then you shouldn't have been setting new frames in the animation. You should be adjusting the constraints instead. You should not set any frames on views that use layout constraints. – rdelmar Oct 24 '14 at 00:01
  • @rdelmar No, what I mean is all of the constraints were declared programmatically. No constraint were made using the interface builder – Minestrone-Soup Oct 24 '14 at 00:38
  • I didn't say anything in my comment about interface builder. It doesn't matter whether you make the constraints in IB or in code, the same rule applies -- if you're using constraints, don't set frames. – rdelmar Oct 24 '14 at 00:44
  • @rdelmar Then can you answer http://stackoverflow.com/questions/26499193/positioning-uibuttons-at-top-of-screen without setting frames. Because when I get rid of that section of code the constraints get screwed up – Minestrone-Soup Oct 24 '14 at 00:57
  • You already accepted Zhang's answer to that question, and that answer does it without setting any frames. I answered a similar question here, http://stackoverflow.com/questions/26268387/auto-layout-equal-space-between-uibuttons/26268470#26268470 that uses spacer views, so it keeps the buttons spaced equally no matter the screen size. – rdelmar Oct 24 '14 at 01:13
  • @rdelmar But he calls alloc and init on btn1 – Minestrone-Soup Oct 24 '14 at 01:34
  • So? Why is that an issue? He's creating all his buttons in code, so he has to do that. – rdelmar Oct 24 '14 at 01:35