一些小知識點(3)

作者:Love@YR
鏈接:http://blog.csdn.net/jingqiu880905/article/details/52055635
請尊重原創,謝謝!

  1. 如何讓webview頁面不能下拉,不要露出下拉後面的背景和滑到底之後上滑後面的背景?
    self.webView.scrollView.bounces = NO;

  2. vi文件保存退出命令 先按esc鍵 然後輸入:wq ,編輯時刪除當前光標所在位置的字符用x,readonly option is set add to override !時用sudo vi 不要用vi

  3. UIWebView中禁止長按響應和放大鏡等默認交互行爲:
    http://my.oschina.net/hmj/blog/111344
    文中說了兩種方法,一種是直接在css裏去禁止:

body.disable-default-action
{
    -webkit-touch-callout:none ;
    -webkit-user-select:none ;
}

一種是在webViewDidFinishLoad裏禁止。
4. 預估行高可以減少行高計算方法被執行的次數

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
  return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  //一大堆代碼
  return size.height;
}

雖然estimatedHeightForRowAtIndexPath也被執行了很多次,但是方法體裏直接return 一個定值,不耗時。如果不加入這個方法,heightForRowAtIndexPath就被執行很多次,每次又一大堆代碼。
5. 關於build settings裏的配置
涉及到路徑的,因爲此設置是寫在工程名.xcodeproj裏,所以其他都是在工程名的文件夾裏,如Prefix Header :JSCallOC/PrefixHeader.pch
6. debug模式改release模式。edit scheme- run- info build configuration改release,debug executable勾掉。
7. 屏蔽NSLog

#ifdef DEBUG
#define NJLog(...) NSLog(__VA_ARGS__)
#else
#define NJLog(...)
#endif

然後在代碼裏不用NSLog用NJLog,這樣在非debug模式下就不會打印log

或者這種:

#define DISABLE_LOG

#ifndef DISABLE_LOG
#define ENABLE_LOG
#endif

#if defined(ENABLE_LOG)
#define TLog(format, ...) NSLog((@"%s@%d: " format), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#define CLog(format, ...) NSLog((@"%s@%d: " format), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)

#else
#define NSLog(format, ...)
#define TLog(format, ...)
#define CLog(format, ...)
#endif

這個意思就是如果定義了禁止log,那麼代碼中NSLog就不再打印(還有一些其他你自定義的log)否則NSLog沒被重定義,跟debug, release無關。
8. 關於各種空:
首先了解下C語言的各種類型:http://www.cnblogs.com/onedime/archive/2012/11/21/2780149.html
各種空類型參考:
http://blog.csdn.net/tianjf0514/article/details/18054365
http://www.mamicode.com/info-detail-649183.html

nil: NSObject對象空指針
Nil: NSObject類空指針(比如說類存在則是個非Nil的class,不存在則是Nil。就比如initWithNibName:bundle:這個方法,if (self = [super initWithNibName:NULL bundle:NULL]) {。。。})
NULL: 指向其他類型(如:基本類型、C類型int * char 、結構體Ivar 、枚舉、id類型(就比如上面的例子的類型就是可能是空也可能是字串,所以用了id類型))的空指針
NSNull:通常表示集合中的空值,爲了佔位的。不能用空對象。
9. C語言中的const
const char * ch
const在*前面表示這是一個字符串常量指針,它所指向的字符串不能被修改,但是這個指針的指向可以改變,它和char const *ch是一樣的,比如:

const char *ch="123";//指向常量字符串123,123的內容不能被修改,但可以改變ch的指向
ch="Hello World!";//可以改變ch的指向

如果const在*的後面,則表示這是一個指針常量,它的指向不能被修改,但可以修改批所指向的內容,比如:

char c[] = "123";
char * const ch=c;//ch的指向不能被修改,但它所指向的內容視情況可以被修改,
//比如由於c是個字符數組,所以,可以通過ch修改c數組的值
ch[0]='4';//修改c[0]爲4,c變成"423


