IOS - 程式碼片段

來源:互聯網
上載者:User

一些程式碼片段  1. 16進位顏色值的轉換#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]  2.md5 + (NSString*)md5:(NSString*)str{    constchar*cStr = [str UTF8String];    unsignedcharresult[16];    CC_MD5(cStr, strlen(cStr), result);    return[NSStringstringWithFormat:@"XXXXXXXXXXXXXXXX",            result[0], result[1], result[2],  result[3],            result[4],  result[5],  result[6],  result[7],            result[8],  result[9],  result[10],  result[11],            result[12],  result[13],  result[14],  result[15]            ];}  3.調用 //1、調用 內建mail [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"mailto://admin@hzlzh.com"]];   //2、調用 電話phone [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"tel://8008808888"]];   //3、調用 SMS [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"sms://800888"]];   //4、調用內建 瀏覽器 safari [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"http://www.hzlzh.com"]];   //調用phone可以傳遞號碼,調用SMS 只能設定號碼,不能初始化SMS內容。  4.計算2個經緯度之間距離 +(double)distanceBetweenOrderBy:(double)lat1:(double)lat2:(double)lng1:(double)lng2{      CLLocation* curLocation = [[CLLocationalloc]initWithLatitude:lat1longitude:lng1];    CLLocation* otherLocation = [[CLLocationalloc]initWithLatitude:lat2longitude:lng2];   doubledistance  = [curLocation distanceFromLocation:otherLocation];    returndistance;}   5.輸入框中是否有個叉號,在什麼時候顯示,用於一次性刪除輸入框中的內容text.clearButtonMode=UITextFieldViewModeAlways;       6.iOS本地推送 第一步:建立本地推送 // 建立一個本地推送  UILocalNotification*notification = [[[UILocalNotificationalloc]init]autorelease]; //設定10秒之後 NSDate*pushDate = [NSDatedateWithTimeIntervalSinceNow:10]; if(notification != nil) {      // 設定推送時間      notification.fireDate= pushDate;      // 設定時區      notification.timeZone= [NSTimeZonedefaultTimeZone];     // 設定重複間隔      notification.repeatInterval= kCFCalendarUnitDay;      // 推送聲音      notification.soundName= UILocalNotificationDefaultSoundName;     // 推送內容      notification.alertBody= @"推送內容";     //顯示在icon上的紅色圈中的數子     notification.applicationIconBadgeNumber= 1;     //設定userinfo 方便在之後需要撤銷的時候使用      NSDictionary*info = [NSDictionarydictionaryWithObject:@"name"forKey:@"key"];     notification.userInfo= info;      //添加推送到UIApplication            UIApplication*app = [UIApplicationsharedApplication];     [appscheduleLocalNotification:notification];        }    第二步:接收本地推送 - (void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification*)notification{     UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"iWeibo"message:notification.alertBodydelegate:nilcancelButtonTitle:@"確定"otherButtonTitles:nil];     [alertshow];     // 表徵圖上的數字減1      application.applicationIconBadgeNumber-= 1; }    第三步:解除本地推送 // 獲得 UIApplication  UIApplication*app = [UIApplicationsharedApplication]; //擷取本地推送數組 NSArray*localArray = [app scheduledLocalNotifications]; //聲明本地通知對象 UILocalNotification*localNotification;  if(localArray) {      for(UILocalNotification*noti inlocalArray) {          NSDictionary*dict = noti.userInfo;         if(dict) {              NSString*inKey = [dict objectForKey:@"key"];             if([inKey isEqualToString:@"對應的key值"]) {                  if(localNotification){                      [localNotificationrelease];                     localNotification = nil;                 }                 localNotification = [noti retain];                 break;             }         }     }           //判斷是否找到已經存在的相同key的推送     if(!localNotification) {          //不存在初始化         localNotification = [[UILocalNotificationalloc]init];     }           if(localNotification) {          //不推送 取消推送          [appcancelLocalNotification:localNotification];         [localNotificationrelease];         return;     } }   7.點選連結直接跳轉到 App Store 指定應用下載頁面  //跳轉到應用頁面NSString*str = [NSStringstringWithFormat:@"http://itunes.apple.com/us/app/id%d",appid]; [[UIApplicationsharedApplication]openURL:[NSURLurlWithString:str]]; //跳轉到評價頁面NSString*str = [NSStringstringWithFormat:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id;=%d",                            appid ];    [[UIApplicationsharedApplication]openURL:[NSURLurlWithString:str]];  8.父級view 不響應touch事件 子view相應事件 -(id)hitTest:(CGPoint)pointwithEvent:(UIEvent*)event {    idhitView = [superhitTest:pointwithEvent:event];    if(hitView == self)returnnil;    elsereturn hitView;}   9.給視圖加上倒影效果 constCGFloat kReflectPercent = -0.25f;constCGFloat kReflectOpacity = 0.3f;constCGFloat kReflectDistance = 10.0f;+ (void)addSimpleReflectionToView: (UIView*) theView{    CALayer*reflectionLayer = [CALayerlayer];    reflectionLayer.contents= [theView layer].contents;    reflectionLayer.opacity= kReflectOpacity;    reflectionLayer.frame= CGRectMake(0.0f,0.0f,        theView.frame.size.width,        theView.frame.size.height* kReflectPercent);    CATransform3Dstransform = CATransform3DMakeScale(1.0f, -1.0f,1.0f);    CATransform3Dtransform = CATransform3DTranslate(stransform,0.0f,        -(kReflectDistance + theView.frame.size.height),0.0f);    reflectionLayer.transform= transform;    reflectionLayer.sublayerTransform= reflectionLayer.transform;    [[theViewlayer]addSublayer:reflectionLayer];} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.