【问题标题】:iOS: How to limit the width of CGRectMakeiOS:如何限制 CGRectMake 的宽度
【发布时间】:2014-09-05 21:54:49
【问题描述】:

我正在制作一个非常简单的应用程序,它有一个 UIButton 和一个 UIImageView。每次用户点击按钮时,图像宽度都会增加 5。我的问题是,如何限制图像的宽度?可以说,如果它达到 90,则阻止图像增加其宽度。我需要在我的代码中实现什么?

这是我的代码:

文件.h

@interface ViewController : UIViewController
{
     IBOutlet UIButton *buttonOne;
     IBOutlet UIImageView *imageOne;
}

-(IBAction)buttonOne:(id)sender;

@end

文件.m

@implementation ViewController

-(IBAction)buttonOne:(id)sender{

imageOne.frame = CGRectMake(100, 31, imageOne.frame.size.width + 5, 28);

}

@end

任何帮助将不胜感激!

提前谢谢你!

【问题讨论】:

    标签: ios objective-c frame cgrectmake


    【解决方案1】:
    imageOne.frame = CGRectMake(100, 31, MIN(90, imageOne.frame.size.width + 5), 28);
    

    【讨论】:

      【解决方案2】:

      一个简单的if 声明:

      CGFloat width = imageOne.frame.size.width;
      if (width <= 85) {
          width += 5;
      }
      imageOne.frame = CGRectMake(100, 31, width, 28);
      

      【讨论】:

        【解决方案3】:

        让它在大于或等于 90 时保持在 90 并且不会变大。

        CGFloat width = imageOne.frame.size.width;
        if (width >= 90) {
            width -= (width - 90);
        }
        imageOne.frame = CGRectMake(100, 31, width, 28);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多