總結iOS中的常用開發技巧
前言:
在我們iOS開發的過程中,你要是知道一些特別的小技巧的話,其實是可以幫你省很多事的,當然這東西也不需要我們專門去記,估計沒有幾個開發人員喜歡死記硬背,有需要,上網找,邊學邊用才是技巧的正確的打開方式。畢竟,這東西也是一個隨著時間去積累總結的一個過程。這里總結了一些平時積累到的一些開發的小技巧,其實有一些要是碰不到那個問題我也記不起來,所以打算一直更新下去,把碰到的小技巧一點點的都總結起來,把它最后做成一個系列。你要有什么好的,歡迎在下面評論里展示出來給大家看,大家相互學習。
一:給凡是繼承與UIView的控件添加個別方向的圓角
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
label.backgroundColor = [UIColor blueColor];
CGRect rect = CGRectMake(0, 0, 100, 100);
CGSize size = CGSizeMake(10, 10);
// 圓角的位置
UIRectCorner corner = UIRectCornerTopLeft;
// 貝塞爾曲線 給矩形可添加圓角的方法
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:size];
// 創建shaplayer
CAShapeLayer * masklayer = [[CAShapeLayer alloc]init];
masklayer.frame = label.bounds;
// 設置路徑
masklayer.path = path.CGPath;
label.layer.mask = masklayer;
[self.view addSubview:label];
效果如下:
二:微信朋友圈的這個跳轉你留意了嗎?
你試著去點擊你微信朋友圈里面找一條你發的朋友圈,點擊查看詳情,然后再點擊點贊數或者評論數那里的按鈕,你就會看到像下面的翻轉效果。
TestViewController * con = [[TestViewController alloc]init];
// 下面的modalTransitionStyle屬性是一個枚舉類型的,定義了四種方式
/**
typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {
UIModalTransitionStyleCoverVertical = 0,// 默認的從下到上
UIModalTransitionStyleFlipHorizontal , // 翻轉
UIModalTransitionStyleCrossDissolve,// 漸顯
UIModalTransitionStylePartialCurl , // 翻頁
};
con.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:con animated:YES completion:^{
}];
// 返回和平常一樣的Dismiss
[self dismissViewControllerAnimated:YES completion:^{
}];</code></pre>
這里是一個翻轉和翻頁效果的效果圖:

順便給大家一個MAC 端的GIF圖錄制的軟件:簡單,粗暴的 LICEcap
三:TextFile的各種自定義
下面只是一個簡單的例子,比如它的提示的位置和字體顏色,效果如下:

下面還是一些它的方法,還有許多在TextFile的.h文件里面,大家可以去學習,只要我們繼承與TextFile,重寫它們的下面相應的方法即可:
//控制顯示文本的位置
-(CGRect)textRectForBounds:(CGRect)bounds
{
}
//控制清除按鈕的位置
-(CGRect)clearButtonRectForBounds:(CGRect)bounds
{
}
//控制編輯文本的位置
-(CGRect)editingRectForBounds:(CGRect)bounds
{
}
//控制placeHolder的位置,左右縮20
-(CGRect)placeholderRectForBounds:(CGRect)bounds
{
CGRect inset = CGRectMake(bounds.origin.x, bounds.origin.y+10, bounds.size.width-10, bounds.size.height);
return inset;
}
//控制placeHolder的顏色字體
-(void)drawPlaceholderInRect:(CGRect)rect
{
[[UIColor redColor]setFill];
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
attributes[NSForegroundColorAttributeName] = [UIColor redColor];
attributes[NSFontAttributeName] = [UIFont systemFontOfSize:14];
[self.placeholder drawInRect:rect withAttributes:attributes];
}
四:小結一下屬性字符串:
/**
* 字符屬性小結
NSString *const NSFontAttributeName; 字體 對應一個 UIFont 對象
NSString *const NSParagraphStyleAttributeName; 段落 對應的值是一個NSParagraphStyle對象
NSString *const NSForegroundColorAttributeName; 字體顏色 對應的值是一個UIColor 對象
NSString *const NSBackgroundColorAttributeName; 字體背景色 也是一個UIcolor對象
NSString *const NSLigatureAttributeName;連字符
NSString *const NSKernAttributeName;字間距 對應的值是一個NSNumber對象,默認 0
NSString *const NSStrikethroughStyleAttributeName;中間線 (字符串中間一條線)對應的值是一個NSNumber對象,默認值是NSUnderlineStyleNone
NSString *const NSUnderlineStyleAttributeName;下劃線 對應的值是一個NSNumber對象
NSString *const NSStrokeColorAttributeName;邊線顏色 對應的值是一個UIColor對象
NSString *const NSStrokeWidthAttributeName;邊線寬度 該屬性所對應的值是一個 NSNumber對象
NSString *const NSShadowAttributeName;(陰影)橫豎排版 對應的值是一個NSShadow對象。默認為 nil
NSString *const NSVerticalGlyphFormAttributeName; 搭配上面的陰影使用效果更好
*/
五:側滑手勢
這里說一下,要是在導航欄上,當你push到下一個界面的時候,你要是使用的是系統的返回方式的話,那你的應用是自己會支持側滑手勢,你可以自己試一下 ,但在很多的情況下,我們的返回按鈕是會自定義的。這個時候系統的側滑手勢就不在起作用,但側滑作為一個APP常見的也是一個用戶體驗很好的東西,建議大家還是給APP 加上來增強我們的用戶體驗。
// 下面的代碼寫在你整個項目的基類里面去
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//需要遵循一下手勢的代理
self.navigationController.interactivePopGestureRecognizer.delegate = self;
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
這里再說一下這個 FDFullscreenPopGesture 我覺得也很不錯,一個零行代碼的三方庫,你需要做的就是把它加到你的項目中間去就夠了!看看git上它的一個展示效果:

