Search code examples
iphoneobjective-canimationcore-animation

Animation on UIButton Click in iPhone


Everybody.. How to set animation on button click ?

I have two buttons, both have different frame..

First   CGRectMake (10 , 10 , 100 , 80 ); It has Black Image,
Second  CGRectMake (200, 10 , 210 , 80 ); It has White Image,

When I click on First button of those, It should be animate to move from one frame to another frame..

Only seen like first's button Black Image move to second button location.. Like Image changes of Buttons..

After clicked button frame does not change... Should be remain as First and Second..

I have tried but not got the exact

buttonFirst.frame = buttonSecond.frame;
buttonFirst.center = buttonSecond.center; 
buttonFirst.transform = CGAffineTransformMakeTranslation( buttonSecond.frame.origin.x - buttonFirst.frame.origin.x , buttonSecond.frame.origin.y - buttonFirst.frame.origin.y);

How to animate this ?

Editting :

I need to move one checkers from one CGRect to Another CGRect.. So, It want to display like moving checkers, But i dont want to change CGRect, Only button images looks like that...

Please tell me.. Thanks...


Solution

  • Finally i got the answer...

    I have set two button in two separate view. after set one timer, and move first button image to another button's position.. Here is a sample code for that..

    Step 1 : Set view with Timer

    UIView *oldView = [[UIView alloc] init];  
                        oldView = [viewArray objectAtIndex:selectedIndex];
                        UIView *newView = [[UIView alloc] init];
                        newView = [viewArray objectAtIndex:[sender tag]]; 
                        oldRect = [oldView frame];                    
                        newRect = [newView frame];
                        movingTimer = [[NSTimer alloc] init];
                        movingTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(movingCheckers:) userInfo:nil repeats:YES];
    
                        imageViewMove = [[UIImageView alloc] init];
                        [imageViewMove setFrame:oldRect];
                        [imageViewMove setImage:[UIImage imageNamed:blackManName]];
    
                        [self.view bringSubviewToFront:imageViewMove];
                        [self.view addSubview:imageViewMove];
    

    Step 2 : Moving Image to second position

    CGPoint aPoint = CGPointMake(newRect.origin.x - oldRect.origin.x, oldRect.origin.y - newRect.origin.y);                    
    imageViewMove.frame = CGRectMake(imageViewMove.frame.origin.x + (aPoint.x / 6) , imageViewMove.frame.origin.y - (aPoint.y / 6), imageViewMove.frame.size.width, imageViewMove.frame.size.height);
    if (imageViewMove.frame.origin.x >= newRect.origin.x)
    {
                    [selectButton setBackgroundImage:nil forState:UIControlStateNormal]; 
                    [moveButton setBackgroundImage:imageViewMove.image forState:UIControlStateNormal];
                    [imageViewMove removeFromSuperview];
                    [movingTimer invalidate];
    }
    

    Now its work.....