2014-09-05 37 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);