Mais conteúdo relacionado
Semelhante a I os 02 (20)
I os 02
- 2. 課程⼤大綱
• 另⼀一種換圖的⽅方式
• QV003:多個按鈕,按下後顯⽰示不同的圖
• 學習⽂文字的顯⽰示 (UILabel)
• QV002:按鈕顯⽰示不同的⽂文字
• Property & Synthesize
• 由 UILable 查詢資料的查閱⽅方法
• QV058:數種顯⽰示資料的練習
• 隨機亂數。資料運算
• QV057:九九乘法 CAI 練習
- 28. 可以由程式做的設定
• text 改變⽂文字內容
• font ⽂文字字型
• color ⽂文字顏⾊色
• textAlignment 對⿑齊⽅方式
• enabled 有作⽤用或失效
• .................
• ⽂文字⼤大⼩小的調整
• ⾼高亮突出 (highlightedTextColor, highlighted)
• 影⼦子 (shadowColor, shadowOffset)
- 30. ⽂文字的基本屬性
- (void)viewDidLoad
{
[super viewDidLoad];
myLabel.text = @"Hello";
myLabel.font = [UIFont fontWithName:@"Arial" size:80.0];
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.textColor = [UIColor yellowColor];
myLabel.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:0.8];
myLabel.enabled = YES;
}
- 33. 想想⼀一些⽐比較像 APP 的變化
• 顯⽰示不同的⽂文字 ......
• 顯⽰示按了幾次按鍵 ......
• 顯⽰示丟骰⼦子的點數 ......
• 顯⽰示現在的時間 ......
• ............(發揮想像⼒力)............
- 35. ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
IBOutlet UILabel *myLabel1;
IBOutlet UILabel *myLabel2;
IBOutlet UILabel *myLabel3;
IBOutlet UILabel *myLabel4;
IBOutlet UIImageView *ballImageView;
int cnt;
}
- (IBAction) btn1Tap:(id)sender;
- (IBAction) btn2Tap:(id)sender;
- (IBAction) btn3Tap:(id)sender;
- (IBAction) btn4Tap:(id)sender;
@end
- 38. ViewController.m (2/4)
- (IBAction) btn2Tap:(id)sender
{
cnt++;
myLabel2.text = [NSString stringWithFormat:@"你點了 %d 次", cnt];
}
- (void)viewDidLoad
{
[super viewDidLoad];
cnt = 0;
}
- 39. ViewController.m (3/4)
- (IBAction) btn3Tap:(id)sender
{
int num = 1 + arc4random() % 6;
myLabel3.text = [NSString stringWithFormat:@"骰⼦子扔出 %d 點", num];
NSString *imageString = [[NSString alloc]
initWithFormat:@"Circle%d.png", num];
ballImageView.image = [UIImage imageNamed:imageString];
}
- 40. ViewController.m (4/4)
- (IBAction) btn4Tap:(id)sender
{
// ⽅方法⼀一
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
NSString *nowString = [dateFormat stringFromDate:now];
注意:不能同時存在 (因為⽤用相同的名稱)
// ⽅方法⼆二
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
NSString *nowString = [dateFormat stringFromDate:[NSDate date]];
// 顯⽰示⽇日期
myLabel4.text = [NSString stringWithFormat:@"%@", nowString];
}
- 41. 隨機亂數
• 產⽣生範圍在 0 到 2^32-1 ( 4294967295) 之
間的整數亂數
• 無需設定亂數種⼦子
• 範圍為傳統 rand() 及 random() 的兩倍
• ⼀一般使⽤用 % (整數餘數) 取得所要的範圍
- 42. 隨機亂數使⽤用
• 取得 0~8 之間的整數亂數
int value = arc4random() % 9;
• 取得 1~6 之間的整數亂數
int value = (arc4random() % 6) + 1;
• 取得 0~100 之間的浮點數亂數
#define ARC4RANDOM_MAX 0x100000000
(double) value = floorf(((double)arc4random() / ARC4RANDOM_MAX) * 100.0f);
- 47. ViewController.m (3/3)
- (IBAction) ShowQuestion:(id)sender
{
num1 = 1 + arc4random() % 9;
num2 = 1 + arc4random() % 9;
numLabel1.text = [NSString stringWithFormat:@"%d", num1];
numLabel2.text = [NSString stringWithFormat:@"%d", num2];
answerLabel.hidden = YES;
}
- (IBAction) ShowAnswer:(id)sender
{
int answer = num1 * num2;
answerLabel.text = [NSString stringWithFormat:@"%d", answer];
answerLabel.hidden = NO;
}
- 49. - (IBAction) ShowQuestion:(id)sender
{
*** 省略部份程式碼 ***
answerLabel.hidden = YES; 增加畫⾯面上物件的
}
顯⽰示及隱藏
- (IBAction) ShowAnswer:(id)sender
{
*** 省略部份程式碼 ***
answerLabel.hidden = NO;
}
- (void) initial
{
*** 省略部份程式碼 ***
num1 = 3;
num2 = 7;
answerLabel.hidden = YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
!
[self initial];
}
- 51. 延伸學習
• 查詢學過的 UI 元件
(UILabel, UIButton, UIImageView, UIAlert...)
還有哪些屬性或⽅方法可以使⽤用