iOS開發:小技巧積累

來源:互聯網
上載者:User

標籤:style   http   io   ar   os   使用   sp   for   檔案   

1、擷取全域的Delegate對象,這樣我們可以調用這個對象裡的方法和變數:

[(MyAppDelegate*)[[UIApplication sharedApplication] delegate] MyMethodOrMyVariable];

2、獲得程式的主Bundle:

NSBundle *bundle = [NSBundle mainBundle];

Bundle可以理解成一種檔案夾,其內容遵循特定的架構。

Main Bundle一種主要用途是使用程式中的資源檔,片、聲音、plst檔案等。

NSURL *plistURL = [bundle URLForResource:@"plistFile" withExtension:@"plist"];

上面的代碼獲得plistFile.plist檔案的路徑。

3、在程式中播放聲音:

首先在程式添加AudioToolbox:

其次,在有播放聲音方法的.m方法添加#import:

#import

接下來,播放聲音的代碼如下:

NSString *path = [[NSBundle mainBundle] pathForResource:@"soundFileName" ofType:@"wav"]; 

SystemSoundID soundID; 

AudioServicesCreateSystemSoundID ((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID); 

AudioServicesPlaySystemSound (soundID); 

4、設定和擷取類中屬性值:

[self setValue: 變數值 forKey: 變數名];

[self valueForKey: 變數名];

5、讓某一方法在未來某段時間之後執行:

[self performSelector:@selector(方法名) withObject:nil afterDelay:延遲時間(s)]; 

6、獲得裝置版本號碼:

float version = [[[UIDevice currentDevice] systemVersion] floatValue];

7、捕捉程式關閉或者進入後台事件:

UIApplication *app = [UIApplication sharedApplication];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];

applicationWillResignActive:這個方法中添加想要的操作

8、查看裝置支援的字型:

for (NSString *family in [UIFont familyNames]) {

    NSLog(@"%@", family);

    for (NSString *font in [UIFont fontNamesForFamilyName:family]) {

        NSLog(@"\t%@", font);

    }

}

9、為UIImageView添加單擊事件:

imageView.userInteractionEnabled = YES;

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];

[imageView addGestureRecognizer:singleTap];

10、添加多語言支援: 比如Image Picker這樣的組件,它上面的按鈕的文字是隨著裝置語言環境的改變而改變的,但是要先在工程添加語言:

11、使程式支援iTunes這樣的裝置,比如可以使用PC端的工具往程式的Documents中拖放檔案。

12、頁面轉場效果設定:

controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

[self presentModalViewController:controller animated:YES];

可供使用的效果:

UIModalTransitionStyleCoverVertical

UIModalTransitionStyleFlipHorizontal

UIModalTransitionStyleCrossDissolve

UIModalTransitionStylePartialCurl

恢複之前的頁面:

[self dismissModalViewControllerAnimated:YES];

iOS開發:小技巧積累

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.