常見控件宣告:
IBOutlet UIImageView *viewImg; // UIImageView IBOutlet UIActivityIndicatorView *activity; // 指示器 IBOutlet UIProgressView *progress; // 進度條 IBOutlet UISwitch *sw; // 開關 IBOutlet UISegmentedControl *segment; // 選項卡 IBOutlet UISlider *slider; // 滑桿
在 viewDidLoad 中可設定背景、建立畫面、以 NSTimer 定時觸發進度更新,並讓指示器開始旋轉:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
// 定時器:每隔一段時間呼叫 progressChange
timer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(progressChange) userInfo:@"進度測試" repeats:YES];
[activity startAnimating];
}
圖片拉伸:
UIImage *image = [UIImage imageNamed:@"Email_04.png"]; UIImage *newImg = [image stretchableImageWithLeftCapWidth:20 topCapHeight:20]; viewImg.image = newImg;
UISwitch 事件:
-(IBAction)switchChange:(id)sender {
UISwitch *swi = (UISwitch *)sender;
if (swi.on) {
[self progressChange];
[activity startAnimating];
} else {
[activity stopAnimating];
}
}
UISegmentedControl、UISlider 與收起鍵盤:
-(IBAction)segmentChange:(id)sender {
segment = (UISegmentedControl *)sender;
NSLog(@"%d", segment.selectedSegmentIndex);
}
-(IBAction)sliderChange:(id)sender {
slider = (UISlider *)sender;
progress.progress = slider.value;
}
-(IBAction)textFieldCancel {
[userNameTextFiled resignFirstResponder];
[passWordTextFiled resignFirstResponder];
}
UITabBarController 的使用:
LoginButtonViewController *one = [[LoginButtonViewController alloc] initWithNibName:@"LoginButtonViewController" bundle:nil]; one.tabBarItem.title = @"視圖一"; one.tabBarItem.image = [UIImage imageNamed:@"Hein22.jpg"]; TwoViewController *two = [[TwoViewController alloc] initWithNibName:@"TwoViewController" bundle:nil]; two.tabBarItem.title = @"視圖二"; two.tabBarItem.image = [UIImage imageNamed:@"21.png"]; UITabBarController *bar = [[UITabBarController alloc] init]; bar.viewControllers = [[NSArray alloc] initWithObjects:one, two, nil]; [apps.window addSubview:bar.view];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 隱藏狀態欄
[[UIApplication sharedApplication] setStatusBarHidden:YES];
// 手動建立並將主視窗設為全螢幕
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
root = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
[window addSubview:root.view];
[window makeKeyAndVisible];
return YES;
}
另一種做法是在 XXX-info.plist 中新增 Status bar is initially hidden 屬性並勾選即可。
// 延時執行
[self performSelector:@selector(fade) withObject:self afterDelay:2];
-(void)fade {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationDidStopSelector:@selector(gotoLoginView)];
self.view.alpha = 0.3;
[UIView commitAnimations];
}
-(void)gotoLoginView {
LoginAppDelegate *app = (LoginAppDelegate *)([[UIApplication sharedApplication] delegate]);
LoginViewController *loginView = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:app.window cache:YES];
[app.window addSubview:loginView.view];
[self.view removeFromSuperview];
[UIView commitAnimations];
}
動畫效果必須以 window 為基礎,切換 view 時記得把原視圖從父視窗移除。