-2

我正在制作一个非常简单的应用程序,它有一个 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

任何帮助将不胜感激!

先感谢您!

4

3 回答 3

2
imageOne.frame = CGRectMake(100, 31, MIN(90, imageOne.frame.size.width + 5), 28);
于 2014-09-05T22:08:30.347 回答
1

使其在大于或等于 90 的情况下保持在 90 并且不会变得更大。

CGFloat width = imageOne.frame.size.width;
if (width >= 90) {
    width -= (width - 90);
}
imageOne.frame = CGRectMake(100, 31, width, 28);
于 2014-09-05T22:12:24.857 回答
1

一个简单的if声明:

CGFloat width = imageOne.frame.size.width;
if (width <= 85) {
    width += 5;
}
imageOne.frame = CGRectMake(100, 31, width, 28);
于 2014-09-05T21:59:41.857 回答