經常使用開發技巧系列(一)

前言:    html

    在咱們iOS開發的過程當中,你要是知道一些特別的小技巧的話,實際上是能夠幫你省不少事的,固然這東西也不須要咱們專門去記,估計沒有幾個開發人員喜歡死記硬背,有須要,上網找,邊學邊用纔是技巧的正確的打開方式。畢竟,這東西也是一個隨着時間去積累總結的一個過程。這裏總結了一些平時積累到的一些開發的小技巧,其實有一些要是碰不到那個問題我也記不起來,因此打算一直更新下去,把碰到的小技巧一點點的都總結起來,把它最後作成一個系列。你要有什麼好的,歡迎在下面評論裏展現出來給你們看,你們相互學習。git

一:給凡是繼承與UIView的控件添加個別方向的圓角github

   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];

效果以下:web

二:微信朋友圈的這個跳轉你留意了嗎?微信

    你試着去點擊你微信朋友圈裏面找一條你發的朋友圈,點擊查看詳情,而後再點擊點贊數或者評論數那裏的按鈕,你就會看到像下面的翻轉效果。學習

   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:^{
        
    }];
    

 這裏是一個翻轉和翻頁效果的效果圖:     字體

                                

順便給你們一個MAC 端的GIF圖錄制的軟件:簡單,粗暴的 LICEcapurl

三:TextFile的各類自定義spa

    下面只是一個簡單的例子,好比它的提示的位置和字體顏色,效果以下:3d

 

下面仍是一些它的方法,還有許多在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];
相關文章
相關標籤/搜索