- UILable
- numberofLines屬性設(shè)置文字的行數(shù),0為自動(dòng)計(jì)算
- 當(dāng)文字行數(shù)超過(guò)寬度時(shí)會(huì)自動(dòng)換行 超過(guò)高度時(shí)會(huì)顯示省略號(hào)
- UIButton
- 該對(duì)象初始化需要使用靜態(tài)方法UIButton* btn = [UIButton buttonWidthType:$BtnType];
- 設(shè)置title需要用setTitle forState 例:
[btn setTitle:@"click me" forState:UIControlStateNormal];
- 設(shè)置圓角
//設(shè)置超出圖形范圍剪裁
btn.clipToBounds = YES;
//設(shè)置圖形布局的角半徑為10;
btn.layer.cornerRadius = 10;
- 綁定事件函數(shù)需要用到選擇器@selector()
//例子:
[btn addTarget:self action:@selector(sayHey:) forControlEvents:UIControlEventTouchDown];
- (void) sayHey:(UIButton*)btn{
NSLog(@"%@",btn);
NSLog(@"hey");
}
//創(chuàng)建定時(shí)器
[NSTimer scheduledTimerWithTimeInterval:2 repeats:NO block:^(NSTimer * _Nonnull timer) {
NSLog(@"begin");
}];
//也可以用@selector方式去創(chuàng)建 創(chuàng)建完即運(yùn)行 不需要手動(dòng)調(diào)用
//`注`: NSTimeInterval(第一個(gè)參數(shù))是一個(gè)以秒為單位的間隔.
- UISwitch
- 使用addTarget來(lái)綁定函數(shù) 用isOn來(lái)判斷控件狀態(tài)
UISwitch* sw = [[UISwitch alloc] initWithFrame:CGRectMake(30, 50, 200, 100)];
[sw addTarget:self action:@selector(pressTarget:) forControlEvents:UIControlEventValueChanged];
- (void) pressTarget:(UISwitch*)sw{
NSLog(@"%i",sw.isOn);
}
```
* UISlider(滑塊)
```objectivec
UISlider* us = [[UISlider alloc] initWithFrame:CGRectMake(30, 50, 200, 200)];
us.value = 20;
us.maximumValue = 200;
us.minimumValue = 10;
us.tintColor = [UIColor greenColor];
//圓圈樣式
us.thumbTintColor = [UIColor purpleColor];
[us addTarget:self action:@selector(pressTarget:) forControlEvents:UIControlEventValueChanged];
[view addSubview:us];
- (void) pressTarget:(UISlider*)us{
NSLog(@"%.0f",us.value);
}
- (instancetype)init{
mainPage* view = [super init];
UIStepper* step = [[UIStepper alloc] initWithFrame:CGRectMake(30, 50, 200, 200)];
step.minimumValue = 10;
step.maximumValue = 100;
step.stepValue = 10;
step.value = 8;
UILabel* ul = [[UILabel alloc] initWithFrame:CGRectMake(80, 50, 200, 200)];
ul.text = [NSString stringWithFormat:@"%i",8];
ul.tag = 200;
[step addTarget:self action:@selector(pressTarget:) forControlEvents:UIControlEventValueChanged];
[view addSubview:step];
[view addSubview:ul];
return view;
}
- (void) pressTarget:(UIStepper*)us{
UILabel* ul = [self viewWithTag:200];
ul.text =[NSString stringWithFormat:@"%.0f",us.value];
}
- (instancetype)init{
mainPage* view = [super init];
UISegmentedControl* usc = [[UISegmentedControl alloc] initWithFrame:CGRectMake(30, 50, 300,50)];
[usc insertSegmentWithTitle:@"first" atIndex:0 animated:YES];
[usc insertSegmentWithTitle:@"second" atIndex:1 animated:YES];
[usc addTarget:self action:@selector(pressTarget:) forControlEvents:UIControlEventValueChanged];
[view addSubview:usc];
return view;
}
- (void) pressTarget:(UISegmentedControl*)usc{
NSLog(@"%li",usc.selectedSegmentIndex);
}
UIActivityIndicatorView* ac = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(30, 120, 300,50)];
//必須設(shè)置一種風(fēng)格樣式
ac.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[ac startAnimating];
//[ac stopAnimating]; 關(guān)閉加載動(dòng)畫
//textfiled樣式
tx.borderStyle = UITextBorderStyleRoundedRect;
tx.keyboardType = UIKeyboardTypeNumberPad;
// 開啟密碼保護(hù)
tx.secureTextEntry = YES;
//回收鍵盤
//viewController.m 點(diǎn)擊空白處回收鍵盤
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITextField* tx = [self.view viewWithTag:201];
[tx resignFirstResponder];
}
//監(jiān)聽事件(需要實(shí)現(xiàn)UITextFieldDelegate協(xié)議)
@interface mainPage : UIView<UITextFieldDelegate>
//給控件指定代理類
tx.delegate = self;
//選擇需要監(jiān)聽的事件進(jìn)行實(shí)現(xiàn)
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"%@",@"開始編輯");
}
mainPage* view = [super init];
UIScrollView* usv = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//設(shè)置內(nèi)容畫布大小
usv.contentSize = CGSizeMake(320*5, 570);
//創(chuàng)建子視圖加入到滾動(dòng)視圖中
UIView* uv = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 570)];
uv.backgroundColor = [UIColor greenColor];
[usv addSubview:uv];
[view addSubview:usv];
UIImage* imgData = [UIImage imageNamed:@"test1.jpg"];
UIImageView* img = [[UIImageView alloc] initWithImage:imgData];
- UIGestureRecognizer(手勢(shì)基礎(chǔ)事件)
//開啟事件響應(yīng)默認(rèn)是NO
img.userInteractionEnabled = YES;
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAct:)];
// 幾次點(diǎn)擊時(shí)觸發(fā)
gesture.numberOfTapsRequired = 1;
// 需要幾個(gè)手指點(diǎn)擊觸發(fā)
gesture.numberOfTouchesRequired = 1;
// 給視圖添加響應(yīng)事件
[img addGestureRecognizer:gesture];
- (void)tapAct:(UITapGestureRecognizer *)gestrue{
UIView* image = gestrue.view;
//開啟動(dòng)畫
[UIView beginAnimations:nil context:nil];
// 獲取手勢(shì)在視圖中的位置
CGPoint point = [gestrue locationInView:image];
image.frame = CGRectMake(point.x, point.y, image.frame.size.width, image.frame.size.height);
[UIView commitAnimations];
NSLog(@"%@",NSStringFromCGPoint(point));
}
//注一個(gè)視圖可以添加多個(gè)響應(yīng)事件 但是一個(gè)響應(yīng)事件不能給多個(gè)視圖使用
- UIGestureRecognizer(手勢(shì)高級(jí)事件)
- UIRotationGestureRecognizer UIPinchGestureRecognizer
- (instancetype)init{
mainPage* view = [super init];
UIImage* imgData = [UIImage imageNamed:@"test1.jpg"];
UIImageView* img = [[UIImageView alloc] initWithImage:imgData];
img.frame = CGRectMake(0, 0, 300, 500);
[view addSubview:img];
img.userInteractionEnabled = YES;
UIRotationGestureRecognizer* rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotaAct:)];
UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAct:)];
[img addGestureRecognizer:rotation];
[img addGestureRecognizer:pinch];
return view;
}
- (void)pinchAct:(UIPinchGestureRecognizer *)gestrue{
UIImageView* curView = (UIImageView*)gestrue.view;
// 在原來(lái)的矩陣基礎(chǔ)上進(jìn)行縮放大小的矩陣變換 參數(shù)2是x方向縮放比例 參數(shù)2是Y方向縮放比例
curView.transform = CGAffineTransformScale(curView.transform, gestrue.scale, gestrue.scale);
//做完一次變換后必須歸為 不然會(huì)進(jìn)行累加 縮放效果太大
gestrue.scale = 1;
NSLog(@"pinch");
}
- (void)rotaAct:(UIRotationGestureRecognizer *)gestrue{
UIImageView* curView = (UIImageView*)gestrue.view;
// 在原來(lái)的矩陣基礎(chǔ)上進(jìn)行縮放大小的矩陣變換
curView.transform = CGAffineTransformRotate(curView.transform, gestrue.rotation);
//做完一次變換后必須歸為 不然會(huì)進(jìn)行累加 縮放效果太大
gestrue.rotation = 0;
NSLog(@"rotate");
}
注:
如果要實(shí)現(xiàn)多個(gè)手勢(shì)事件同時(shí)響應(yīng)的話需要實(shí)現(xiàn)<UIGestureRecognizerDelegate>代理
然后給手勢(shì)對(duì)象的代理設(shè)置為實(shí)現(xiàn)了代理的類 然后實(shí)現(xiàn)shouldRecognizeSimultaneouslyWithGestureRecognizer方法
返回YES 即允許同時(shí)響應(yīng)多個(gè)手勢(shì)事件
- 拓展手勢(shì)
- UIPanGestureRecognizer 平移手勢(shì)
- UILongPressGestureRecognizer 長(zhǎng)按手勢(shì)
- UISwipeGestureRecognizer 滑動(dòng)手勢(shì)
- xib使用
//AppDelegate.m //nibName要對(duì)應(yīng)xib名稱 mainBundle代表是根目錄
self.window.rootViewController = [[ViewController alloc] initWithNibName:@"VCxib" bundle:[NSBundle mainBundle]];
- 自動(dòng)布局 autoresizingMask
//注意 如果視圖要緊貼容器右側(cè)即設(shè)置左邊距為自動(dòng)布局 其他也是
self.con.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
//注意 自動(dòng)布局可以設(shè)置多個(gè)屬性
self.con.autoresizingMask = UIViewAutoresizingFlexibleWidth;