iphone develop tips

来源:互联网 发布:windows 手写笔记软件 编辑:程序博客网 时间:2024/05/02 00:23


一些iphone的小细节,小技巧
1.时间戳
[cpp] view plaincopy


NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];  
NSString* timeStamp = [NSString stringWithFormat:@"hex=%x,dec=%d,milSec=%.3f",(NSInteger)interval,(NSInteger)interval,interval];  


timeStamp 结果:
[cpp] view plaincopy


hex=4fd7fd8a,dec=1339555210,milSec=1339555210.191  
2.UIView圆角实现
包含QuartzCore.framework ,然后包含头文件#import <QuartzCore/QuartzCore.h>
使用以下的 两行代码既可。
[cpp] view plaincopy


tmpView.layer.cornerRadius  = 10.0;  
tmpView.layer.masksToBounds = YES;  
[cpp] view plaincopy


UIView* tmpView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];  
tmpView.backgroundColor = [UIColor grayColor];  
tmpView.layer.cornerRadius  = 10.0;  
tmpView.layer.masksToBounds = YES;  
[self.view addSubview:tmpView];  
[cpp] view plaincopy


设置边框颜色,
UIView* view = [[UIView alloc] initWithFrame:CGRectZero];
view.layer.borderWidth = 1.0;
view.layer.borderColor = [UIColor redColor].CGColor;


3.用图片平铺做背景
[cpp] view plaincopy


UIView* tmpView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];  
tmpView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];  


4.用图片拉伸做背景
[cpp] view plaincopy


UIImageView* tmpView2 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];  
tmpView2.image = [UIImage imageNamed:@"bg.png"];  


UIImageView 的ContentMode默认是 UIViewContentModeScaleToFill,即拉伸,可以选择居中,左对齐,右对齐来不拉伸显示。
5.使用UIView的transform属性做旋转等动画,在使用完成后需要重新设置transform为默认 CGAffineTransformIdentity,否则下次setframe等操作时,现实错误。
6.使用UILabel时,设置setFrame需要保证CGrect 的值未整数,不能为浮点,否则系统自动做 抗锯齿,现实模糊。图片存在同样问题。
7.播放gif动画
[cpp] view plaincopy


CGRect frameRect = CGRectZero;  
frameRect.size = [UIImage imageNamed:@"callshow_2.gif"].size;  
NSString*gifPath = [[NSBundle mainBundle] pathForResource:@"callshow_2" ofType:@"gif"];  
NSData* gifData = [NSData dataWithContentsOfFile:gifPath];  
UIWebView* webView = [[UIWebView alloc] initWithFrame:frameRect];  
[webView loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];  
[self.view addSubview:webView];  
8.隐藏tabbar之后,底部出现一个白条,去除方法
http://stackoverflow.com/questions/1982172/iphone-is-it-possible-to-hide-the-tabbar
隐藏:
[cpp] view plaincopy


CGRect bounds = [[UIScreen mainScreen] bounds];  
CGRect tabBarFrame = self.tabBarController.tabBar.frame;  
  
self.tabBarController.view.frame = CGRectMake(0,0,bounds.size.width,bounds.size.height+tabBarFrame.size.height);  
self.tabBarController.tabBar.hidden = YES;  
显示:
[cpp] view plaincopy


CGRect bounds = [[UIScreen mainScreen] bounds];  
self.tabBarController.view.frame = CGRectMake(0,0,bounds.size.width,bounds.size.height);      
self.tabBarController.tabBar.hidden = NO;  


9.在导航栏中间加入segmentedControl,
[cpp] view plaincopy


NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"111",@"222",nil];    
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedArray];    
segmentedControl.frame = CGRectMake(80, 0, 160.0, 30.0);  
segmentedControl.selectedSegmentIndex = 0;  
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;  
[segmentedControl addTarget:self action:@selector(segmentButtonPressed:) forControlEvents:UIControlEventTouchUpInside];  
self.navigationItem.titleView = segmentedControl;  


其中 self.navigationItem.titleView 表示导航栏中间的view,另外,如果左右两边分别也有UIBarButtonItem,通过 self.navigationItem.leftBarButtonItem  和 self.navigationItem.rightBarButtonItem 来引用。
10,根据地址查看对应符号和地址
dwarfdump --lookup 0x0000641c -arch armv6 EmergencyNumbers.app.dSYM


11.动态设置UITextField是否安全输入
[cpp] view plaincopy


NSString* pass = passWordTextField_.text;  
passWordTextField_.enabled = NO;  
passWordTextField_.secureTextEntry = !isShowPassWord_;  
passWordTextField_.enabled = YES;  
[passWordTextField_ becomeFirstResponder];  
passWordTextField_.text = pass;  


