1

I have taken two button and that are moving from left to right continuously but when i am trying to click one of them. i am not able to detect which button is clicked even i have set UIViewAnimationOptionAllowUserInteraction property on animation but its not working for me .

{
m_pFacebookPostButton=[UIButton buttonWithType:UIButtonTypeCustom];
m_pFacebookPostButton.frame=CGRectMake(-320, 410, 320, 50);
[m_pFacebookPostButton retain];
[m_pFacebookPostButton setBackgroundColor:[UIColor greenColor]];
m_pFacebookPostButton.tag=204;
[m_pFacebookPostButton setTitle:@"Facebook" forState:UIControlStateNormal];
//[m_pFacebookPostButton addTarget:self action:@selector(clickone) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:m_pFacebookPostButton];

m_pFacebookPostButton1=[UIButton buttonWithType:UIButtonTypeCustom];
m_pFacebookPostButton1.frame=CGRectMake(0, 410, 320, 50);
[m_pFacebookPostButton1 retain];
[m_pFacebookPostButton1 setBackgroundColor:[UIColor redColor]];
m_pFacebookPostButton1.tag=205;
[m_pFacebookPostButton1 setTitle:@"Twitter" forState:UIControlStateNormal];
//[m_pFacebookPostButton1 addTarget:self action:@selector(clicktwo) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:m_pFacebookPostButton1];

[UIView animateWithDuration:5.0
                      delay:0.0
                    options:(UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction)
                 animations:^{
                     [UIView setAnimationDelegate:self];
                     [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];


                      m_pFacebookPostButton.frame = CGRectMake(0, 410, 320, 50);
                      m_pFacebookPostButton1.frame = CGRectMake(320, 410, 320, 50);



                 }
                 completion:^(BOOL finished){
                     NSLog(@"Move to left done");
                 }];

}

Please help me to move out this problem ,Thanx in advance for helping me.

2 Answers2

0

You can create a custom button object (New File > SubClass of: UIButton) and add to the bottom

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (CGRectContainsPoint(self.bounds, point)) {
        NSLog("Button got hit");
        return self;
    }
    else {
        return nil
    }
}

and use this custom button to in your viewController

myCustomButton * m_pFacebookPostButton = [[myCustomButton alloc] init];
Efesus
  • 197
  • 2
  • 17
0

Seems like the issue regarding Quartz2D.

Refer this==> Is it possible to successful animate a moving UIButton?

Engin Kurutepe stated that,

This sounds a bit too complicated for UIKit and CoreAnimation. It seems like you're developing something like a game. Why not use a global animation timer for let's say ~60 fps and do your drawing in Quartz2D? Then for each touch you could quickly check any hits between Quartz refreshes.

EDIT: Try using other animation methods.

Example:

-(void)hello
{
    NSLog(@"hello");

}
-(void)animationDone
{
    NSLog(@"done");
}

UIButton *button=[[UIButton alloc]init];
button.frame = CGRectMake(30, 50, 100, 100);
button.backgroundColor=[UIColor redColor];
[button addTarget:self action:@selector(hello) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:button];
// animate
[UIView beginAnimations:@"button_in" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDone)];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationBeginsFromCurrentState:YES];
[button setFrame:CGRectMake(100.0, 100.0, 100.0, 30.0)];
[UIView commitAnimations];

Hope it helps...

Community
  • 1
  • 1
Master Stroke
  • 5,108
  • 2
  • 26
  • 57