【问题标题】:Animation on UIButton Click in iPhoneiPhone中的UIButton点击动画
【发布时间】:2011-09-27 11:37:28
【问题描述】:

大家.. 如何设置按钮点击动画?

我有两个按钮,都有不同的框架..

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

如何制作动画?

编辑:

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

请告诉我.. 谢谢...

【问题讨论】:

    标签: iphone objective-c animation core-animation


    【解决方案1】:

    非常简单的逻辑

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

    根据您的用途使用逻辑..

    【讨论】:

      【解决方案2】:

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

      [UIView beginAnimation/endAnimation] bock
      

      【讨论】:

      • 是的,还有.. 这是代码.. UIImageView *imageView = [[UIImageView alloc] init]; [imageView setFrame:button.frame]; [imageView setImage:buttonS.currentBackgroundImage]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration: 2.0]; [imageView setFrame:buttonS.frame]; [UIView 提交动画]; [imageView removeFromSuperview];但没用..
      【解决方案3】:
      CGRect oldRectFirst = buttonFirst.frame;
      CGRect oldRectSecond = buttonSecond.frame;
      [UIView animateWithDuration:0.2f animations:^{
          buttonFirst.frame = oldRectSecond;
          buttonSecond.frame = oldRectFirst;
      }];
      

      【讨论】:

      • 好的,我想我明白了。立即查看
      【解决方案4】:

      这是工作程序的示例:

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

      【讨论】:

      • animations 部分设置buttonFirst.frame = buttonSecond.frame; 并且它将起作用。
      • 我刚刚编辑了代码,这一定是有效的。在options你可以设置动画选项。
      【解决方案5】:

      我终于得到了答案……

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

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

      现在它的工作......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多