通遼做網(wǎng)站通過seo來賺錢百度seo培訓
UI學習
- 一:UIView基礎(chǔ)
- frame屬性
- 隱藏視圖對象:
- UIView的層級關(guān)系
- 二:UIWindow對象
- 三:UIViewController基礎(chǔ)
- UIViewController使用
- 四:定時器與視圖移動
- 五:UISwitch控件
- 六:滑動條和進度條
- 七:步進器和分欄控件
- 八:警告對話框和等待提示器
- 九:UITextField
- 滾動視圖
- 滾動視圖基礎(chǔ)
- 滾動視圖高級
- 總結(jié)
一:UIView基礎(chǔ)
我們先來介紹一下frame屬性
frame屬性
frame框架可以理解為視圖在父視圖中的位置和大小的描述。具體來說,frame框架包括視圖的原點坐標和寬高兩個屬性。
- 原點坐標(origin):原點坐標表示視圖在父視圖坐標系統(tǒng)中的位置,通常是相對于父視圖左上角的距離。原點坐標是一個CGPoint對象,包括x和y兩個屬性。
- 寬高(size):寬高表示視圖在父視圖中的大小,通常是視圖的寬度和高度。寬高是一個CGSize對象,包括width和height兩個屬性
而CGRectMake是一個用來創(chuàng)建CGRect結(jié)構(gòu)體的函數(shù),該函數(shù)傳入四個參數(shù),分別為原點坐標的x和y,寬高的width和height。
簡而言之,我們對frame屬性初始化,傳入前兩個參數(shù)確定開始位置,傳入后兩個參數(shù)作為視圖的大小。這也是其他控件的基本屬性。
UIview是iOS的視圖對象,是顯示在屏幕上的所有對象的基礎(chǔ)類,
也就是說,所有顯示在屏幕上的對象一定都繼承與UIView,屏幕上所有能看到的對象都是UIView的子類
- UIView是一個矩形對象,有背景顏色,可以顯示,有層級關(guān)系
- frame屬性是UIView的基本屬性,而UiView是其他對象的父類,因此所有對象都有該屬性。
下面使用代碼來展示UIView的基礎(chǔ)內(nèi)容:
- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.//初始化一個UIview對象UIView *view = [[UIView alloc] init];//設(shè)置UIview的位置view.frame = CGRectMake(100, 100, 100, 200);view.backgroundColor = [UIColor orangeColor];//將新建的視圖添加到父親視圖上//1:將新建的視圖顯示到屏幕上//2:將視圖作為父親視圖的子視圖管理起來[self.view addSubview:view];//將自己從父親視圖刪除掉//1.從父親視圖的管理中刪除//2.不會顯示在屏幕。//[view removeFromSuperview] ;
}
效果:
隱藏視圖對象:
分別有三種方法:
- 設(shè)置hidde屬性:是否隱藏
- 設(shè)置alpha屬性:設(shè)置透明度
- 設(shè)置opaque屬性:是否顯示不透明
//是否隱藏視圖對象//YES:不顯示//NO:顯示,默認值為NOview.hidden = YES;//設(shè)置視圖的透明度//alpha= 1:不透明//a = 0 :透明//a = 0.5: 半透明view.alpha = 1;//設(shè)置是否顯示不透明view.opaque = YES;
UIView的層級關(guān)系
UIview是在父視圖中可以用self.view.subviews[]的數(shù)組下標來查看,而這個數(shù)組是有順序的,即層級關(guān)系。
- 先加入的視圖,在數(shù)組的開始,即在屏幕的最底層。
- 重復(fù)加入,不會開辟新的數(shù)組位置,而是移動數(shù)組的排序方式。
- 可以使用bringSubviewToFront、sendSubviewToBack等方法來改變視圖的順序。
- removeFromSuperview方法可以刪除子視圖。
- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.UIView *view1 = [[UIView alloc] init];view1.frame = CGRectMake(100, 100, 150, 150);view1.backgroundColor = [UIColor orangeColor];UIView *view2 = [[UIView alloc] init];view2.frame = CGRectMake(125, 125, 150, 150);view2.backgroundColor = [UIColor blueColor];UIView *view3 =[[UIView alloc] init];view3.frame = CGRectMake(150, 150, 150, 150);view3.backgroundColor = [UIColor blackColor];//將三個視圖對象顯示到屏幕上并且添加到父親視圖上。//重復(fù)加入已有元素,不會擴大數(shù)組,而是移動數(shù)組[self.view addSubview:view1];[self.view addSubview:view2];[self.view addSubview:view3];[self.view addSubview:view1];//[self.view addSubview:view2];//將某一個視圖調(diào)整到最前面顯示//參數(shù):UIView對象,調(diào)整到一個視圖到最前方//實際上也移動了數(shù)組的位置。// [self.view bringSubviewToFront:view1];//將某一個視圖調(diào)整到最后顯示//參數(shù):UIView對象,調(diào)整哪一個視圖到最后//實際上也移動了數(shù)組的位置// [self.view sendSubviewToBack:view3];UIView *viewFront = self.view.subviews[0];// [view1 removeFromSuperview];UIView *viewBack = self.view.subviews[2];if (viewFront == view2) {NSLog(@"等于");}if (viewBack == view1) {NSLog(@"等于");}
}
效果:
同時,我們可以驗證重復(fù)添加視圖,是移動數(shù)組位置而不是創(chuàng)建新的數(shù)組。
二:UIWindow對象
注意:在XCode13之后我們使用代碼來創(chuàng)建初始化一個UIWindow對象不在AppDelegate類中進行,而是在SceneDelegate類進行,并且不需要創(chuàng)建UIWindow對象,只需要為其初始化視圖管理器后即可進行使用。
UIWindow對象是UIView最頂層的容器,包含應(yīng)用并顯示所有的UIView對象。同時,也可以反映傳遞事件給UIView。
下面是UIWindow的一些基本使用。
SceneDelegate.m文件
#import "SceneDelegate.h"@interface SceneDelegate ()@end@implementation SceneDelegate- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {//對UIWindow對象創(chuàng)建視圖控制器self.window.rootViewController = [[UIViewController alloc] init] ;//設(shè)置背景顏色self.window.backgroundColor = [UIColor blackColor];//直接給window上添加視圖UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];view1.backgroundColor = [UIColor blueColor];UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(150, 0, 50, 200)];view2.backgroundColor = [UIColor redColor];//當創(chuàng)建一個新的背景視圖,然后將這個視圖作為window的子視圖,再讓view1作為背景視圖的子視圖,就會有一個層級關(guān)系//當移動背景視圖的時候,view1視圖也會隨著移動,子視圖是參照父視圖的坐標系UIView *backview = [[UIView alloc] initWithFrame:CGRectMake(100, 400, 200, 200)];backview.backgroundColor = [UIColor whiteColor];//將子視圖添加到爸爸視圖上[backview addSubview:view1];[backview addSubview:view2];[self.window addSubview:backview];NSLog(@"%@", view1.window);NSLog(@"%@", backview.window);NSLog(@"%@", self.window);//使window有效并顯示到屏幕上[self.window makeKeyAndVisible];}
效果:
同時,通過對三個window對象的打印,我們可以印證整個程序僅有一個UIWindow對象。
三:UIViewController基礎(chǔ)
UIViewController是視圖控制器的意思。整個UIKit框架中只能有一個根視圖控制器,屬于window屬性,但是可以有多個視圖控制器,視圖控制器用來管理界面和處理界面的邏輯類對象,程序啟動前必須對根視圖控制器賦值。
我們在SceneDelegate.m文件中實現(xiàn)對根視圖控制器的初始化。
#import "SceneDelegate.h"
#import "ViewController.h"
@interface SceneDelegate ()@end@implementation SceneDelegate- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {ViewController *vcRoot = [[ViewController alloc] init];self.window.rootViewController = vcRoot;self.window.backgroundColor = [UIColor redColor];}
UIViewController使用
通過對UIViewController的學習,我們可以實現(xiàn)視圖控制界面的切換。下面我們實操來加深學習。
首先,我們需要創(chuàng)建視圖控制器,并為其初始化不同的顏色。
之后我們可以調(diào)用presentViewController方法,實現(xiàn)顯示一個新的視圖控制器界面到屏幕上。再在新的view文件中使用dismissViewControllerAnimated方法(關(guān)閉當前視圖控制器界面),回到原來的視圖控制器界面。
具體實現(xiàn)代碼:
ViewController.m文件
#import "ViewController.h"
#import "ViewC02.h"
@interface ViewController ()@end@implementation ViewController-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{//創(chuàng)建視圖控制器二ViewC02* vc = [[ViewC02 alloc] init];//整個屏幕vc.modalPresentationStyle = UIModalPresentationFullScreen;//顯示一個新的視圖控制器到屏幕上//P1:新的視圖控制器對象//P2:使用動畫切換動畫效果//P3:切換結(jié)束后功能調(diào)用,不需要則穿nil[self presentViewController:vc animated:YES completion:nil];
}
//第一次程序加載視圖時調(diào)用
//只調(diào)用一次
- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.self.view.backgroundColor = [UIColor blueColor];NSLog(@"viewDidLoad 第一次加載!");
}//當視圖控制器的視圖即將顯示時,調(diào)用次函數(shù)
//視圖分為:1.顯示前 2.正在處于顯示狀態(tài) 3.已經(jīng)被隱藏
//參數(shù):是否用動畫切換后顯示
- (void) viewWillAppear:(BOOL)animated
{NSLog(@"viewWillAppear,視圖即將顯示!");
}
//視圖即將消失,調(diào)用次函數(shù)
//參數(shù):表示是否用動畫切換后消失
//當前的狀態(tài):視圖還顯示在屏幕上
-(void) viewWillDisAppear:(BOOL)animated
{NSLog(@"視圖即將消失!");
}//當視圖已經(jīng)顯示到屏幕后到瞬間調(diào)用次函數(shù)
//參數(shù):表示是否用動畫切換顯示的
//當前狀態(tài):已經(jīng)顯示到屏幕上
-(void) viewDidAppear:(BOOL)animated
{NSLog(@"視圖已經(jīng)顯示");
}//當前視圖已經(jīng)從屏幕上消失
//參數(shù):表示是否用動畫切換顯示的
//當前狀態(tài):當前視圖控制器視圖已經(jīng)消失
-(void) viewDidDisappear:(BOOL)animated
{NSLog(@"視圖已經(jīng)消失!");
}
@end
ViewC02.m文件:
#import "ViewC02.h"@interface ViewC02 ()@end@implementation ViewC02- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.self.view.backgroundColor = [UIColor orangeColor];
}-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{ //點擊屏幕后使當前視圖控制器界面消失。[self dismissViewControllerAnimated:YES completion:nil];
}
效果:點擊前:
點擊后:
上圖代碼中,我們還加入了一些操作函數(shù),來顯示屏幕的調(diào)用。
注意:添加下述代碼后,才會使整個屏幕切換,從而顯示viewDidDisappear操作函數(shù)。
vc.modalPresentationStyle = UIModalPresentationFullScreen;
代碼運行結(jié)果:
四:定時器與視圖移動
創(chuàng)建一個定時器,先要在接口文件中創(chuàng)建NSTimer屬性。
@property (retain, nonatomic) NSTimer *timerVier;
之后,采用scheduledTimerWithTimeInterval:的類方法來創(chuàng)建定時器。并啟動該定時器。
該方法有五個參數(shù):
P1:每隔多長時間調(diào)用定時器函數(shù),以秒為單位
P2:表示實現(xiàn)定時器函數(shù)的對象(指針)
P3:定時器函數(shù)對象
P4:可以傳入定時器函數(shù)中一個參數(shù),無參數(shù)可以傳nil
P5:定時器是否重復(fù)操作,YES為重復(fù),NO只完成一次函數(shù)調(diào)用
而我們要實現(xiàn)使指定view對象移動,需要先為view對象設(shè)置一個標簽值,在定時器的事件函數(shù)中進行使用。
下列代碼中的view.frame =CGRectMake(view.frame.origin.x+1, view.frame.origin.y+1, 80, 80);即實現(xiàn)這一功能,使view視圖的x,y軸隨時間移動。
ViewController.m文件
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize timerVier = _timerVier;- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];btn.frame = CGRectMake(100, 100, 80, 40);[btn setTitle:@"啟動定時器" forState:UIControlStateNormal];[btn addTarget:self action:@selector((pressStart)) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:btn];UIButton *btnStop = [UIButton buttonWithType:UIButtonTypeRoundedRect];btnStop.frame = CGRectMake(100, 200, 80, 40);[btnStop setTitle:@"停止定時器" forState:UIControlStateNormal];[btnStop addTarget:self action:@selector(pressStop) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:btnStop];UIView *view = [[UIView alloc] init];view.frame = CGRectMake(0, 0, 80, 80);view.backgroundColor = [UIColor blueColor];[self.view addSubview:view];//設(shè)置view對標簽值//通過爸爸視圖對象以及view的標簽值可以獲得相應(yīng)的對象view.tag = 101 ;
}//按下開始按鈕函數(shù)
- (void) pressStart
{//NSTimer的類方法創(chuàng)建一個定時器并啟動這個定時器//P1:每隔多長時間調(diào)用定時器函數(shù),以秒為單位//P2:表示實現(xiàn)定時器函數(shù)的對象(指針)//P3:定時器函數(shù)對象//P4:可以傳入定時器函數(shù)中一個參數(shù),無參數(shù)可以傳nil//P5:定時器是否重復(fù)操作,YES為重復(fù),NO只完成一次函數(shù)調(diào)用//返回值為一個新建好的定時器對象_timerVier = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTimer:) userInfo:@"lyf" repeats:YES];
}//定時器函數(shù)
//可以將定時器本身作為參數(shù)傳入
-(void) updateTimer:(NSTimer*) timer
{ //userInfo為id類型NSLog(@"test!! name = %@", timer.userInfo);//tag最好從100開始UIView *view = [self.view viewWithTag:101];view.frame =CGRectMake(view.frame.origin.x+1, view.frame.origin.y+1, 80, 80);
}
//按下停止按鈕時調(diào)用
-(void) pressStop
{if (_timerVier != nil) {//停止定時器[_timerVier invalidate];}
}
@end
效果:
還有一點需要注意:如果對上述代碼連續(xù)點擊兩次啟動定時器,此時再按停止定時器將無法停止視圖移動。
原因:每次點擊都創(chuàng)造出一個定時器對象,從而使第一次點擊的定時器對象無法覆蓋。即無法再停止第一個定時器對象,只能停止新建立的定時器對象。
解決辦法:將該屬性變成單例模式,或者每次進入pressStart時,調(diào)用一遍結(jié)束方法。即:
- (void) pressStart
{[self pressStop];//每次調(diào)用前先關(guān)閉前一個定時器。_timerVier = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTimer:) userInfo:@"lyf" repeats:YES];
}
五:UISwitch控件
UISwitch控件是蘋果官方庫提供的一個控件,與定時器相同,需要先在接口部分聲明一個屬性,在實現(xiàn)部分實現(xiàn)其具體功能。
但是,作為蘋果官方的控件,蘋果對其作出了一定的限制:如無法改變寬度高度值和設(shè)計樣式。
下例代碼詳細展示了該控件的使用:
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize mySwitch = _mySwitch;- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.//創(chuàng)建一個開關(guān)對象_mySwitch = [[UISwitch alloc] init];//蘋果官方的控件的位置設(shè)置//位置X,Y的值可以改變//寬度高度值是無法改變。系統(tǒng)默認設(shè)置了_mySwitch.frame = CGRectMake(100, 100, 0, 0);//開關(guān)狀態(tài)設(shè)置屬性//YES:開啟狀態(tài)//NO:關(guān)閉狀態(tài)//_mySwitch.on = YES;//[_mySwitch setOn:YES];//設(shè)置開關(guān)狀態(tài)//P1:狀態(tài)設(shè)置//P2:是否開啟動畫效果[_mySwitch setOn:YES animated:YES];[self.view addSubview:_mySwitch];//設(shè)置開啟狀態(tài)的風格顏色[_mySwitch setOnTintColor:[UIColor orangeColor]];//設(shè)置開關(guān)圓按鈕的風格顏色[_mySwitch setThumbTintColor:[UIColor blueColor]];//設(shè)置整體風格顏色 沒作用原因已截圖[_mySwitch setTintColor:[UIColor redColor]];//向開關(guān)控件添加事件函數(shù)//P1:函數(shù)實現(xiàn)對象//P2:函數(shù)對象//P3:事件響應(yīng)時的事件類型UIControlEventValueChanged:狀態(tài)發(fā)生變化[_mySwitch addTarget:self action:@selector(swChange:) forControlEvents:UIControlEventValueChanged];
}
- (void) swChange:(UISwitch*) sw
{if (sw.on == YES) {NSLog(@"哈哈,打開咯");}else {NSLog(@"哈哈,關(guān)閉咯");}NSLog(@"我變質(zhì)了");
}@end
代碼結(jié)果:
六:滑動條和進度條
滑動條與進度條也需要在接口部分定義屬性,在實現(xiàn)部分實現(xiàn)功能。
除了對這兩個的基本使用,我們還可以實現(xiàn)用滑動條來控制進度條的長短,當滑動條與進度條不是相同長度時,我們可以使用下面的代碼來實現(xiàn)等比例控制。
pView.progress = (_slider.value - _slider.minimumValue) / (_slider.maximumValue - _slider.minimumValue);
ViewController.h
#import <UIKit/UIKit.h>@interface ViewController : UIViewController
{//進度條對象//一般用來表示下載或者視頻播放的進度UIProgressView * pView;//滑動條定義//一般用來進行調(diào)整音頻音樂的音量等UISlider *_slider;
}
//定義一個進度條屬性
@property (retain, nonatomic) UIProgressView *pView;
//定義一個滑動條屬性
@property (retain, nonatomic) UISlider *slider;
@end
ViewController.m
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize slider = _slider;
@synthesize pView = _pView;- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.//進度條的創(chuàng)建pView = [[UIProgressView alloc] init];//進度條的位置大小設(shè)置//進度條的高度是不可以變化的.P4不可變pView.frame = CGRectMake(50, 100, 300, 40);pView.progressTintColor = [UIColor greenColor];pView.trackTintColor = [UIColor blackColor];//設(shè)置進度條的進度值//范圍從0-1//最小值為0,最大值為1pView.progress = 0;//設(shè)置進度條的風格特征pView.progressViewStyle = UIProgressViewStyleBar;[self.view addSubview: pView];//創(chuàng)建滑動條對象_slider = [[UISlider alloc] init];//位置設(shè)置,高度不可變更_slider.frame = CGRectMake(10, 200, 300, 40);//設(shè)置滑動條的最大值_slider.maximumValue = 100;//設(shè)置滑動條的最小值,可以為負_slider.minimumValue = -100;//設(shè)置滑動條的滑塊位置_slider.value = 0;//左側(cè)滑條背景顏色// _slider.minimumTrackTintColor = [UIColor blueColor];//右側(cè)滑動條背景顏色_slider.maximumTrackTintColor = [UIColor greenColor];//設(shè)置滑塊顏色_slider.thumbTintColor = [UIColor orangeColor];[_slider addTarget:self action:@selector(pressSlider) forControlEvents:UIControlEventValueChanged];[self.view addSubview:_slider];
}-(void) pressSlider
{pView.progress = (_slider.value - _slider.minimumValue) / (_slider.maximumValue - _slider.minimumValue);NSLog(@"value = %f", _slider.value);
}@end
效果:
七:步進器和分欄控件
先要在接口文件中定義步進器對象和分欄控制器對象。
ViewController.h
#import <UIKit/UIKit.h>@interface ViewController : UIViewController
{//定義步進器對象//按照一定的數(shù)字來調(diào)整某個數(shù)據(jù)UIStepper *_stepper;//分欄控制器定義UISegmentedControl *_segControl;
}@property (retain, nonatomic) UIStepper *stepper;
@property (retain, nonatomic) UISegmentedControl *segControl;@end
在.m文件中,我們不僅可以以文本來初始化分攔控件,還可以以照片來初始化分攔控件。
ViewController.m
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize stepper = _stepper;
@synthesize segControl = _segControl;- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view._stepper = [[UIStepper alloc] init];//只能設(shè)置位置,寬高不能改變_stepper.frame = CGRectMake(100, 100, 80, 40);//設(shè)置步進器的最小值_stepper.minimumValue = 0;//設(shè)置步進器的最大值_stepper.maximumValue = 100;//設(shè)置步進器的當前值,默認值為0_stepper.value = 10;//設(shè)置步進器每次向前或者向后步進的步伐值_stepper.stepValue = 5;//是否可以重復(fù)響應(yīng)事件操作_stepper.autorepeat = YES;//是否將步進結(jié)果通過事件函數(shù)響應(yīng)出來//不顯示中間過程,長按直接顯示結(jié)果。_stepper.continuous = NO;//添加事件函數(shù)[_stepper addTarget:self action:@selector(stepChange) forControlEvents:UIControlEventValueChanged];[self.view addSubview:_stepper];_segControl = [[UISegmentedControl alloc] init];//寬度可變,高度不可變_segControl.frame = CGRectMake(10, 200, 300, 40);//添加一個按鈕元素[_segControl insertSegmentWithTitle:@"0元" atIndex:0 animated:NO];//參數(shù)一:按鈕選項文字//參數(shù)二:按鈕索引位置//參數(shù)三:是否有插入的動畫效果[_segControl insertSegmentWithTitle:@"5元" atIndex:1 animated:NO];[_segControl insertSegmentWithTitle:@"10元" atIndex:2 animated:NO];// [_segControl insertSegmentWithTitle:@"30元" atIndex:0 animated:NO];//以照片作為分攔控件的圖像。//注意要將照片放入當前視圖內(nèi)。UIImage *image = [UIImage imageNamed:@"WechatIMG26890.jpg"];image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];[_segControl setImage:image forSegmentAtIndex:2];//當前默認按鈕索引設(shè)置_segControl.selectedSegmentIndex = 0;[_segControl addTarget:self action:@selector(segChange) forControlEvents:UIControlEventValueChanged];[self.view addSubview:_segControl];
}-(void) segChange
{NSLog(@"%d", _segControl.selectedSegmentIndex);
}-(void) stepChange
{NSLog(@"step press! value = %f", _stepper.value);
}@end
效果
代碼結(jié)果:
八:警告對話框和等待提示器
在遇到電量不足等特殊情況時,可以彈出警告對話框提示用戶。
在下載或加載比較大的文件時,可以顯示等待提示器提示用戶。
我們在ViewController 的接口文件中定義這兩個對象。
@interface ViewController : UIViewController
{//定義一個警告對話框視圖對象UIAlertController *_alertView;//等待提示對象//當下載,或加載比較大的文件時,可以顯示此控件,處于提示等待狀態(tài)UIActivityIndicatorView *_activityIndicator;
}@property (retain, nonatomic) UIAlertController *alertVier;
@property (retain, nonatomic) UIActivityIndicatorView *act;@end
在實現(xiàn)部分,我們先建立兩個按鈕,用來彈出提示器。
隨后開始實現(xiàn)這兩個提示器。
- 警告對話框
[UIAlertController alertControllerWithTitle:@"警告" message:@"你的手機已經(jīng)被劫持!!!" preferredStyle:UIAlertControllerStyleAlert];
使用如上代碼,設(shè)置樣式為彈出警告式的對話框。參一為標題,參二為警告內(nèi)容。
UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"打錢" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在這里編寫執(zhí)行該選項的代碼*/}];
隨后創(chuàng)建三個不同的action對象,如上所示參一為提示,參二為樣式,參三為需要執(zhí)行的代碼塊
樣式為以下四種:
- UIAlertActionStyleDefault:默認樣式,用于表示用戶可以選擇的主要操作。
- UIAlertActionStyleCancel:取消樣式,通常用于取消操作,只能有一個取消按鈕。
- UIAlertActionStyleDestructive:破壞性樣式,用于表示執(zhí)行此操作可能有破壞性后果。
- UIAlertActionStyleTextInput:輸入樣式,允許用戶在彈出的警告框中輸入文本。
最后將操作對象加入到警告框中,再使該警告框顯示。
- 等待提示器
等待提示器的高度和寬度由蘋果官方設(shè)定,不可改變。可以設(shè)置四種不同風格的等待指示器。
- UIActivityIndicatorViewStyleWhiteLarge:表示一個白色的大號等待提示器。
- UIActivityIndicatorViewStyleWhite:表示一個白色的普通大小等待提示器。
- UIActivityIndicatorViewStyleGray:表示一個灰色的普通大小等待提示器。
- UIActivityIndicatorViewStyleMedium:表示一個中等大小的等待提示器,通常是灰色的。
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize alertVier = _alertVier;
@synthesize act = _act;- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.for (int i = 0; i < 2; i++){UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];btn.frame = CGRectMake(100, 100 + 100 * i, 100, 40);if (i == 0){[btn setTitle:@"警告對話框" forState:UIControlStateNormal];} else if (i == 1) {[btn setTitle:@"等待指示器" forState:UIControlStateNormal];}btn.tag = 101 + i;[btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:btn];}}- (void)pressBtn:(UIButton*) btn {//警告對話框創(chuàng)建if (btn.tag == 101) {_alertView = [UIAlertController alertControllerWithTitle:@"警告" message:@"你的手機已經(jīng)被劫持!!!" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"打錢" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在這里編寫執(zhí)行該選項的代碼*/}];UIAlertAction* action02 = [UIAlertAction actionWithTitle:@"打錢" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){/* 在這里編寫執(zhí)行該選項的代碼*/}];UIAlertAction* action03 = [UIAlertAction actionWithTitle:@"投降" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在這里編寫執(zhí)行該選項的代碼*/}];[_alertView addAction:action01];[_alertView addAction:action02];[_alertView addAction:action03];[self presentViewController:_alertView animated:YES completion:nil];} else if (btn.tag == 102){//寬度和高度不可改變_activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100, 300, 80, 80)];//設(shè)定提示的風格_activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleMedium;_activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleLarge;[self.view addSubview:_activityIndicator];//啟動動畫并顯示[_activityIndicator startAnimating];//停止動畫并隱藏//[_activityIndicator stopAnimating];}
}@end
效果:
(點擊警告對話框)
(點擊等待指示器):
九:UITextField
先要在ViewController的接口部分中定義一個UITextField屬性,在接口部分實現(xiàn)。
作為一個文本控件,該控件只能輸入一行文本,不能輸入或顯示多行,常用來做登錄界面。
我們可以設(shè)置文本的大小,位置,邊框風格和鍵盤風格,字體的大小和顏色。此處主要介紹邊框風格和鍵盤風格
邊框風格:
- UITextBorderStyleRoundedRect圓角風格
- UITextBorderStyleLine線框風格
- UITextBorderStyleBezel:bezel風格
- UITextBorderStyleNone:無邊框風格
鍵盤風格(常用):
- UIKeyboardTypeDefault:默認風格
- UIKeyboardTypeNamePhonePad:字母和數(shù)字組合風格
- UIKeyboardTypeNumberPad:純數(shù)字風格
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize textField = _textField;- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.//設(shè)定一個文本輸入?yún)^(qū)對象self.textField = [[UITextField alloc] init];//設(shè)定文字輸入?yún)^(qū)的位置self.textField.frame = CGRectMake(100, 100, 180, 40);//設(shè)置textField的內(nèi)容文字self.textField.text = @"用戶名";//設(shè)置文字的字體大小self.textField.font = [UIFont systemFontOfSize:15];//設(shè)置字體的顏色self.textField.textColor = [UIColor blueColor];//設(shè)置邊框風格//UITextBorderStyleRoundedRect圓角風格//UITextBorderStyleLine線框風格//UITextBorderStyleBezel:bezel風格//UITextBorderStyleNone:無邊框風格self.textField.borderStyle = UITextBorderStyleLine;//設(shè)置虛擬鍵盤風格//UIKeyboardTypeDefault:默認風格//UIKeyboardTypeNamePhonePad:字母和數(shù)字組合風格//UIKeyboardTypeNumberPad:純數(shù)字風格self.textField.keyboardType = UIKeyboardTypeNumberPad;//提示文字信息//當text屬性為空self.textField.placeholder = @"請輸入用戶名....";//是否作為密碼輸入//YES作為密碼加密,NO正常顯示self.textField.secureTextEntry = NO;[self.view addSubview:self.textField];
}- (void) textFieldDidBeginEditing:(UITextField *)textField
{NSLog(@"開始編輯了!");
}
-(void) textFieldDidEndEditing:(UITextField *)textField
{self.textField.text = @"";NSLog(@"編輯輸入結(jié)束了");
}//是否可以進行輸入
//如果返回值為YES:可以進行輸入,默認為YES
//NO:不能輸入文字,無權(quán)限的時候
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{return YES;
}//是否可以結(jié)束輸入
//如果返回值為YES:可以結(jié)束輸入,默認為YES
//NO:不能結(jié)束文字,輸入密碼位數(shù)不夠
-(BOOL) textFieldShouldEndEditing:(UITextField *)textField
{if (self.textField.text.length < 9) {return NO;} else {return YES;}
}//點擊屏幕空白處調(diào)用此函數(shù)
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{//使虛擬鍵盤回收,不再做為第一消息響應(yīng)者[self.textField resignFirstResponder];
}@end
UITextField控件的四個協(xié)議:
開始編輯協(xié)議:手機鍵盤彈出瞬間調(diào)用
- (void) textFieldDidBeginEditing:(UITextField *)textField
結(jié)束編輯協(xié)議:手機鍵盤收回瞬間調(diào)用
-(void) textFieldDidEndEditing:(UITextField *)textField
是否可以進行輸入
如果返回值為YES:可以進行輸入,默認為YES
NO:不能輸入文字
使用場景:無權(quán)限的時候使用
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
是否可以結(jié)束輸入
如果返回值為YES:可以結(jié)束輸入,默認為YES
NO:不能結(jié)束文字
使用場景:輸入密碼位數(shù)不夠
-(BOOL) textFieldShouldEndEditing:(UITextField *)textField
滾動視圖
手機屏幕較小,通常情況下無法完全展示內(nèi)容,此時我們可以通過手勢來滾動屏幕,達到移動視圖的效果,而這個應(yīng)用就是滾動視圖。
像我們常用的QQ聊天頁面,微信聊天頁面等,就是滾動視圖的實際應(yīng)用。
滾動視圖基礎(chǔ)
我們可以設(shè)置上下滾動的視圖,也可以設(shè)置左右滾動的視圖,對應(yīng)的畫布大小需要跟著改變。
滾動視圖的創(chuàng)建在視圖控制器中進行,即ViewController文件中進行。
圖片的添加我們采用循環(huán)的方式簡略。
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.UIScrollView *sv = [[UIScrollView alloc] init];//設(shè)置滾動視圖的位置,使用矩形來定位視圖位置sv.frame = CGRectMake(0, 0, 394, 852);//是否按照整頁來滾動視圖sv.pagingEnabled = YES;//是否可以開啟滾動效果sv.scrollEnabled = YES;//設(shè)置畫布的大小,畫布顯示在滾動視圖內(nèi)部,一般大于Frame的大小sv.contentSize = CGSizeMake(394*6, 852*2);//是否可以邊緣彈動效果sv.bounces = YES;//開啟橫向彈動效果sv.alwaysBounceHorizontal = YES;//開啟縱向彈動效果sv.alwaysBounceVertical = YES;//顯示橫向滾動效果sv.showsHorizontalScrollIndicator = YES;//顯示縱向滾動條sv.showsVerticalScrollIndicator = YES;//設(shè)置背景顏色sv.backgroundColor = [UIColor whiteColor];//使用循環(huán)創(chuàng)建5張圖片視圖for (int i = 0; i < 5; i++){NSString *strName = [NSString stringWithFormat:@"%d.jpg", i+1];UIImage *image = [UIImage imageNamed:strName];UIImageView *iView = [[UIImageView alloc] initWithImage:image];iView.frame = CGRectMake(394*i, 0, 394, 952);[sv addSubview:iView];}[self.view addSubview:sv];
}@end
滾動視圖高級
滾動視圖中還有一個協(xié)議叫UIScrollViewDelegate,這個協(xié)議可以讓我們實現(xiàn)更多控制效果。我們新建一個豎屏滾動效果來學習這個協(xié)議的方法。
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.scrollView = [[UIScrollView alloc] init];scrollView.frame = CGRectMake(10, 50, 384, 802);//取消彈動效果scrollView.bounces = NO;//是否允許通過點擊屏幕讓滾動視圖響應(yīng)事件//YES:滾動視圖可以接受觸碰事件//NO:不接受觸屏事件//scrollView.userInteractionEnabled = NO;//設(shè)置畫布大小,縱向效果scrollView.contentSize = CGSizeMake(384, 802*9);for (int i = 0; i < 9; i++){//生成圖片名稱NSString *strName = [NSString stringWithFormat:@"%d.jpg", i+1];UIImage *image = [UIImage imageNamed:strName];UIImageView *iView = [[UIImageView alloc] init];iView.image = image;iView.frame = CGRectMake(0, 802 * i, 384, 802);[scrollView addSubview:iView];}[self.view addSubview:scrollView];//取消按頁滾動效果scrollView.pagingEnabled = NO;//滾動視圖畫布的移動位置,偏移位置//功能:決定畫筆顯示的最終圖像結(jié)果scrollView.contentOffset = CGPointMake(0, 0);//將當前視圖控制器作為代理對象scrollView.delegate = self;
}//當滾動視圖移動時,只要offset坐標發(fā)生變化,都會調(diào)用此函數(shù)
//參數(shù):調(diào)用次協(xié)議的滾動視圖對象
//可以使用此函數(shù)來監(jiān)控滾動視圖的位置
//- (void) scrollViewDidScroll:(UIScrollView *)scrollView
//{
// NSLog(@"y = %f", scrollView.contentOffset.y);
//}- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{//scrollView.contentOffset = CGPointMake(0, 0);//讓滾動視圖移動到指定位置,動畫移動[scrollView scrollRectToVisible:CGRectMake(0, 0, 384, 802) animated:YES];
}//結(jié)束滑動時調(diào)用
-(void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{NSLog(@"Did End Drag!");
}
//將要開始滑動時調(diào)用
-(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView
{NSLog(@"will Begin Drag");
}
//結(jié)束滑動視圖后,準備結(jié)束滑行調(diào)用該函數(shù)
-(void) scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{NSLog(@"Will End Drag!");
}
//準備開始滑動時調(diào)用該方法
-(void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{NSLog(@"Will Begin Deceleratege!");
}
//停止滑動時調(diào)用該方法
-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{NSLog(@"視圖停止移動");
}
@end
注意:當結(jié)束滑動后,會有一個類似于慣性的滑動效果,此時會調(diào)用不同的方法。
效果:
總結(jié)
多多學習,多多思考。這篇博客的總結(jié)讓我加深了對UI控件的認識,并且熟練了很多UI控件的使用