標籤:
一、iOS9中UIAlertController的簡單使用
很明顯,簡單的UIAlertView已經不能用了,我感覺很傷心。
// 建立
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"開始了" message:@"開始了!" preferredStyle:UIAlertControllerStyleActionSheet];
// UIAlertControllerStyleActionSheet 是顯示在螢幕底部
// UIAlertControllerStyleAlert 是顯示在中間
// 設定按鈕
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *defult = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];
// UIAlertAction *destructive = [UIAlertAction actionWithTitle:@"destructive" style:UIAlertActionStyleDestructive handler:nil];
// 添加按鈕
[alert addAction:cancel];
[alert addAction:defult];
// [alert addAction:destructive];
//顯示
[self presentViewController:alert animated:YES completion:nil];
——----——————// 複雜的,添加TextField,並監聽。
註:文本輸入框只能添加到Alert的風格中,ActionSheet是不允許的
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"登陸";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"密碼";
textField.secureTextEntry = YES;
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"添加監聽代碼";
// 要設定UITextFieldText的代理
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
}];
[self presentViewController:alert animated:YES completion:nil];
// 監聽方法實現
- (void)alertTextFieldTextDidChange:(NSNotification *)notification
{
UIAlertController *alert = (UIAlertController *)self.presentedViewController;
if (alert) {
// 下標為2的是最後一個,添加了監聽的 alert.textFields[2]
UITextField *lisen = alert.textFields[2];
// 限制輸出長度,超過6個則不允許點擊確認鍵
// 超過6個按鈕變灰色 enabled = NO;
UIAlertAction *action = alert.actions.lastObject;
action.enabled = lisen.text.length <= 6;
}
}
效果是這樣的!
二、NSTimer
NSTimer準確嗎?如果不準備,怎麼辦?
不準確。通常用來有一定時間跨度的周期性時間的處理!
處理Timer可以用多線程,在遊戲中多用CADisplayLink。
/**
參數說明
1. 時間間隔,double
2. 監聽時鐘觸發的對象
3. 調用方法
4. userInfo,可以是任意對象,通常傳遞nil
5. repeats:是否重複
*/
// scheduledTimerWithTimeInterval 方法本質上就是建立一個時鐘,
// 添加到運行迴圈的模式是DefaultRunLoopMode
// ----------------------------------------------
// 1>
// self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:@"hello timer" repeats:YES];
// ----------------------------------------------
// 2> 與1等價
// self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
// // 將timer添加到運行迴圈
// // 模式:預設的運行迴圈模式
// [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
// ----------------------------------------------
// 3>
self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
// 將timer添加到運行迴圈
// 模式:NSRunLoopCommonModes的運行迴圈模式(監聽滾動模式)
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
// 停止時鐘,invalidate是唯一停止時鐘的方法
// 一旦調用了invalidate方法,timer就無效了,如果再次啟動時鐘,需要重新執行個體化
[self.timer invalidate];
三、ScrollView
/**
放大縮小
1> 設定代理
2> 指定最大/最小的縮放比例
*/
// 映像的setter
- (void)setImage:(UIImage *)image
{
_image = image;
// 設定映像視圖的內容
self.imageView.image = image;
// 讓映像視圖根據映像自動調整大小
[self.imageView sizeToFit];
// 告訴scrollView內部內容的實際大小
self.scrollView.contentSize = image.size;
}
/**
在getter方法中
* 如果是屬性自身的,使用_成員變數
* 如果是其他屬性,使用self. getter方法,從而可以保證如果該對象沒有被執行個體化,能夠及時的被建立並載入
*/
- (UIImageView *)imageView
{
if (_imageView == nil) {
_imageView = [[UIImageView alloc] init];
[self.scrollView addSubview:_imageView];
}
return _imageView;
}
- (UIScrollView *)scrollView
{
if (_scrollView == nil) {
_scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
// 設定屬性
// 設定邊距
_scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
// 不顯示水平滾動標示
_scrollView.showsHorizontalScrollIndicator = NO;
// 不顯示垂直滾動標示
_scrollView.showsVerticalScrollIndicator = NO;
// *** 位移位置
_scrollView.contentOffset = CGPointMake(0, 0);
// 取消彈簧效果,內容固定,不希望出現彈簧效果時
// 不要跟bounds屬性搞混了
_scrollView.bounces = NO;
// 設定代理
_scrollView.delegate = self;
// 設定最大/最小縮放比例
_scrollView.maximumZoomScale = 2.0;
_scrollView.minimumZoomScale = 0.2;
[self.view addSubview:_scrollView];
}
return _scrollView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 設定映像
self.image = [UIImage imageNamed:@"minion"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
btn.center = self.view.center;
[self.view addSubview:btn];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
}
- (void)click
{
// 移動大圖的位移位置
CGPoint offset = self.scrollView.contentOffset;
offset.x += 20;
offset.y += 20;
// 注意:設定contentOffset會忽略contentSize
self.scrollView.contentOffset = offset;
}
#pragma mark - UIScrollView的代理方法
/**
1> 設定了代理
2> 指定了最大、最小的縮放比例
表示ScrollView是可以縮放的
代理方法的"傳回值"實際上就是控制器告訴滾動視圖,要縮放的是UIImageView
*/
// 告訴ScrollView要縮放的視圖是誰,具體的縮放實現,是由ScrollView來完成的
// 1> scrollView要知道縮放誰
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
// 2> 滾動視圖即將開始縮放,通常不需要寫
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
NSLog(@"%s", __func__);
}
// 3> 正在縮放,通常也不需要實現
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
// NSLog(@"%s", __func__);
NSLog(@"%@", NSStringFromCGAffineTransform(self.imageView.transform));
}
// 4> 完成縮放,通常也不需要實現
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
NSLog(@"%s", __func__);
}
________________________________________________________________________________________
運行迴圈示範
int main(int argc, const char * argv[])
{
@autoreleasepool {
int selection = -1;
while (YES) {
printf("請輸入選擇,0表示退出:");
scanf("%d", &selection);
if (selection == 0) {
printf("歡迎下次再來!88\n");
break;
} else {
printf("您選擇了第 %d 項功能\n", selection);
}
}
}
return 0;
}
2015/10/3 iOS 筆記 細節 iOS9中UIAlertController的簡單使用 ScrollView NSTimer