您好,登錄后才能下訂單哦!
小編給大家分享一下iOS代碼片段的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
【1】鍵盤(pán)遮擋的一種解決方法:
主要實(shí)現(xiàn)邏輯: 將需要顯示的控件放置在一個(gè)單獨(dú)的UIView中,然后整個(gè)view上移處理
代碼實(shí)現(xiàn):
+(void)animationView:(UIScrollView *)anmaView activeRect:(CGRect)filedRect
{
// CGRect filedRect=filed.frame;
CGPoint point=anmaView.contentOffset;
// 計(jì)算出距離屏幕上方的相對(duì)坐標(biāo)高度
float realDis=filedRect.origin.y-point.y;
// 計(jì)算到目標(biāo)點(diǎn)的距離的絕對(duì)值
int distance1=0;
if (iPhone5) {
distance1=120;
}else{
distance1=90;
}
float distance=fabsf(realDis-distance1);
if (realDis>distance1) {
point.y+=distance;
}else{
point.y-=distance;
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
anmaView.contentOffset=point;
[UIView commitAnimations];
}
【2】移動(dòng)號(hào)碼驗(yàn)證(網(wǎng)絡(luò)轉(zhuǎn)載,已驗(yàn)證)
- (BOOL)isMobileNumber:(NSString*)mobileNum
{
/**
* 手機(jī)號(hào)碼
* 移動(dòng):134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
* 聯(lián)通:130,131,132,152,155,156,185,186
* 電信:133,1349,153,180,189
*/
NSString* MOBILE= @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
/**
* 中國(guó)移動(dòng):China Mobile
* 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
*/
NSString* CM= @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
/**
* 中國(guó)聯(lián)通:China Unicom
* 130,131,132,152,155,156,185,186
*/
NSString* CU= @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
/**
* 中國(guó)電信:China Telecom
* 133,1349,153,180,189
*/
NSString* CT= @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
/**
* 大陸地區(qū)固話(huà)及小靈通
* 區(qū)號(hào):010,020,021,022,023,024,025,027,028,029
* 號(hào)碼:七位或八位
*/
// NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",MOBILE];
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CT];
if(([regextestmobile evaluateWithObject:mobileNum] == YES)
|| ([regextestcm evaluateWithObject:mobileNum] == YES)
|| ([regextestct evaluateWithObject:mobileNum] == YES)
|| ([regextestcu evaluateWithObject:mobileNum] == YES))
{
return YES;
}
else
{
return NO;
}
}
【3】郵箱驗(yàn)證(網(wǎng)絡(luò)轉(zhuǎn)載,未驗(yàn)證)
-(BOOL)validateEmail:(NSString*)email
{
if((0 != [email rangeOfString:@"@"].length) &&
(0 != [email rangeOfString:@"."].length))
{
NSCharacterSet* tmpInvalidCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
NSMutableCharacterSet* tmpInvalidMutableCharSet = [[tmpInvalidCharSet mutableCopy] autorelease];
[tmpInvalidMutableCharSet removeCharactersInString:@"_-"];
//使用compare option 來(lái)設(shè)定比較規(guī)則,如
//NSCaseInsensitiveSearch是不區(qū)分大小寫(xiě)
//NSLiteralSearch 進(jìn)行完全比較,區(qū)分大小寫(xiě)
//NSNumericSearch 只比較定符串的個(gè)數(shù),而不比較字符串的字面值
NSRange range1 = [email rangeOfString:@"@"
options:NSCaseInsensitiveSearch];
//取得用戶(hù)名部分
NSString* userNameString = [email substringToIndex:range1.location];
NSArray* userNameArray = [userNameString componentsSeparatedByString:@"."];
for(NSString* string in userNameArray)
{
NSRange rangeOfInavlidChars = [string rangeOfCharacterFromSet: tmpInvalidMutableCharSet];
if(rangeOfInavlidChars.length != 0 || [string isEqualToString:@""])
return NO;
}
NSString *domainString = [email substringFromIndex:range1.location+1];
NSArray *domainArray = [domainString componentsSeparatedByString:@"."];
for(NSString *string in domainArray)
{
NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet:tmpInvalidMutableCharSet];
if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])
return NO;
}
return YES;
}
else // no ''@'' or ''.'' present
return NO;
}
BOOL NSStringIsValidEmail(NSString *checkString)
{
NString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSString *laxString = @".+@.+\.[A-Za-z]{2}[A-Za-z]*";
NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:checkString];
}
以上是“iOS代碼片段的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。