11,点击事件穿透父视图,让子图响应事件
参考:http://cocoaheads.tumblr.com/post/2130871776/ignore-touches-to-uiview-subclass-but-not-to-its
要将父视图的userInteractionEnabled 设为 YES
[cpp] view plaincopy


-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event {  
    id hitView = [super hitTest:point withEvent:event];  
    if (hitView == self) return nil;  
    else return hitView;  
}  


12,让视图只响应 某个区域的点击事件,利用11中提到方法
以下代码实现让view只响应CGRectMake(0, 0, 160, 40) 区域的事件
[cpp] view plaincopy


- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event  
{  
    id hitView = [super hitTest:point withEvent:event];  
    if (hitView == self &&!CGRectContainsPoint(CGRectMake(0, 0, 160, 40), point))   
    {  
        NSLog(@">>>point=%@",NSStringFromCGPoint(point));  
        return nil;  
    }  
    else   
        return hitView;  
}  


13,屏幕截图
[cpp] view plaincopy


CGContextRef context = UIGraphicsGetCurrentContext();  
// check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+)  
if (UIGraphicsBeginImageContextWithOptions != NULL)  
    UIGraphicsBeginImageContextWithOptions(size,NO,0.0);  
else  
    // iOS is < 4.0   
    UIGraphicsBeginImageContext(size);  
[self.layer renderInContext:context];  
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();  
UIGraphicsEndImageContext();  
//然后将该图片保存到图片图  
UIImageWriteToSavedPhotosAlbum(image,self,nil,nil);  


14,由颜色生成图片
[cpp] view plaincopy


CGContextRef context = UIGraphicsGetCurrentContext();  
// check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+)  
if (UIGraphicsBeginImageContextWithOptions != NULL)  
    UIGraphicsBeginImageContextWithOptions(size,NO,0.0);  
else  
    // iOS is < 4.0   
    UIGraphicsBeginImageContext(size);  
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);  
CGContextFillRect(context, rect);  
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();  
UIGraphicsEndImageContext();  


15.UIImageView 点击效果


[cpp] view plaincopy


UIImageView *imgView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0,320, 44)];  
imgView.userInteractionEnabled=YES;  
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];  
[singleTap setNumberOfTapsRequired:1];  
[imgView addGestureRecognizer:singleTap];  
[singleTap release];  
16. 移动光标到末尾
[cpp] view plaincopy


UITextView * m_textInput;  
//设置光标到输入文字的末尾  
NSUInteger length = m_textInput.text.length;  
m_textInput.selectedRange = NSMakeRange(length,0);    




17.Xcode添加doxygen注释
http://blog.chukong-inc.com/index.php/2012/05/16/xcode4_fast_doxygen/
http://blog.csdn.net/ch_soft/article/details/7633300


18 UIColor 反色
[cpp] view plaincopy


+ (UIColor*)reverseColor:(UIColor*)color  
{  
    const CGFloat *componentColors = CGColorGetComponents(color.CGColor);  
    return [UIColor colorWithRed:(255 - componentColors[0])  
                           green:(255 - componentColors[1])  
                            blue:(255 - componentColors[2])  
                           alpha:componentColors[3]];  
      
}  


19 测试方法:
1.BIT_ViewController中viewDidLoad中注释掉[BI_RootViewSDK create:textField_];
2.编译出BaiduInputMethod.dylib
3.dsymutil BaiduInputMethod.dylib产生BaiduInputMethod.dylib.dSYM文件夹
4.XCode->Product->Profile->Time Profile
5.启动Instruments后,如果找不到符号,请Stop掉Instruments,然后File->Re-Symbolicate Document,再次启动就可以了


20 一个消息被多次发送,如何提高速度
当一个消息要被发送给某个对象很多次的时候,可以直接使用methodForSelector:来进行优化,比如下述代码:
[cpp] view plaincopy


//////////////////////////////////////////////////////////////  
void (*setter)(id, SEL, BOOL);  
int i;  
  
setter = (void (*)(id, SEL, BOOL))[target  
     methodForSelector:@selector(setFilled:)];  
for ( i = 0; i < 1000, i++ )   
     setter(targetList[i], @selector(setFilled:), YES);  
//////////////////////////////////////////////////////////////  


其中,methodForSelector:是由Cocoa Runtime System提供的,而不是Objective-C本身的语言特性。这里需要注意转换过程中函数类型的正确性,包括返回值和参数,而且这里的前两个参数需要显示声明为id和SEL。



	
				
		
原创粉丝点击