iphone develop tips

來源:互聯網
上載者:User

一些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。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.