UI基礎(chǔ)的簡單控制

contentMode屬性

帶有scale單詞的:圖片有可能會(huì)拉伸

UIViewContentModeScaleToFill

將圖片拉伸至填充整個(gè)imageView

圖片顯示的尺寸跟imageView的尺寸是一樣的

帶有aspect單詞的:保持圖片原來的寬高比

UIViewContentModeScaleAspectFit

保證剛好能看到圖片的全部

UIViewContentModeScaleAspectFill

拉伸至圖片的寬度或者高度跟imageView一樣

沒有scale單詞的:圖片絕對(duì)不會(huì)被拉伸,保持圖片的原尺寸

UIViewContentModeCenter

UIViewContentModeTop

UIViewContentModeBottom

UIViewContentModeLeft

UIViewContentModeRight

UIViewContentModeTopLeft

UIViewContentModeTopRight

UIViewContentModeBottomLeft

UIViewContentModeBottomRight

小語法點(diǎn)

不能直接修改:OC對(duì)象的結(jié)構(gòu)體屬性的成員

下面的寫法是錯(cuò)誤的

imageView.frame.size= imageView.image.size;

正確寫法

//在OC中對(duì)象結(jié)構(gòu)體成員的修改,可以創(chuàng)建一個(gè)臨時(shí)的結(jié)構(gòu)體來取值CGRecttempFrame = imageView.frame;tempFrame.size= imageView.image.size;imageView.frame= tempFrame;

initWithImage:方法

利用這個(gè)方法創(chuàng)建出來的imageView的尺寸和傳入的圖片尺寸一樣

修改frame的3種方式

直接使用CGRectMake函數(shù)

imageView.frame=CGRectMake(100,100,200,200);

利用臨時(shí)結(jié)構(gòu)體變量

CGRecttempFrame = imageView.frame;tempFrame.origin.x=100;tempFrame.origin.y=100;tempFrame.size.width=200;tempFrame.size.height=200;imageView.frame= tempFrame;

使用大括號(hào){}形式

imageView.frame= (CGRect){{100,100}, {200,200}};

抽取重復(fù)代碼

將相同代碼放到一個(gè)新的方法中

不用的東西就變成方法的參數(shù)

圖片的加載方式

有緩存

UIImage*image = [UIImageimageNamed:@"圖片名"];

使用場合:圖片比較小、使用頻率較高

建議把需要緩存的圖片直接放到Images.xcassets

無緩存

NSString*file = [[NSBundlemainBundle] pathForResource:@"圖片名"ofType:@"圖片的擴(kuò)展名"];UIImage*image = [UIImageimageWithContentsOfFile:@"圖片文件的全路徑"];

使用場合:圖片比較大、使用頻率較小

不需要緩存的圖片不能放在Images.xcassets

放在Images.xcassets里面的圖片,只能通過圖片名去加載圖片

延遲做一些事情

[abc performSelector:@selector(stand:) withObject:@"123"afterDelay:10];// 10s后自動(dòng)調(diào)用abc的stand:方法,并且傳遞@"123"參數(shù)

音頻文件的簡單播放

// 創(chuàng)建一個(gè)音頻文件的URL(URL就是文件路徑對(duì)象)NSURL*url = [[NSBundlemainBundle] URLForResource:@"音頻文件名"withExtension:@"音頻文件的擴(kuò)展名"];// 創(chuàng)建播放器self.player= [AVPlayerplayerWithURL:url];// 播放[self.playerplay];


UIImageView的常見屬性

@property(nonatomic,retain)UIImage*image;顯示的圖片@property(nonatomic,copy)NSArray*animationImages;顯示的動(dòng)畫圖片@property(nonatomic)NSTimeIntervalanimationDuration;動(dòng)畫圖片的持續(xù)時(shí)間@property(nonatomic)NSIntegeranimationRepeatCount;動(dòng)畫的播放次數(shù)(默認(rèn)是0,代表無限播放)

UIImageView的常見方法

-(void)startAnimating//開始動(dòng)畫

-(void)stopAnimating;//停止動(dòng)畫

-(BOOL)isAnimating;//是否正在執(zhí)行動(dòng)畫

UIImage

一個(gè)UIImage對(duì)象代表一張圖片,一般通過imageNamed:方法就可以通過文件名加載項(xiàng)目中的圖片

例子

UIImage*image = [UIImageimageNamed:@"lufy"];

UILabel

設(shè)置顯示多少行,要想顯示所有的行數(shù),設(shè)置為0

label.numberOfLines =2;

