IOS 代码片段



一些代码片段


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://[email protected]"]];
 
 
 
//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];
}





你可能感兴趣的:(IOS 代码片段)