本人iOS菜鳥(niǎo)一枚, 不太會(huì)寫文章, 語(yǔ)文從來(lái)沒(méi)及格過(guò), 進(jìn)入正題
需求分析
看圖說(shuō)話:
一個(gè)個(gè)輸入號(hào)碼太麻煩了 所以就賦值粘貼了
主要的功能
- 當(dāng)剛進(jìn)來(lái)的時(shí)候有一個(gè)空的輸入框
- 輸入一個(gè)號(hào)碼為正確的號(hào)碼時(shí)再添加一個(gè)空的輸入框(輸入錯(cuò)誤不添加給提示)
- 刪除號(hào)碼的功能
做了個(gè)簡(jiǎn)易的demo
判斷思路
整個(gè)頁(yè)面上的控件就不用多說(shuō)了 就是一個(gè)UICollectionView
基本上都是采用Block傳值的, cell里注冊(cè)了個(gè)通知, 當(dāng)前的textFeild的內(nèi)容改變就會(huì)收到這個(gè)通知, 一大部分邏輯在這里, 直接上代碼
- (void)textFeildDidChange:(NSNotification *)noti{
if (noti.object == self.textFeild){
if ([self isMobile:self.textFeild.text]) {
self.item.isNumber = YES;
} else {
self.item.isNumber = NO;
}
self.item.number = self.textFeild.text;
if (self.textFeildChangeBlock) {
self.textFeildChangeBlock(self, self.item);
}
if (self.textFeild.text.length == 0){
if (self.textFeildEmptyString) {
self.textFeildEmptyString(self, self.item);
}
}
if (self.textFeild.text.length > 10) {
self.textFeild.text = [self.textFeild.text substringToIndex:11];
if (![self isMobile:self.textFeild.text]) {
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"請(qǐng)輸入正確的手機(jī)號(hào)" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];
[alert show];
}
}
if ([self isMobile:self.textFeild.text]) {
self.textFeild.backgroundColor = RGBColor(242, 245, 235);
self.textFeild.textColor = RGBColor(0, 0, 0);
if (self.nextNumberTextFeildBlock) {
self.nextNumberTextFeildBlock();
}
} else {
self.textFeild.backgroundColor = [UIColor whiteColor];
self.textFeild.textColor = RGBColor(252, 77, 48);
}
}
}
輸入的號(hào)碼每改變一次, 就會(huì)調(diào)用這個(gè)方法, 先是判斷是否是手機(jī)號(hào), 將輸入框的手機(jī)號(hào)賦值給當(dāng)前cell綁定的Item上 然后傳出去 更新collectionView里的數(shù)組的對(duì)應(yīng)的手機(jī)號(hào)碼
手機(jī)號(hào)不能大于11位, 所以做了一個(gè)截取的操作, 當(dāng)是11為的時(shí)候, 判斷是不是手機(jī)號(hào), 不是的話, 給一個(gè)提示. 再一次判斷是否是手機(jī)號(hào)是為了能添加下一個(gè)輸入框, 讓是手機(jī)號(hào)的cell高亮, 字體顏色高亮
中間有一個(gè)判斷長(zhǎng)度是否為0的操作 是刪除空的cell的
下面我們看看這些Block操作的代碼
__weak typeof(self)weakSelf = self;
[cell setTextFeildChangeBlock:^(NumberCollectionCell *cell, NumberItem *item) {
NSIndexPath *indexPath2 = [collectionView indexPathForCell:cell];
[weakSelf.numbers replaceObjectAtIndex:indexPath2.row withObject:item];
}];
[cell setNextNumberTextFeildBlock:^{
NumberItem *item = [weakSelf.numbers lastObject];
NSString *number = item.number;
if ([weakSelf isMobile:number]) {
[weakSelf.numbers addObject:[NumberItem numberItemWithNumber:@"" isNumber:NO]];
NSIndexPath *indexPath1 = [NSIndexPath indexPathForItem:weakSelf.numbers.count - 1 inSection:0];
[collectionView insertItemsAtIndexPaths:@[indexPath1]];
NSInteger count = weakSelf.numbers.count;
NSInteger cloum = weakSelf.cloum;
if ((count - 1) % cloum == 0) {
NSInteger lines = count % cloum == 0 ? count / cloum : count / cloum + 1;
CGPoint offset = CGPointMake(0, lines * (weakSelf.itemHeight + 10) + 10);
CGFloat height = self.bounds.size.height;
if (offset.y > height) {
offset.y = offset.y - height;
offset.y = offset.y + 10;
collectionView.contentOffset = offset;
}
}
NumberCollectionCell *cell1 = (NumberCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath1];
[cell1 textFeildBecomeFirstResponder];
}
}];
[cell setTextFeildEmptyString:^(NumberCollectionCell *cell, NumberItem *item) {
NSIndexPath *indexPath = [weakSelf indexPathForCell:cell];
if (indexPath.row != weakSelf.numbers.count - 1) {
NSIndexPath *indexPath1 = [NSIndexPath indexPathForItem:self.numbers.count -1 inSection:0];
NumberCollectionCell *cell1 = (NumberCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath1];
[cell1 textFeildBecomeFirstResponder];
[weakSelf.numbers removeObjectAtIndex:indexPath.row];
[weakSelf deleteItemsAtIndexPaths:@[indexPath]];
}
}];
注意點(diǎn)
- 添加的時(shí)候之所以沒(méi)有給動(dòng)畫, 是因?yàn)橐尲磳⒊霈F(xiàn)的cell的textField成為第一響應(yīng)者, 加了動(dòng)畫有一個(gè)時(shí)間差, 拿不到這個(gè)cell
沒(méi)有一點(diǎn)注釋, 有需要可以私信我
第一次發(fā)文章, 不會(huì)分享, 將就看一下吧, 有什么問(wèn)題可以評(píng)論, 當(dāng)然也可以加我QQ互相交流847881570 備注:簡(jiǎn)書(shū)