iOS 龍的天空 (Obj-c)

聯(lián)系方式


Head

  • 這個APP是我的第一個獨立開發(fā)的作品,主要是為了方便我自己使用(當(dāng)然也方便了別人)╮(╯_╰)╭,使用的是object-c寫的。是一個論壇網(wǎng)站(龍的天空)的APP實現(xiàn)。有興趣的可以去上面觀摩一下。APP Store
  • 這篇文章主要是介紹下本APP的一些項目搭建與功能實現(xiàn)

About

  • 工具是用的Xcode 7.3,抓接口是用的青花瓷和chrome自帶的開發(fā)者工具。PS:其實chrome調(diào)試挺好用的- 項目啟動是從4月中旬開始的,然后隔三差五的花個1到2小時寫寫,中間停了一個月的時間,到7月底結(jié)束,滿打滿算的話,大概真正花了一個半月的時間吧(其實主要有一些時間花在找圖標(biāo),抓接口上了),期間推翻界面重新設(shè)計了一次界面,浪費了一些時間。

先來幾張效果圖吧

  • 首頁
    首頁.gif
  • 板塊
    板塊.gif
  • 熱門
    熱門.gif
  • 消息
    消息.gif
  • 用戶
    用戶.gif
  • 帖子內(nèi)容
    帖子.gif
  • 用戶交互
    用戶交互.gif

1.首頁
首頁.png

  • 布局使用的UITableView,自定義UITableViewCell樣式。
  • 實現(xiàn)如圖所示cell距離上下左右間隔樣式,只需在自定義cell的類里重寫setFrame方法。如下所示:
- (void)setFrame:(CGRect)frame{ 
static CGFloat margin = 10;  
frame.origin.x = margin;    
frame.size.width -= 2 * frame.origin.x;  
frame.origin.y += margin;  
frame.size.height -= margin;   
[super setFrame:frame];}

  • 若要實現(xiàn)點擊cell時的背景也與cell樣式相同,如圖:
    點擊cell.png

    可以自定義cell的selectedBackgroundView,代碼如下:
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, 0, self.frame.size.width-20, self.frame.size.height)];
view.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.00];
view.layer.cornerRadius = 15;
view.layer.masksToBounds = YES;
self.selectedBackgroundView = view;

2.幾種cell的自定義

A.png

B.png
  • 主要分這兩種,其余的都很簡單。
  • A圖主要利用了UILableattributedText屬性,同時將網(wǎng)絡(luò)請求回的帶HTML標(biāo)簽的字符串通過NSHTMLTextDocumentType轉(zhuǎn)換即可,無需手動截取匹配標(biāo)簽,省去了很多麻煩。代碼如下:

string = [NSString stringWithFormat:@"<head><style>img{max-width:14;max-height:14}</style></head>%@",string];
NSMutableAttributedString * messageAttrStr = [[NSMutableAttributedString alloc] initWithData:[string dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil]; ```

  • B圖則在cell中放置了幾個UILable,手動截取字符串再賦值給UILable.text即可

Time.png
  • cell中的時間顯示如上圖
  • 將返回的時間戳(類似于1471934323這種格式)進行相應(yīng)的格式化即可,代碼如下,并做好了相應(yīng)的注釋:
-(void)setTimeText:(NSInteger)dataLine{   
 NSString * timeString;  
  NSDate * todayDate = [NSDate date];//當(dāng)前時間    
NSDate * pastDate = [NSDate dateWithTimeIntervalSince1970:dataLine];//發(fā)帖時間
    NSDateFormatter * format = [[NSDateFormatter alloc] init];  
  NSCalendar * calendar = [NSCalendar currentCalendar]; 
   NSInteger unit =  NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; 
   NSDateComponents * comDate =  [calendar components:unit fromDate:pastDate toDate:todayDate options:NSCalendarWrapComponents];//時間差值的組件
  if (comDate.year < 1) {//判斷是否今年   
        if ([calendar isDateInToday:pastDate]) {//判斷是否今天   
           if (comDate.hour >= 1) {//判斷是否大于1小時 
               format.dateFormat = @"今天HH:mm";             
               timeString = [format stringFromDate:pastDate];      
            }else if(comDate.hour < 1 && comDate.minute >= 1){            //判斷是否小于1小時     
               timeString = [NSString stringWithFormat:@"%2ld分鐘前",(long)comDate.minute];      
            }else if(comDate.hour < 1 && comDate.minute < 1 && comDate.second >= 0){//判斷是否在一分鐘內(nèi)
                timeString =@"剛剛";         
             }     
        }else if ([calendar isDateInYesterday:pastDate]){  //判斷是否昨天      
          format.dateFormat = @"昨天HH:mm";       
           timeString = [format stringFromDate:pastDate];     
        }else{ // 前天之前       
          format.dateFormat = @"MM-dd HH:mm";       
          timeString = [format stringFromDate:pastDate];     
         }  
  }else{ 
       format.dateFormat = @"yyyy-MM-dd HH:mm";
       timeString = [format stringFromDate:pastDate];  
  }  
  _time.text = timeString;
}

3.消息界面的搭建

消息.png
  • 主要分為上下兩個部分,上面可以點擊,若按鈕超出范圍可以滾動,下面可以滑動切換
  • 紫色部分利用UIScrollView來搭建,紅色部分利用UICollectionView來搭建。
  • 紫色部分按鈕的文字需要在viewWillAppear:方法里實現(xiàn),否則可能會出現(xiàn)相應(yīng)視圖未布局完成,文字不顯示。
  • 紅色部分設(shè)置UICollectionViewFlowLayout布局與紅色大小相同,水平滾動,間距為0即可
  • 當(dāng)點擊按鈕時會將前一個選中狀態(tài)的按鈕取消,再給當(dāng)前按鈕添加選中狀態(tài),然后將UICollectionView對象的contentOffset給設(shè)置成按鈕對應(yīng)的tag乘以屏幕寬度。
  • 若是滑動紅色界面,則在UICollectionView對象滑動完成后的scrollViewDidEndDecelerating:里調(diào)用設(shè)置按鈕的方法即可。

4.板塊的業(yè)務(wù)邏輯實現(xiàn)

板塊.png
  • 這個界面的UI搭建不難,主要是內(nèi)部數(shù)據(jù)的請求處理比較繁瑣- 首先要請求我的已關(guān)注列表,再請求所有板塊列表,最后請求板塊的信息數(shù)據(jù),網(wǎng)絡(luò)請求使用的是AFNetworking框架。
  • AFNetworking內(nèi)部請求是異步操作,所以需要解決請求完所有的數(shù)據(jù)再刷新的問題,但所有數(shù)據(jù)請求完,需要消耗一定的時間,會給人有點慢的感覺,因為內(nèi)部需要做循環(huán)截取數(shù)據(jù)的操作。所以使用了數(shù)據(jù)歸檔來緩解這個問題,只需要第一次請求的時候是全部請求,以后只要不清空緩存,就只需要請求關(guān)注列表和信息列表,同時,因為有了板塊列表數(shù)據(jù),即使網(wǎng)絡(luò)不好時刷新數(shù)據(jù),也不會顯示空白,讓人等的心急。

5.搜索界面

搜索.png
  • 分別是3個UIView,UIView的紫色部分是UILable,綠色部分是UIButton
  • 當(dāng)搜索條文字改變就會實時調(diào)用按鈕的setTitle: forState來改變文字,不能使用titleLabel.text來修改,無效!

5.1搜索帖子列表

Paste_Image.png
  • 搜索的文字會標(biāo)紅,使用的是NSMutableAttributedString來處理字符串,將相應(yīng)的字符的位置利用Key:NSForegroundColorAttributeName來設(shè)置顏色,再將富文本整體賦值給UILable的attributedText即可

6.發(fā)帖回帖

  1. 發(fā)帖


    發(fā)帖.png
  2. 回帖


    回帖.png
  • 回帖是將發(fā)帖的標(biāo)題欄給隱藏了,再設(shè)置相應(yīng)的自動約束即可
  • 右上角放大縮小按鈕會根據(jù)當(dāng)前窗口的大小判斷來決定是否縮放,利用的是獲取鍵盤的高度的系統(tǒng)通知方法來控制。以下為代碼范例:
 #pragma mark - 當(dāng)鍵盤出現(xiàn)或改變時調(diào)用
- (void)keyboardDidShow:(NSNotification *)aNotification{ 
   _keyHidden=NO;  
  NSDictionary *userInfo = [aNotification userInfo]; 
  NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; 
  CGRect keyboardRect = [aValue CGRectValue];  
  CGFloat height = keyboardRect.size.height;  
  CGFloat y  = keyboardRect.origin.y;  
  _keyHeight = height; 
  _keyY = y;  
  if (self.post==nil) {    
    return; 
  } 
  if (self.post.message.isFirstResponder==YES||self.post.title.isFirstResponder==YES) {    
    if (self.post.frame.origin.y==0) {    
        [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{  
              self.post.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-self.keyHeight-topHeight);   
         } completion:nil];        
     }else{       
         CGFloat maxY =  self.post.frame.size.height+topHeight+self.post.frame.origin.y;  
          if (maxY>_keyY) {        
        [UIView animateWithDuration:0.1 animations:^{   
               CGRect ract = self.post.frame;    
               ract.origin.y=[UIScreen mainScreen].bounds.size.height-topHeight-(self.post.frame.size.height+_keyHeight); 
               self.postY = ract.origin.y;       
               self.post.frame = ract;        
        }];         
         }else{         
              self.postY = self.post.frame.origin.y;     
         }       
     } 
   }
}

  • 發(fā)帖回帖的內(nèi)容視圖是UITextView,裝載的是富文本內(nèi)容。當(dāng)點擊圖上按鈕添加鏈接
    Paste_Image.png
  • 可以添加鏈接:
    QQ20160823-0@2x.png
  • 然后會在發(fā)帖框內(nèi)顯示:
    Paste_Image.png
  • 這里有個小的注意點,當(dāng)添加了連接后,你直接在鏈接后面輸入文字,會將文字顏色和屬性更改成來鏈接文字的屬性,如圖:
    Paste_Image.png
  • 所以需要在鏈接文字的前后各加一個空格,同時設(shè)置空格的屬性為顏色黑色,字體大小等即可。代碼如下所示:
NSString * nullString = @" ";
urlText = [nullString stringByAppendingString:urlText];
urlText = [urlText stringByAppendingString:@" "];
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:urlText];
UITextPosition * beginning = weakSelf.post.message.beginningOfDocument;
UITextRange* selectedRange = weakSelf.post.message.selectedTextRange;
UITextPosition* selectionStart = selectedRange.start;
UITextPosition* selectionEnd = selectedRange.end;
NSInteger location = [weakSelf.post.message offsetFromPosition:beginning toPosition:selectionStart];
NSInteger length = [weakSelf.post.message offsetFromPosition:selectionStart toPosition:selectionEnd];
NSRange range = NSMakeRange(location, length);[weakSelf.post.message.textStorage insertAttributedString:attributeString atIndex:range.location];
[weakSelf.post.message.textStorage addAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:1.00 green:0.42 blue:0.42 alpha:1.00], NSLinkAttributeName:[NSURL URLWithString:URL],NSFontAttributeName:[UIFont systemFontOfSize:14]} range:NSMakeRange(range.location+1, urlText.length-2)];
[weakSelf.post.message.textStorage addAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:14]} range:NSMakeRange(range.location+urlText.length-1, 1)];
[weakSelf.post.message.textStorage addAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:14]} range:NSMakeRange(range.location, 1)];weakSelf.post.message.selectedRange=NSMakeRange(weakSelf.post.message.text.length,0);
  • PS:這里我是在Block回調(diào)里使用的,所以使用了__weak 類名 * weakSelf = self;來設(shè)置weakSelf關(guān)鍵字。

7.用戶界面
用戶界面.png

  • 這里分成了3塊來布局
    • 藍色部分
    • 又分成3塊來布局:
      Paste_Image.png
    • 當(dāng)紅色區(qū)塊無內(nèi)容時會隱藏
      hide.png
  • 黃色部分為5個自定義的UIButton,點擊會跳轉(zhuǎn)到對應(yīng)的控制器
  • 控制器與消息界面實現(xiàn)方式相同,共用一套視圖,可以點擊按鈕或滑動界面
    Paste_Image.png
  • 紫色部分在無內(nèi)容時也會隱藏
    Paste_Image.png
    Paste_Image.png

8.設(shè)置界面
Paste_Image.png

  • 設(shè)置界面最簡單,只需要固定的幾個cell就可以。
  • 這里介紹下清除緩存的功能實現(xiàn)
    • 首先是如何顯示緩存大小
    • 需要獲取緩存文件夾內(nèi)容的大小,這里需要注意的是要遍歷緩存文件夾內(nèi)所有的文件來累加得到總大小,不能直接獲取文件夾大小,直接獲取到的文件夾大小與真實大小不同(當(dāng)然用戶不知道,要是偷懶也可以)。代碼如下:
NSString  *Path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
- (NSInteger)getSizeOfPath:(NSString *)Path {
    NSFileManager *mgr = [NSFileManager defaultManager];
    NSArray *subpaths = [mgr subpathsAtPath:Path];
    NSInteger totalSize = 0;
    for (NSString *subPath in subpaths) {  
      NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
        NSInteger fileSize = (long)[[mgr attributesOfItemAtPath:filePath error:nil] fileSize];  
      totalSize += fileSize;  
  }  
  return totalSize;
}
  • 然后是刪除緩存,只要直接將緩存文件刪除即可,代碼如下:
NSString  *Path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
- (void)removePath:(NSString *)Path { 
   NSFileManager *mgr = [NSFileManager defaultManager]; 
   NSArray *subPaths = [mgr contentsOfDirectoryAtPath:Path error:nil]; 
   for (NSString *subPath in subPaths) {   
     NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];   
     [mgr removeItemAtPath:filePath error:nil];  
  }
}

9.具體帖子內(nèi)容界面
Paste_Image.png

9.1控件布局

  • 上部綠框點擊圖所示可跳出UIPickerView來選擇頁面
    Paste_Image.png
    Paste_Image.png
  • 下部紫框為UIView,分別放置了幾個按鈕
  • 功能如圖所示,一目了然,同時View 會根據(jù)當(dāng)前界面的位置來自動隱藏或顯示。
    Paste_Image.png

9.2內(nèi)容布局
Paste_Image.png

  • 內(nèi)容界面使用的是WKWebview,將HTML字符串截取合并后重新轉(zhuǎn)換成HTML格式字符串再交給WKWebview處理,同時將自定義的CSS文件一并賦值,即可控制內(nèi)容樣式。
  • 界面里的超鏈接或者頭像,都是通過截取WKWebview的代理方法
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler ;

里的url鏈接來進行判斷處理。

  • 有圖片的界面,可以點擊圖片進入圖片查看界面
    Paste_Image.png
    Paste_Image.png
  • 圖片有各種手勢,縮放,拖移,長按,短按等
  • 這里我本來是自己實現(xiàn)的縮放、拖移手勢,但不太完美,所以就用了一個別人寫的圖片手勢的代碼修改而來

VIPhotoViewDemo


10.其他

1.點擊Tabbar按鈕實現(xiàn)滑動到頂部或刷新

Paste_Image.png
首先是在自定義的UITabBarController里的UITabBarControllerDelegate的方法

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;```
里面來判斷當(dāng)前點擊的是哪個按鈕對應(yīng)的控制器,然后根據(jù)控制器的`view`的`contentOffset`來判斷是否已經(jīng)在頂部,不在則滑動到頂部,在則執(zhí)行刷新操作。

---
3.框架使用
- 網(wǎng)絡(luò)請求  [AFNetworking](https://github.com/AFNetworking/AFNetworking)
- 圖片下載  [SDWebImage](https://github.com/rs/SDWebImage)
- 數(shù)據(jù)解析  [MJExtension](https://github.com/CoderMJLee/MJExtension)
- HUD顯示 [SVProgressHUD](https://github.com/SVProgressHUD/SVProgressHUD)
- 刷      新   [MJRefresh](https://github.com/CoderMJLee/MJRefresh)
- 崩潰日志  [Bugly](https://github.com/BuglyDevTeam/Bugly-iOS)
- 然后框架導(dǎo)入使用的CocoaPods,不過最近Carthage比較流行,最近也在學(xué)習(xí)Carthage。
---
4.這是我的第一個正式且上架的APP,所以也一直在更新中,主要是在iPhone手機上使用,所以在iPad上顯示時會出現(xiàn)有些布局樣式不一樣,因為沒有進行iPad專門的適配,都是交給系統(tǒng)來做的。然后因為第一個嘛,所以怕不熟練,用的是最熟的OC寫的,不過接下來的幾個APP已經(jīng)轉(zhuǎn)swift了,swift寫起來還是很舒服的。

---
## 11.最后因為我當(dāng)初代碼是直接提交到國內(nèi)的Coding上去的,還是私有庫,同時因為懶癌,所以就沒有再上傳到github上,所以大家是看不到代碼了(話說代碼我寫的有些亂,也沒多少注釋。)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.badgeVaule氣泡提示 2.git終端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夾內(nèi)容...
    i得深刻方得S閱讀 4,990評論 1 9
  • 因為要結(jié)局swift3.0中引用snapKit的問題,看到一篇介紹Xcode8,swift3變化的文章,覺得很詳細...
    uniapp閱讀 4,877評論 0 12
  • 一 、使用git管理工程 1、可以使用OSChina遠程管理工程(免費) 2、可以使用GitHub網(wǎng)站進行遠程管理...
    YuGHo閱讀 2,582評論 1 3
  • 趁午休,做個導(dǎo)圖回顧周六上了一整天正面管教的課程。 在分享之前,我想所有的關(guān)系都是指向自己,所以回來覺察與愛護、調(diào)...
    Jessica_LHM閱讀 247評論 0 0
  • 我想這是對父母說的話。 “你們的孩子都不是你們的孩子,乃是生命為自己所渴望的兒女,他們借你而來卻不因你而來。他們雖...
    簡邱閱讀 2,231評論 0 0

友情鏈接更多精彩內(nèi)容