Chinaunix首页 | 论坛 | 博客
  • 博客访问: 189944
  • 博文数量: 60
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 385
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-19 21:43
个人简介

readonly

文章分类

全部博文(60)

文章存档

2013年(60)

我的朋友

分类: iOS平台

2013-02-20 00:46:28


1.iPhone 应用中获得时间差
CFTimeInterval time = CFAbsoluteTimeGetCurrent();
当然你也可以纪录开始接触和手指离开的时间,二者相减便是时差
time1 = [touch timestamp]; time2 = [touch timestamp]; 
时间差 = time2 - time1; 

2.iPhone播放简短音效的代码 
NSString *path = [[NSBundle mainBundle] pathForResource:@"BGM_Win" ofType:@"wav"]; 

SystemSoundID soundID; 

AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);

AudioServicesPlaySystemSound(soundID);




3.触发iPhone震动的代码
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);




4.生成随机 guid 串的代码
+ (NSString*) stringWithUUID {
CFUUIDRef    uuidObj = CFUUIDCreate(nil);//create a new UUID
//get the string representation of the UUID
NSString    *uuidString = (NSString*)CFUUIDCreateString(nil, uuidObj);
CFRelease(uuidObj);
return [uuidString autorelease];
}

5.异步发送 HTTP 请求的代码
NSMutableData* buf = [[NSMutableData alloc] initWithLength:0];
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
// 收到响应时, 会触发
- (void)connection:(NSURLConnection *)aConnection didReceiveResponse: (NSURLResponse *)aResponse;
// 你可以在里面判断返回结果, 或者处理返回的 http 头中的信息
// 每收到一次数据, 会调用一次
- (void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *) data;
// 因此一般来说,是
- (void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *) data
{
    [buf appendData:data];
}
// 当然 buffer 就是前面 initWithRequest 时同时声明的.
// 网络错误时触发
- (void)connection:(NSURLConnection *)aConn didFailWithError:(NSError*)error;
// 全部数据接收完毕时触发
- (void)connectionDidFinishLoading:(NSURLConnection *)aConn;

6.UIWebView加载gif图片 ——这样可以解决gif图片不能下载到本地加载,使用SDWebImage down也出现界面不流畅,卡的问题

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [homeTableCell getImageWidth:funnyPic], [homeTableCell getImageHeight:funnyPic])];
        webView.backgroundColor = [UIColor clearColor];
        //禁止webView的滑动 这样写主要是因为5.0以前的系统不能直接获取到webView.scrollView
        [(UIScrollView *)[[webView subviews] objectAtIndex:0] setBounces:NO];
        // 不让有白色的边,这个margin是必须的   
        NSString *html = [NSString stringWithFormat:@" ",funnyPic.imageUrl];
        [webView loadHTMLString:html baseURL:nil];
        [imageView addSubview:webView];

7.根据当前键盘的高度来设置UITextField的位置

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
}

- (void)keyboardWillShow:(id)sender {
    CGRect keyboardFrame;
    [[[((NSNotification *)sender) userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
    CGFloat keyboardHeight = CGRectGetHeight(keyboardFrame);
    [self.textImageView setFrame:CGRectMake(0, 416-keyboardHeight, 320, 45)];
}


8.view控件加边框

profileImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[profileImageButton.layer setMasksToBounds:YES];
[profileImageButton.layer setCornerRadius:4.0]; //设置矩形四个圆角半径
[profileImageButton.layer setBorderWidth:1.0];   //边框宽度
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){225.0/255.0, 225.0/255.0, 225.0/255.0, 1.0 });
[profileImageButton.layer setBorderColor:colorref];//边框颜色


9.单独设置圆角

[iconImage.layer setCornerRadius:4.0];
[iconImage setClipsToBounds:YES];


10.时区返回格式为数字(-12—+12)

-(NSString *)getTimeZone{ 
    NSString *zone = [[NSTimeZone systemTimeZone] description];//Europe/Berlin//       America/New_York//   Asia/Harbin
    //这三个可以用来测试exp:NSString *zone = [[NSTimeZone timeZoneWithName:@"America/New_York"] description];
    NSString *time = [[zone componentsSeparatedByString:@"offset "] objectAtIndex:1];
    int inv = [time intValue];
    int result = inv / (60 * 60);
    if (result>0) {
        return [NSString stringWithFormat:@"+%d", result];
    }
    return [NSString stringWithFormat:@"%d", result];
}

11.检测iphone插入/拔出耳机事件

void audioRouteChangeListenerCallback (
void *inUserData,
AudioSessionPropertyID inID,
UInt32 inDataSize,
const void *inData)
{
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
CFStringRef state = nil;
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute
,&propertySize,&state);

NSLog(@"%@",(NSString *)state);//return @"Headphone" or @"Speaker" and so on.
}
- (void)viewDidLoad {
[super viewDidLoad];

AudioSessionInitialize (NULL, NULL, NULL, NULL);
OSStatus status = AudioSessionAddPropertyListener(
kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback,self);
//if(status == 0){//ok;}
}


阅读(1442) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~