@property(nonatomic,copy)NSString*text;顯示的文字@property(nonatomic,retain)UIFont*font;字體@property(nonatomic,retain)UIColor*textColor;文字顏色@property(nonatomic)NSTextAlignmenttextAlignment;對(duì)齊模式(比如左對(duì)齊、居中對(duì)齊、右對(duì)齊)@property(nonatomic)NSIntegernumberOfLines;文字行數(shù)@property(nonatomic)NSLineBreakModelineBreakMode;換行模式

UIFront

UIFont代表字體,常見創(chuàng)建方法有以下幾個(gè):

1系統(tǒng)默認(rèn)字體 + (UIFont *)systemFontOfSize:(CGFloat)fontSize;

2粗體 + (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize;

3斜體 + (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize;

可以不用記,當(dāng)需要時(shí),要學(xué)會(huì)自學(xué),跳到頭文件中,查看就可以,不懂的可以自己動(dòng)手測試出來


UIButton

UIbutton的狀態(tài)

normal(普通狀態(tài))

默認(rèn)情況(Default)

對(duì)應(yīng)的枚舉常量:UIControlStateNormal

highlighted(高亮狀態(tài))

按鈕被按下去的時(shí)候(手指還未松開)

對(duì)應(yīng)的枚舉常量:UIControlStateHighlighted

disabled(失效狀態(tài),不可用狀態(tài))

如果enabled屬性為NO,就是處于disable狀態(tài),代表按鈕不可以被點(diǎn)擊

對(duì)應(yīng)的枚舉常量:UIControlStateDisabled

設(shè)置按鈕在不同狀態(tài)的背景圖片

按鈕的樣式

UIButton的常見設(shè)置

- (void)setTitle:(NSString *)titleforState:(UIControlState)state;設(shè)置按鈕的文字

- (void)setTitleColor:(UIColor *)colorforState:(UIControlState)state;設(shè)置按鈕的文字顏色

- (void)setImage:(UIImage *)imageforState:(UIControlState)state;設(shè)置按鈕內(nèi)部的小圖片

- (void)setBackgroundImage:(UIImage *)imageforState:(UIControlState)state;設(shè)置按鈕的背景圖片

btn.titleLabel.font = [UIFont systemFontOfSize:13];設(shè)置按鈕的文字字體(需要拿到按鈕內(nèi)部的label來設(shè)置)

- (NSString *)titleForState:(UIControlState)state;獲得按鈕的文字

- (UIColor *)titleColorForState:(UIControlState)state;獲得按鈕的文字顏色

- (UIImage *)imageForState:(UIControlState)state;獲得按鈕內(nèi)部的小圖片

- (UIImage *)backgroundImageForState:(UIControlState)state;獲得按鈕的背景圖片

choose for the UIImageView and UILabel and UIButton

UIButton、UIImageView、UILabel的選擇


僅僅是顯示數(shù)據(jù),不需要點(diǎn)擊

建議選擇UIImageView、UILabel

不僅顯示數(shù)據(jù),還需要監(jiān)聽點(diǎn)擊

建議選擇UIButton

其實(shí)UIImageView、UILabel也可以通過手勢識(shí)別器來監(jiān)聽

長按控件后,會(huì)改變顯示的內(nèi)容

不用考慮了,選擇UIButton(因?yàn)閁IButton有highlighted這種狀態(tài))

同時(shí)顯示2張圖片:背景圖片、內(nèi)容圖片

不用考慮了,選擇UIButton

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一個(gè)UIButton的實(shí)例變量, 使一個(gè)按鈕(button)在觸摸屏上生效。一個(gè)按鈕監(jiān)聽觸摸事件,當(dāng)被點(diǎn)擊時(shí),給目...
    wushuputi閱讀 1,654評(píng)論 0 1
  • 前言:UI控件整理之UIButton 一、顯示圖片(復(fù)選框) UIButton *button = [UIButt...
    心如止水的魚閱讀 354評(píng)論 0 0
  • 一、UIView常見屬性 1.frame 位置和尺寸(以父控件的左上角為原點(diǎn)(0,0)) 2.center 中點(diǎn)(...
    脫下國際籃化身程序猿閱讀 359評(píng)論 0 0
  • 2017年的夏天,炎熱而又茫然的躺在沙發(fā)里,心想著充滿未知的未來,聽著電視里傳來“敢問路在何方”的旋律。 都說我們...
    V加加布魯根閱讀 325評(píng)論 0 0
  • 穿行十月的風(fēng)雨 □曾曉琴 越過十月風(fēng)雨 有誰相伴 經(jīng)年累月的路途 那 風(fēng)雨被十月的目光…… 打量近處與遠(yuǎn)方 困...
    向前的冰山來客15669閱讀 297評(píng)論 0 6

友情鏈接更多精彩內(nèi)容