IOS - 代碼片段 – iPhone手機開發技術文章 iPhone軟體開發教學課程

一些代碼片段

 

 

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:@"https://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:@"https://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];

}

 

You May Also Like