2014-09-05 106 views
-2

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

这里是我的代码:

File.h

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

-(IBAction)buttonOne:(id)sender; 

@end 

File.m

@implementation ViewController 

-(IBAction)buttonOne:(id)sender{ 

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

} 

@end 

任何帮助将不胜感激!

预先感谢您!

回答

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

谢谢你的男人!有用! – 2014-09-05 22:13:24

1

一个简单的if声明:

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

为了让这个如果是大于或等于90比将保持在90,并没有得到任何更大。

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