隱藏狀態列有兩種層次的做法。程式碼方式只是讓它「看不見」,佔用的 20pt 空間仍然存在:
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
若要連版面一起重排,則該改 Info.plist,新增一個 UIStatusBarHidden 鍵並把型別設為 Boolean,重新編譯後才會生效。切換方向同理:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
三者效果完全不同,選錯就會出現「按鈕明明在畫面上卻點不動」的困惑。
int value = [string integerValue]; // 字串轉整數 NSString *s = [NSString stringWithFormat:@"%d", value]; // 整數轉字串
[self.tableView reloadData];
for (UIView *oneView in cell.contentView.subviews) {
if ([oneView isMemberOfClass:[UITextField class]]) {
textField = (UITextField *)oneView;
}
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:kTransitionDuration];
[UIView setAnimationTransition:([self.mainView superview] ?
UIViewAnimationTransitionCurlUp : UIViewAnimationTransitionCurlDown)
forView:self.containerView cache:YES];
if ([self.flipToView superview]) {
[self.flipToView removeFromSuperview];
[self.containerView addSubview:self.mainView];
} else {
[self.mainView removeFromSuperview];
[self.containerView addSubview:self.flipToView];
}
[UIView commitAnimations];
這是一維的翻頁效果,若想自訂旋轉軸心,透過圖層的 anchorPoint 設定即可:
imageView.layer.anchorPoint = CGPointMake(0.5, 1.0);
[self.navigationController setNavigationBarHidden:YES animated:YES];
NSString *mapUrl = [NSString stringWithFormat:@"https://maps.google.com/maps?q=%f,%f",
loc.latitude, loc.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mapUrl]];
NSDateFormatter *format = [[NSDateFormatter alloc] init]; [format setDateFormat:@"hh:mm:ss"]; NSString *str = [format stringFromDate:[NSDate date]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(xxx:) name:UIApplicationWillTerminateNotification object:nil];
長時間的背景音樂用 AVAudioPlayer,記得把 numberOfLoops 設為 -1 才能無限循環:
myBackMusic.numberOfLoops = -1; [myBackMusic prepareToPlay]; [myBackMusic setVolume:1]; [myBackMusic play];
短音效(例如按鈕點擊聲)則用 AudioServices 這組 C API, overhead 更低:
AudioServicesCreateSystemSoundID(theSoundURL, &sameViewSoundID); AudioServicesPlaySystemSound(sameViewSoundID);
label.lineBreakMode = UILineBreakModeWordWrap; label.numberOfLines = 0; // 0 代表不限行數
以上都是 iOS 早期(Objective-C、MRC 時代)的寫法,在現代專案中多已改為 Auto Layout、AVAudioSession 與 blocks 動畫,但底層概念與思路完全相通,理解它們有助於看懂老專案的程式碼。