views:

119

answers:

3

Hi, I'm trying to animate a UIButton to move up the screen. At any point the user can touch it. However, it doesn't seem to respond to touches while moving, only at the start and end of its animation. I guess this is because the button itself isn't moving, just the image of it. Any ideas how I can solve this? Here's my code so far. Thanks!

- (void)viewDidLoad {
    [self newBubble];
    [super viewDidLoad];
}

- (void)newBubble {
    UIButton *bubble = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
    bubble.frame = CGRectMake(10, 380, 50, 50);
    UIImage *bubbleImage = [UIImage imageNamed:@"bubble.png"];
    [bubble setBackgroundImage:bubbleImage forState:UIControlStateNormal];
    bubble.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [bubble addTarget:self action:@selector(bubbleBurst:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:bubble];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:5.0];
    CGAffineTransform newTransform = CGAffineTransformMakeScale(1.5, 1.5);
    bubble.transform = CGAffineTransformTranslate(newTransform, 10, -200);
    [UIView commitAnimations];
}

- (IBAction)bubbleBurst:(id)sender {
    NSLog(@"Bubble burst");
    UIButton *bubbleBurst = sender;
    [bubbleBurst removeFromSuperview];
}
A: 

I'm pretty sure you would have to use Core Animation if you want the button to respond to touches while it is moving, but I have never done this so this is really more a tip then an answer!

Alan Taylor
Indeed I expect you are right. Any idea which classes to use though? I tried CAKeyframeAnimation, moving the 'position' property of the layer but same problem. As with CABasicAnimation. I'll keep at it, just hoping someone might be able to point me there sooner? Perhaps I should update the position of the button using an NSTimer? However this would get pretty messy with multiple buttons...
Smikey
A: 

I agree. I think the problem is that you're not using Core Animation. I can't believe you couldn't figure that out for yourself.

Zak833
Omg Zak what are you doing on here? I actually thought that was a real comment at first and got all embarrassed. Go and do something useful and stop net stalking me ;)
Smikey
I am doing useful things! Research, my friend... Research...
Zak833
Well if you need an iPhone app you know where to come. Provided it doesn't use core animation :)
Smikey
A: 

Well after posting a similar question, I found the answer here

Basically, you have to move the button frame by frame using an NSTimer. In my case I was worried there would be too many NSTimers involved moving each button, but in the end I used a single timer which loops through an array of buttons and moves them all one by one.

Smikey