实用代码

1.将UIView设置成圆角的矩形需要加QuartzCore.framework

m_mainImgView.layer.cornerRadius = 6;

m_mainImgView.layer.masksToBounds = YES;


2.segment要添加图片

 [mapType_SegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"d_cn_32.png"] atIndex:0 animated:YES];
 [mapType_SegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"w_cn_32.png"] atIndex:1 animated:YES];
 [mapType_SegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"r_cn_32.png"] atIndex:2 animated:YES];


  3.上传图片

    //上传到服务器
    //分界线的标识符
    NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
    //根据url初始化request
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",web_site]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSLog(@"url:%@",url);
    //分界线 --AaB03x
    NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
    //结束符 AaB03x--
    NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
    //得到图片的data
    NSLog(@"daxiao:%dKB",[UIImageJPEGRepresentation(image, 0.1) length]/1024);
    NSData* data = UIImageJPEGRepresentation(image, 0.1);
    //http body的字符串
    NSMutableString *body=[[NSMutableString alloc]init];
    NSString *message = [NSString stringWithFormat:@"%@\r\n",[dict JSONRepresentation]];
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:message,@"message",nil];
    NSArray *keys = [dictionary allKeys];
    for (int j=0; j<[keys count]; j++) {
        NSString *key =[keys objectAtIndex:j];
        //添加分界线,换行
        [body appendFormat:@"%@\r\n",MPboundary];
        //添加字段名称,换2行
        [body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
        //添加字段的值
        [body appendFormat:@"%@\r\n",[dictionary objectForKey:key]];
    }
    [body appendFormat:@"%@\r\n",MPboundary];
    //添加字段名称,换2行
    [body appendFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"image.PNG\"\r\n"];
    ////添加分界线,换行
    [body appendFormat:@"%@\r\n",MPboundary];
    //声明上传文件的格式
    [body appendFormat:@"Content-Type: image/PNG\r\n\r\n"];
    //声明结束符:--AaB03x--
    NSString *end=[[NSString alloc]initWithFormat:@"\r\n%@",endMPboundary];
    //声明myRequestData,用来放入http body
    NSMutableData *myRequestData=[NSMutableData data];
    //将body字符串转化为UTF8格式的二进制
    [myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
    //将image的data加入
    [myRequestData appendData:data];
    //加入结束符--AaB03x--
    [myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
    //设置HTTPHeader中Content-Type的值
    NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY,nil];
    //设置HTTPHeader
    [request setValue:content forHTTPHeaderField:@"Content-Type"];
    //设置Content-Length
    [request setValue:[NSString stringWithFormat:@"%d", [myRequestData length]] forHTTPHeaderField:@"Content-Length"];
    //http method
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:myRequestData];
    //建立连接,设置代理
    NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [self.connections addObject:[NSDictionary dictionaryWithObjectsAndKeys:connection,@"connection",[dict objectForKey:@"MessageID"],@"assId", nil]];


4.计算2点之间距离

CLLocation* orig=[[[CLLocation alloc] initWithLatitude:**  longitude:**] autorelease];
CLLocation* dist=[[[CLLocation alloc] initWithLatitude:** longitude:** ] autorelease];
俩点间的距离=[orig distanceFromLocation:dist];
这个算出来的是俩点间的直线距离。偏差50米左右

5.字符串过滤

    NSString *str = @"一个 (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 汉0932字#@!中..文//>?输~~~@#¥#@%#^#^%&^*&(*)入";    
    NSMutableCharacterSet *set = [[NSMutableCharacterSet alloc] init];

    [set formUnionWithCharacterSet:[NSCharacterSet lowercaseLetterCharacterSet]];//小写字母

    NSLog(@"1 %@",[str stringByTrimmingCharactersInSet:set]);

    [set formUnionWithCharacterSet:[NSCharacterSet uppercaseLetterCharacterSet]];//大写字母

    NSLog(@"2 %@",[str stringByTrimmingCharactersInSet:set]);

    [set formUnionWithCharacterSet:[NSCharacterSet symbolCharacterSet]];//符号
    
    NSLog(@"3 %@",[str stringByTrimmingCharactersInSet:set]);
    
         [set formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];//标点
    
    NSLog(@"4 %@",[str stringByTrimmingCharactersInSet:set]);
    
         [set formUnionWithCharacterSet:[NSCharacterSet controlCharacterSet]];//把握符
    
    NSLog(@"5 %@",[str stringByTrimmingCharactersInSet:set]);
    
         [set formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];//小数
    
    NSLog(@"6 %@",[str stringByTrimmingCharactersInSet:set]);
    
         [set formUnionWithCharacterSet:[NSCharacterSet letterCharacterSet]];//文字
    
    NSLog(@"7 %@",[str stringByTrimmingCharactersInSet:set]);
    
         [set formUnionWithCharacterSet:[NSCharacterSet nonBaseCharacterSet]];//非根蒂根基
    
    NSLog(@"8 %@",[str stringByTrimmingCharactersInSet:set]);
    
         [set formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];//字母数字
    
    NSLog(@"9 %@",[str stringByTrimmingCharactersInSet:set]);
    
         [set formUnionWithCharacterSet:[NSCharacterSet decomposableCharacterSet]];//可分化
    
    NSLog(@"10 %@",[str stringByTrimmingCharactersInSet:set]);
    
         [set formUnionWithCharacterSet:[NSCharacterSet illegalCharacterSet]];//不法
    
    NSLog(@"11 %@",[str stringByTrimmingCharactersInSet:set]);
    
         [set formUnionWithCharacterSet:[NSCharacterSet capitalizedLetterCharacterSet]];//大写
    
    NSLog(@"12 %@",[str stringByTrimmingCharactersInSet:set]);
    
         [set formUnionWithCharacterSet:[NSCharacterSet newlineCharacterSet]];//换行符
    
    NSLog(@"13 %@",[str stringByTrimmingCharactersInSet:set]);
    
         [set formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//空格换行
    
    NSLog(@"14 %@",[str stringByTrimmingCharactersInSet:set]);

6.字符串操作

1.截取字符串

NSString*string =@"sdfsfsfsAdfsdf";

string = [string substringToIndex:7];//截取下标7之后的字符串

NSLog(@"截取的值为:%@",string);

[string substringFromIndex:2];//截取下标2之前的字符串

NSLog(@"截取的值为:%@",string);


2.匹配字符串

NSString*string =@"sdfsfsfsAdfsdf";

NSRangerange = [stringrangeOfString:@"f"];//匹配得到的下标

NSLog(@"rang:%@",NSStringFromRange(range));

string = [string substringWithRange:range];//截取范围类的字符串

NSLog(@"截取的值为:%@",string);


3.分隔字符串

NSString*string =@"sdfsfsfsAdfsdf";

NSArray *array = [string componentsSeparatedByString:@"A"]; //从字符A中分隔成2个元素的数组

NSLog(@"array:%@",array); //结果是adfsfsfs和dfsdf


// 触摸背景,关闭键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    UIView *view = (UIView *)[touch view];
    if (view == self.scrollView) {
        [self.txtViewContent resignFirstResponder];
        [self.txtTel resignFirstResponder];
        [self.txtContact resignFirstResponder];
    }
}

或者是

在.h文件中,添加 - ( IBAction)backgroundTap:( id)sender;

    在.m中,添加

    - (IBAction)backgroundTap:(id)sender{

        [self.txtTele resignFirstResponder];

        [self.txtPerson resignFirstResponder];

        [self.txtViewContent resignFirstResponder];

    }

    然后将View的class改为UIControl,如图

    

    最后连接Control的Touch Down事件


修改navigation的push动画

导入QuartzCore/QuartzCore.h

        CATransition *transition = [CATransition animation];
        transition.duration = 1;
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        transition.type = @"cube";
        transition.subtype = kCATransitionFromTop;
        transition.delegate = self;
        [self.navigationController.view.layer addAnimation:transition forKey:nil];
        [self.navigationController pushViewController:sub2 animated:NO];


   


你可能感兴趣的:(实用代码)