六:你想給你的WebView添加一個頭部視圖
其實做這個效果有很多很多的方式,你可以把你的WebView加到ScrollView上去,在給它加一個頭部的view,這樣也沒有問題,但其實大家也都知道,WebView自己本身就是包含有ScrollView,那你有沒有想過,把它的頭部直接添加到自己包含的ScrollView呢?其實也是沒問題的,它包含的ScrollView里面有一個UIWebBrowserView,它是來顯示WebView上面的網頁內容的,所以你只要拿到它,改變它也就OK了,看看下面的代碼:
// scrollView里面是一個UIWebBrowserView(負責顯示WebView的內容),你可以通過調整它的位置來給你的webview添加一個頭部。
UIView *webBrowserView = self.ZXwebView.scrollView.subviews[0];//拿到webView的webBrowserView
self.backHeadImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height*2/3.0)];
//[_backHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
[_backHeadImageView setImage:[UIImage imageNamed:@"1.jpg"]];
//把backHeadImageView插入到webView的scrollView下面
//[self.ZXwebView insertSubview:_backHeadImageView belowSubview:self.ZXwebView.scrollView];
//把backHeadImageView添加到你webView的scrollView上面,這兩個效果不一樣,你可以自己試一下。
[self.ZXwebView.scrollView addSubview:_backHeadImageView];
//更改webBrowserView的frame向下移backHeadImageView的高度,使其可見
CGRect frame = webBrowserView.frame;
frame.origin.y = CGRectGetMaxY(_backHeadImageView.frame);
webBrowserView.frame = frame;
// 加載網頁
[_ZXwebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];

七:TableView的尾部處理
TableView 這個我們也有一個常見的,比如說你創建了的cell,你用到了十個,那剩下的將用內容空白但cell還是會存在的形式出現,像下面這樣子,你覺得很丑,這時候怎么辦?

這個其實很簡單,你只需要處理一下 TableView 的尾部視圖,賦一個初始化的View給它就OK了。
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
// 在這里處理,你可以試著看看有這句和沒有這句的區別。
self.tableView.tableFooterView = [[UIView alloc]init];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
cell.textLabel.text = @"張旭你個混蛋";
return cell;
}
八:導航欄那些事
導航欄上面的那些事兒的話我先給大家一個鏈接,總結的比較的全面,具體知識大家而已去看看這個鏈接里面的內容,我們就說點小技巧,簡單的,怎樣把導航設置成透明。
上面的導航就是透明的,只是它的那條線還在,這個我們也可以隱藏的,看下面兩句代碼 :
// 設置導航透明
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
// 隱藏導航下面的線條
// self.navigationController.navigationBar.shadowImage = [UIImage new];
還有,導航這一塊的,比如根據下面滑動視圖的滑動來改變導航的透明度這類利用 Runtime 解決的問題,前連天在總結 Runtime 的時候有說過怎么做,感興趣的朋友可以去翻翻,鏈接這里。
九:GCD寫一個定時器其實很簡單的
看下面代碼的自動提示,直接敲上去你不需要再寫太多了,但要注意你的在這里吧time寫成全局變量或者在 dispatch_source_set_event_handler里面再去調用一次你的time。

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 這里只是一個延時效果,記得把time寫成全局變量,防止在ARC下被提前釋放
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
NSLog(@"12321431234");
});
dispatch_resume(timer);
十:判斷界面是push還是present出現的
這里就不多說了,直接上代碼
NSArray *viewcontrollers=self.navigationController.viewControllers;
if (viewcontrollers.count > 1){
if ([viewcontrollers objectAtIndex:viewcontrollers.count - 1] == self){
//push方式
[self.navigationController popViewControllerAnimated:YES];
}
}
else{
//present方式
[self dismissViewControllerAnimated:YES completion:nil];
}
十一:禁止鎖屏
這個就比較的常見了,最近公司也準備做直播類的APP,相信這個肯定也會用的到的;
[UIApplication sharedApplication].idleTimerDisabled = YES;
// 或者
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
來自:http://www.cnblogs.com/taoxu/p/5813039.html