【问题标题】:iOS: Autosizing not working on UIImageViewiOS:自动调整大小不适用于 UIImageView
【发布时间】:2014-10-20 15:42:18
【问题描述】:

我正在制作一个非常简单的应用程序来学习 Objective-C 和 Xcode。该应用程序有一个 UIButton 和一个 UIImageView。当用户点击按钮时,图像以对角线运动从右到左向下移动,当它到达屏幕中的某个点时,它会重新生成并再次执行相同的操作,如下图所示:

(使用“if 语句”)

当我使用 iPhone Retina(4 英寸)打开 iOS 模拟器时,它运行良好。 问题是当我使用 iPhone Retina(3.5 英寸)打开模拟器时:

它加载了图像,一切看起来都很好,我按下按钮直到图像到达 B 点,但是当它重新生成时,会发生这种情况:

图像向下移动。我不知道为什么会这样。我整天都在寻找答案,但似乎没有任何效果。我没有选中自动布局框并像这样自动调整大小:

代码如下:

文件.h

{
    IBOutlet UIImageView *imageOne;

}

-(IBAction)Button:(id)sender;

@end

文件.m

-(IBAction)Button:(id)sender{    

//ImageOne moving down and reappearing

[UIView animateWithDuration:0.1f
                 animations:^{

                     imageOne.center = CGPointMake(imageOne.center.x -44, imageOne.center.y +41);

                 }];

if (imageOne.center.x < -28) {
    imageOne.center = CGPointMake(368,487);

    }
}

任何帮助将不胜感激

提前谢谢你!!!

【问题讨论】:

  • 没有答案可以帮助我解决该问题中的问题。所以,与其说可能的重复,你会帮我吗?
  • 尝试为你的 UIImageview 添加约束
  • 我没有选中自动布局
  • 我在 Autosizing 中看到您将图像固定在底部。您可以删除它,或者尝试同时使用顶部和底部固定,看看是否有区别?

标签: ios objective-c iphone xcode uiimageview


【解决方案1】:

试试这个:

-(IBAction)Button:(id)sender{    

   CGPoint startingPoint = CGPointMake(startingXPoint, startingYPoint);
   CGPoint destinationPoint = CGPointMake(destinationXPoint, destinationYPoint);
   imageOne.center = startingPoint;



   [UIView animateWithDuration:0.5f  animations:^{

            imageOne.center = destinationPoint;

        } completion:^(BOOL finished){

            imageOne.center = startingPoint;

        }];
}

【讨论】:

  • 不幸的是它没有用......我的问题是,当图像到达 B 点时,我用于自动调整大小的设置不再起作用。
  • 有没有办法设置一个 if 语句: if (is iPhone (3.5 inch)) { imageOne.center = CGPointMake(other_x_value, other_y_value } else { imageOne.center =CGPointMake (368,487) ;
  • 自动调整大小的设置?不明白你的意思。这对我有用,所以你的设置中的某些东西导致了问题