10. objc_msgSend 報錯:use of undeclared identifier ‘objc_msgSend’ 修錯方法:#import <objc/message.h>
11. 關於模擬器運行時上下出現很大塊黑邊:
一般是運行老的代碼,老的xcode生成的代碼出現這個問題。改成創建一個Launch Screen.storyboard,然後在target那邊設置app icons and launch images那裏選擇Launch Screen File即可~
12. 關於截圖
下面這個例子是截某個tableview的前5行(若小於5行則截所有行)的圖。(section可能不止一個)

-(void)screenshot:(id)sender
{

   UIImage *img= [self captureScrollView:self.parentTableView];//拿到圖片
    UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);//保存到相冊
}


- (UIImage *)captureScrollView:(UITableView *)scrollView{


    NSInteger totalRowCount =0;
    CGFloat fifthRowMaxY=0;
    for (int section = 0; section<_parentTableView.numberOfSections; section++) {
        NSInteger rowCountInSection = [_parentTableView numberOfRowsInSection:section];
        if (totalRowCount+rowCountInSection<5) {
            if (section==_parentTableView.numberOfSections-1) {
                NSIndexPath *cellIndexPath = [NSIndexPath indexPathForRow:rowCountInSection-1 inSection:section];
                CGRect rectInTableView = [_parentTableView rectForRowAtIndexPath:cellIndexPath];
                fifthRowMaxY = CGRectGetMaxY(rectInTableView);
                break;//跳出外層循環
            }
            totalRowCount+=rowCountInSection;
            continue;
        }
        else{
            for (int row=0;row<rowCountInSection; row++) {
                NSIndexPath *cellIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
                if (totalRowCount+row==5-1) {
                    CGRect rectInTableView = [_parentTableView rectForRowAtIndexPath:cellIndexPath];
                    fifthRowMaxY = CGRectGetMaxY(rectInTableView);
                    break;//跳出內層循環
                }
            }
            break;//跳出外層循環
        }
    }


    UIImage* image = nil;
    CGSize size = CGSizeMake(scrollView.contentSize.width, fifthRowMaxY);//不截全部截半截
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);//畫板的高度與內容的高度相同fifthRowMaxY則不會出現多餘空白
//    UIGraphicsBeginImageContext(scrollView.contentSize);
    {
        CGPoint savedContentOffset = scrollView.contentOffset;
        CGRect savedFrame = scrollView.frame;
        scrollView.contentOffset = CGPointZero;
        scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, fifthRowMaxY);
        NSLog(@"scrollView.contentOffset is %@, scrollView.frame is %@",NSStringFromCGPoint(savedContentOffset),NSStringFromCGRect(scrollView.frame));

        [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
        image = UIGraphicsGetImageFromCurrentImageContext();
        //重置爲之前的位置
        scrollView.contentOffset = savedContentOffset;
        scrollView.frame = savedFrame;
    }
    UIGraphicsEndImageContext();

    if (image != nil) {
        return image;
    }
    return nil;
}

…..
13. 關於使用autoLayout時有scrollview時如何設定
http://www.cocoachina.com/ios/20141118/10242.html

總結下來就是首先設置scrollview的frame,即其相對於其superview的座標和寬高,然後重點是scrollview裏的subview來定住scrollview的contentview的寬,高。
首先subview的寬高需要定住,而不是設置成相對於scrollview的contentview的寬高
其次scrollview相對於其subview的top,bottom,leading,trailing要有,這樣才能定住scrollview的contentview的高度和寬度。如下圖:
這裏寫圖片描述
14. ios app的生命週期:
http://blog.csdn.net/totogo2010/article/details/8048652/
15. viewController的生命週期:
http://www.cnblogs.com/xjy-123/p/5271063.html
http://www.cocoachina.com/industry/20121120/5134.html
16. 給工程添加pch文件:
http://www.jianshu.com/p/e6e0e3bbbf38

發佈了56 篇原創文章 · 獲贊 28 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章