Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xcode tap button while moving with UIViewAnimation

I can't make a button action when the button is moving. Can anyone help me how to hide the moving button when tapping on it? It is not reacting when i tap the button. here's the code:

-(void)createTurtle
{

    NSUInteger r = arc4random_uniform(284) + 1;

    NSUInteger randomTitle = arc4random_uniform(1000000) + 1;

    turtle = [[UIButton alloc] init];
    turtle.frame = CGRectMake(r, 0, 36, 47);
    [turtle setImage:[UIImage imageNamed:@"turtle.png"] forState:UIControlStateNormal];
    [turtle addTarget:self action:@selector(turtleTouched:) forControlEvents:UIControlEventTouchDown];
    [turtle setTitle:[NSString stringWithFormat:@"%lu", (unsigned long)randomTitle] forState:UIControlStateNormal];
    [turtle setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view bringSubviewToFront:turtle];
    [self.view addSubview:turtle];



    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:16];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    turtle.frame = CGRectMake(turtle.frame.origin.x, self.view.frame.size.height, 36, 47);
    [UIView commitAnimations];


}







    - (void) turtleTouched: (id) sender
{
    UIButton *button = sender; // Typecast sender
    button.hidden = YES;
}
like image 636
profidash_98 Avatar asked Jul 28 '26 22:07

profidash_98


1 Answers

your method won't fire even though you click on it. but it will fire when you click in its final frame while animating.

Lets say you button has inital frame of (0,0,100,100),
Now you moving it to a frame of (200,200,100,100).

while moving , if you click in region of (200,200,100,100)-final frame, then you will get events. but in areas in middle of path like (50,50,100,100), you won't get events.

Because when you start animating , the frame of button instantly changes to final frame while the button just transition from initial position to final position.

So, instead of doing it, you can override touchesBegan method of your viewController and check whether the touch-point might lie in the transitional frame.

transitionFrame = [button.layer.presentationLayer frame];

Frame of your button will always be final frame throughout the animation.

Code added:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint p =[((UITouch *)[touches anyObject]) locationInView:self.view];
    CGRect r= [turtle.layer.presentationLayer frame];
    BOOL contains= CGRectContainsPoint(r, p);
    if(contains)
        turtle.hidden=YES;

}
like image 171
santhu Avatar answered Jul 31 '26 16:07

santhu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!