【问题标题】:UIView Animation using Frame only changes position and does not change size iOSUIView Animation 使用 Frame 只改变位置,不改变大小 iOS
【发布时间】:2015-07-06 20:50:28
【问题描述】:

所以下面的代码将 self.nowGamePiece 移动到棋盘的中心,但它并没有将大小更改为 500 x 500。事实上,无论这些值(CGRectMake 中的宽度和高度)是什么,此动画中的大小不会改变。

为什么? self.nowGamePiece 也应该将其大小更改为 500x 500!

//self.nowGamePiece is a UIView
    [UIView animateWithDuration:7 animations:^{
         self.nowGamePiece.frame = CGRectMake(self.gameBoardImageView.center.x,self.gameBoardImageView.center.y, 500, 500);
         //doing below only moves the self.nowGamePiece too. Size doesn't change
       //self.nowGamePiece.frame = CGRectMake(100, 100, 500, 500);
        } completion:^(BOOL finished){
            return;
        }];

【问题讨论】:

    标签: ios animation uiview frame


    【解决方案1】:

    我认为这种方法可能有问题。我改用变换解决了它。

    [UIView animateWithDuration:7 animations:^{
        self.nowGamePiece.transform =CGAffineTransformMakeScale(7.0, 7.0);
        self.nowGamePiece.center = CGPointMake(self.gameBoardImageView.center.x, self.gameBoardImageView.center.y);
    } completion:^(BOOL finished){
        return;
    }];
    

    【讨论】:

      【解决方案2】:

      尝试设置视图的 autoResizingMask 属性。我怀疑你的图像是你 nowGamePiece 视图的子视图,当超级视图的框架发生变化时,它只是不调整图像的大小。

      self.nowGamePiece.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
      self.nowGamePiece.autoresizesSubviews = YES;
      

      【讨论】:

      • 图像不是 self.gamePiece 的子视图。图像只是 self.gamePiece 所在的同一 uiview 中的图像。使用您的解决方案,我仍然得到相同的结果。仍然只是位置变化,大小没有变化。
      • 忘掉self.gameBoardImageView吧。如果我这样做 self.nowGamePiece.frame = CGRectMake(100, 100, 500, 500); self.nowGamePiece 移动到 100,100,但大小没有改变。
      • 你的意思是框架实际上并没有变大?我以为你的意思是它明显不能扩展。没有理由有一个空的完成块,你应该把它改成nil。我不认为这是相关的,但.. 只是一个旁注。
      • 另外,为了验证它应该可以工作,我使用了您的代码,与任何其他代码隔离,并简单地使用背景颜色来查看视图更改。事实上它确实改变了框架。你的问题必须是别的东西,这就是为什么我一直在问你关于事情是如何设置的问题。我注意到您在下面回答了您自己的问题 - 我无法发表评论,所以我会在这里问您:在应用此动画之前您是否已经使用过变换?
      • 是的,它们都发生了变化。这是一个视频:youtube.com/watch?v=JKzIjjH92mE 除了您在此处发布的代码之外,肯定还有其他事情发生。
      【解决方案3】:

      试试这个:

      //self.nowGamePiece is a UIView
      [UIView animateWithDuration:7 animations:^{
      
      CGRect frame = self.nowGamePiece.frame;
      frame = CGRectMake(self.gameBoardImageView.center.x, self.gameBoardImageView.center.y, 500, 500);
      self.nowGamePiece.frame = frame;   
      
      } completion:^(BOOL finished){
          return;
      }];
      

      【讨论】:

      • 还是一样的结果。它只会将 self.nowGamePiece 移动到棋盘的中心,并且大小不会改变。我的意思是你的代码和我的基本一样。没有区别。
      • 你在使用自动布局吗?
      猜你喜欢
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 2022-07-22
      • 2015-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多