【问题标题】:Animate move and scale of image UIButton图像 UIButton 的动画移动和缩放
【发布时间】:2010-04-12 04:40:20
【问题描述】:

我希望它是动画的,所以我使用[UIView beginAnimations]

我尝试过使用button.imageView.frame(图像设置为imageView)制作动画,但它只对位置进行动画处理,图像根本不会按比例缩小。

使用hateButton.frame(当图像设置为backgroundImage 时),backgroundImage 会按比例缩小,但不是动画。

如何对 UIButton 图像进行动画缩放?

【问题讨论】:

    标签: iphone uiimageview uiimage uibutton


    【解决方案1】:

    您可以在[UIView beginAnimations][UIView commintAnimations][UIView animateWithDuration] 之间调用layoutIfNeeded

    [UIView animateWithDuration:0.3f animations:^{
        self.myButton.frame = aFrame;    
        [self.myButton layoutIfNeeded];
     }];
    

    【讨论】:

      【解决方案2】:

      我可以使用 Quartz 2D 实现这一点:

      // move anchor point without moving frame
      CGRect oldFrame = hateButton.frame;
      hateButton.layer.anchorPoint = CGPointMake(0, 1); // bottom left
      hateButton.frame = oldFrame;
      
      [UIView beginAnimations:nil context:NULL];  
      CGAffineTransform newTransform;
      newTransform = CGAffineTransformMakeScale(0.8, 0.8);
      hateButton.transform = CGAffineTransformTranslate(newTransform, 0, -160);
      [UIView commitAnimations];
      

      确保你也导入 Quartz2D:

      #import <QuartzCore/QuartzCore.h>
      

      【讨论】:

      • 您可以通过将 button.imageView.transform 设置为 CGAffineTransform 来将转换直接应用于 button.imageView。
      • 我决定使用 UIImageView 代替,因此可以使用普通的动画框架。然后在顶部我放了一个不可见的 UIButton。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多