2011-09-27 49 views
0

大家.. 如何设置按钮点击动画?UIButton上的动画点击iPhone

我有两个按钮,两者具有不同的帧..

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

当我点击者的第一个按钮,它应该以动画从一帧移动到另一帧..

只有见过像第一的按钮黑色图像移动到第二个按钮的位置。像按钮的图像变化..

后点击按钮框架不会改变......应该继续作为第一和第二..

我都试过,但没有得到确切的

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

如何制作动画呢?

EDITTING:

我需要从一个的CGRect移动一个棋子到另一种的CGRect。所以,它要显示像移动棋子,但我不想改变的CGRect,只有按钮图像看起来像这样.. 。

请告诉我.. 谢谢...

回答

0

最后我得到的答案...

我在两个单独的视图中设置了两个按钮。设置之后一个定时器,和第一按钮图像移动到另一个按钮的位置.. 下面是一个示例代码..

步骤1:用定时器设定的视图

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]; 

步骤2:运动图像第二位置

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]; 
} 

现在其工作.....

0

以及.. 你可以试试这个链接

[UIView beginAnimation/endAnimation] bock 
+0

是,还..这里是一个代码..的UIImageView * ImageView的= [[ALLOC的UIImageView] INIT]; [imageView setFrame:button.frame]; [imageView setImage:buttonS.currentBackgroundImage]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:2.0]; [imageView setFrame:buttonS.frame]; [UIView commitAnimations]; [imageView removeFromSuperview];但没有工作.. –

0

这是例如,从工作程序:

[self.buttonFirst setUserInteractionEnabled:NO]; 
    [UIView transitionWithView:self.buttonFirst 
        duration:1.0 
        options:UIViewAnimationOptionTransitionFlipFromLeft | UIViewAnimationOptionCurveLinear 
       animations:^{ 
        buttonFirst.frame = buttonSecond.frame; 
        buttonFirst.center = buttonSecond.center; 
        } 
       completion:^(BOOL finished){ 
        [self.buttonFirst setUserInteractionEnabled:YES];       
       }]; 
+0

你有没有检查你的代码?它是按照我的问题行事吗? –

+0

在设置'animations'的部分'buttonFirst.frame = buttonSecond.frame;'它会工作。 – LightNight

+0

没有伙计..你能详细解释我..? –

0
CGRect oldRectFirst = buttonFirst.frame; 
CGRect oldRectSecond = buttonSecond.frame; 
[UIView animateWithDuration:0.2f animations:^{ 
    buttonFirst.frame = oldRectSecond; 
    buttonSecond.frame = oldRectFirst; 
}]; 
+0

不工作它交替帧 –

+0

好吧,我想我明白了。现在检查它 – kv0

0

很简单的逻辑

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.20f]; 
[UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[firstButton setFrame:CGRectMake(5, 50, 30, 30)];//moves to second button place 
[secondButton setFrame:CGRectMake(5, 5, 30, 30)];//moves to first button place 
[UIView commitAnimations]; 

相应地使用逻辑您使用..

+0

不工作它替代框架.. –