iOS ?零碎知識點

【雜亂中的雜亂】

MRC和ARC混編設(shè)置方式

 -fno-objc-arc
1、調(diào)用代碼使APP進入后臺,達到點擊Home鍵的效果
[[UIApplication sharedApplication] performSelector:@selector(suspend)];
2、帶有中文的URL處理。
http://static.tripbe.com/videofiles/視頻/我的自拍視頻.mp4
NSString *path  = (__bridge_transfer NSString*)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,                                                                                                         
 (__bridge CFStringRef)model.mp4_url,                                                                         
CFSTR(""),                                                                                                    
CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
3、強制讓App直接退出(非閃退,非崩潰)
    - (void)exitApplication {
        AppDelegate *app = [UIApplication sharedApplication].delegate;
        UIWindow *window = app.window;
        [UIView animateWithDuration:1.0f animations:^{
            window.alpha = 0;
        } completion:^(BOOL finished) {
            exit(0);
        }];
    }
4、刪除NSUserDefaults所有記錄
//方法一
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

//方法二
- (void)resetDefaults {
    NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
    NSDictionary * dict = [defs dictionaryRepresentation];
    for (id key in dict) {
        [defs removeObjectForKey:key];
    }
    [defs synchronize];
}
5、相對路徑和絕對路徑

iOS程序有固定的文件訪問限制,只能在自己的沙盒內(nèi)。
相對路徑位置是相對當(dāng)前項目的路徑,無論你的項目在哪臺電腦上這個項目都可以正常運行,而絕對路徑的項目路決只對當(dāng)前電腦的位置有效,換一臺電腦路徑就會出現(xiàn)錯誤。
相對路徑:(SRCROOT)代表的是項目根目錄下。 絕對路勁:(PROJECT_DIR)代表的是當(dāng)前工程文件夾目錄,也就是整個項目。

讀取文件
NSString *newPath=[[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];
NSFileHandle *fileHandle=[NSFileHandle fileHandleForReadingAtPath:newPath];
NSString *str=[NSString stringWithContentsOfFile:newPath encoding:NSUTF8StringEncoding error:nil];
// 獲取程序Documents目錄路徑
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// 獲取程序app文件所在目錄路徑
NSHomeDirectory();

// 獲取程序tmp目錄路徑
NSTemporaryDirectory();

// 獲取程序應(yīng)用包路徑
[[NSBundle mainBundle] resourcePath];
或
[[NSBundle mainBundle] pathForResource: @"info" ofType: @"txt"];

6、禁止鎖屏

默認(rèn)情況下,當(dāng)設(shè)備一段時間沒有觸控動作時,iOS會鎖住屏幕。但有一些應(yīng)用是不需要鎖屏的,比如視頻播放器。

[UIApplication sharedApplication].idleTimerDisabled = YES;
或
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
7、iOS跳轉(zhuǎn)到App Store下載應(yīng)用評分
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=APPID"]];
8、獲取第一響應(yīng)者
UIWindow * keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView * firstResponder = [keyWindow performSelector:@selector(firstResponder)];
9、判斷對象是否遵循了某協(xié)議
if ([self.selectedController conformsToProtocol:@protocol(RefreshPtotocol)])
{
     [self.selectedController performSelector:@selector(onTriggerRefresh)];
}
10、判斷view是不是指定視圖的子視圖
BOOL isView = [textView isDescendantOfView:self.view];
11、獲取一個類的所有子類
+ (NSArray *) allSubclasses
{
    Class myClass = [self class];
    NSMutableArray *mySubclasses = [NSMutableArray array];
    unsigned int numOfClasses;
    Class *classes = objc_copyClassList(&numOfClasses;);
    for (unsigned int ci = 0; ci < numOfClasses; ci++)
    {
        Class superClass = classes[ci];
        do{
            superClass = class_getSuperclass(superClass);
        } while (superClass && superClass != myClass);

        if (superClass)
        {
            [mySubclasses addObject: classes[ci]];
        }
    }
    free(classes);
    return mySubclasses;
}
12、防止scrollView手勢覆蓋側(cè)滑手勢
[scrollView.panGestureRecognizerrequireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
13、獲取手機安裝的應(yīng)用
Class c =NSClassFromString(@"LSApplicationWorkspace");
id s = [(id)c performSelector:NSSelectorFromString(@"defaultWorkspace")];
NSArray *array = [s performSelector:NSSelectorFromString(@"allInstalledApplications")];
for (id item in array)
{
    NSLog(@"%@",[item performSelector:NSSelectorFromString(@"applicationIdentifier")]);
    //NSLog(@"%@",[item performSelector:NSSelectorFromString(@"bundleIdentifier")]);
    NSLog(@"%@",[item performSelector:NSSelectorFromString(@"bundleVersion")]);
    NSLog(@"%@",[item performSelector:NSSelectorFromString(@"shortVersionString")]);
}
14、簡單的圖文混排
- (void)setupTextView 
{
// 富文本技術(shù):
// 1.圖文混排
// 2.隨意修改文字樣式
//    self.textView.text = @"哈哈4365746875";
//    self.textView.textColor = [UIColor blueColor];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"哈哈123456"];
 // 設(shè)置“哈哈”為藍色
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 2)];
[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(0, 2)];
[string addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];

// 設(shè)置“456”為紅色
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6, 2)];
[string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:24] range:NSMakeRange(6, 2)];
[string addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(6, 2)];

// 創(chuàng)建圖片圖片附件
NSTextAttachment *attach = [[NSTextAttachment alloc] init];
attach.image = [UIImage imageNamed:@"d_aini"];
attach.bounds = CGRectMake(0, 0, 15, 15);
NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];


[string appendAttributedString:attachString];

[string appendAttributedString:[[NSAttributedString alloc] initWithString:@"789"]];


 //圖文混排,設(shè)置圖片的位置
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(120, 120, 100, 110)];
    imageView.image = [UIImage imageNamed:@"image"];
    imageView.backgroundColor = [UIColor redColor];
    [self.view addSubview:imageView];
    CGRect rect = CGRectMake(100, 100, 120, 120);
    
    //設(shè)置環(huán)繞的路徑
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:rect];
    self.textView.textContainer.exclusionPaths = @[path];
self.textView.attributedText = string;

/**
 iOS 6之前:CoreText,純C語言,極其蛋疼
 iOS 6開始:NSAttributedString,簡單易用
 iOS 7開始:TextKit,功能強大,簡單易用
 */
}

效果圖

屏幕快照 2016-12-27 下午3.16.39.png
15、設(shè)備號
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];  
 CFShow(infoDictionary);  
// app名稱  
 NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];  
 // app版本  
 NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];  
 // app build版本  
 NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];  
  
  
      
    //手機序列號  
    NSString* identifierNumber = [[UIDevice currentDevice] uniqueIdentifier];  
    NSLog(@"手機序列號: %@",identifierNumber);  
    //手機別名: 用戶定義的名稱  
    NSString* userPhoneName = [[UIDevice currentDevice] name];  
    NSLog(@"手機別名: %@", userPhoneName);  
    //設(shè)備名稱  
    NSString* deviceName = [[UIDevice currentDevice] systemName];  
    NSLog(@"設(shè)備名稱: %@",deviceName );  
    //手機系統(tǒng)版本  
    NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];  
    NSLog(@"手機系統(tǒng)版本: %@", phoneVersion);  
    //手機型號  
    NSString* phoneModel = [[UIDevice currentDevice] model];  
    NSLog(@"手機型號: %@",phoneModel );  
    //地方型號  (國際化區(qū)域名稱)  
    NSString* localPhoneModel = [[UIDevice currentDevice] localizedModel];  
    NSLog(@"國際化區(qū)域名稱: %@",localPhoneModel );  
      
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];  
    // 當(dāng)前應(yīng)用名稱  
    NSString *appCurName = [infoDictionary objectForKey:@"CFBundleDisplayName"];  
    NSLog(@"當(dāng)前應(yīng)用名稱:%@",appCurName);  
    // 當(dāng)前應(yīng)用軟件版本  比如:1.0.1  
    NSString *appCurVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];  
    NSLog(@"當(dāng)前應(yīng)用軟件版本:%@",appCurVersion);  
    // 當(dāng)前應(yīng)用版本號碼   int類型  
    NSString *appCurVersionNum = [infoDictionary objectForKey:@"CFBundleVersion"];  
    NSLog(@"當(dāng)前應(yīng)用版本號碼:%@",appCurVersionNum); 
16、獲取現(xiàn)在UINavagition的第幾層
self.navigationController.viewControllers.count
17、高德地圖出現(xiàn)這個
屏幕快照 2017-01-05 上午10.35.22.png

把在模擬器上不該APP刪除,然后退出Xcode和模擬器

18、 放在某個父視圖的最上層
 [self.view bringSubviewToFront:label1];
19、獲取window
UIWindow *window = [UIApplication sharedApplication].keyWindow;
20、判斷數(shù)組 字典 對象是否為空
//判斷字符串是否為空
#define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )


//判斷數(shù)組是否為空
#define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)



//判斷字典是否為空
#define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allK



//判斷是否是空對象
#define kObjectIsEmpty(_object) (_object == nil \
|| [_object isKindOfClass:[NSNull class]] \
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))

21、移除俯視圖上面的所有子視圖
[MySuperView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
22、結(jié)構(gòu)體轉(zhuǎn)化為NSString
NSString *NSStringFromCGPoint(CGPoint point);  
NSString *NSStringFromCGVector(CGVector vector);  
NSString *NSStringFromCGSize(CGSize size);  
NSString *NSStringFromCGRect(CGRect rect);  
NSString *NSStringFromCGAffineTransform(CGAffineTransform transform);  
NSString *NSStringFromUIEdgeInsets(UIEdgeInsets insets);  
NSString *NSStringFromUIOffset(UIOffset offset);
23、字符串轉(zhuǎn) 結(jié)構(gòu)體
CGPoint CGPointFromString(NSString *string);  
CGVector CGVectorFromString(NSString *string);  
CGSize CGSizeFromString(NSString *string);  
CGRect CGRectFromString(NSString *string);   //我們在監(jiān)聽高度變化時需要用這個 ,獲取鍵盤的frame 值,就是從一個 string類型中取出 CGRect!  
CGAffineTransform CGAffineTransformFromString(NSString *string);  
UIEdgeInsets UIEdgeInsetsFromString(NSString *string);//距離邊界的距離,上左下右的順序!  
UIOffset UIOffsetFromString(NSString *string);   
24、view碎片化
- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates NS_AVAILABLE_IOS(7_0);
- (UIView *)resizableSnapshotViewFromRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates withCapInsets:(UIEdgeInsets)capInsets NS_AVAILABLE_IOS(7_0);


25、隱藏navigatiobbar陰影
self.navigationController.navigationBar.shadowImage = [UIImage new];

【UITextField】

1、設(shè)置placeholder的大小以及顏色


[_phoneTextField setValue:[UIColor colorWithRed:(255)/255.0 green:(255)/255.0 blue:(255)/255.0 alpha:0.5] forKeyPath:@"_placeholderLabel.textColor"];

[_phoneTextField setValue:[UIFont boldSystemFontOfSize:17] forKeyPath:@"_placeholderLabel.font"];

2、收起鍵盤
[self.view endEditing:YES]
3、實時監(jiān)測textField的變化
[_textField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
4、UITextField每四位加一個空格,實現(xiàn)代理
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // 四位加一個空格
    if ([string isEqualToString:@""])
    {
        // 刪除字符
        if ((textField.text.length - 2) % 5 == 0)
        {
            textField.text = [textField.text substringToIndex:textField.text.length - 1];
        }
        return YES;
    }
    else
    {
        if (textField.text.length % 5 == 0)
        {
            textField.text = [NSString stringWithFormat:@"%@ ", textField.text];
        }
    }
    return YES;
}

//=======================================================
//=======================================================
//=======================================================

【UITableView】

1、滾動時執(zhí)行的代理,可以找出當(dāng)前滾動的位置
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;  // 當(dāng)前滾動位移
    CGRect bounds = scrollView.bounds;          // UIScrollView 可視高度
//    CGSize size = scrollView.contentSize;         // 滾動區(qū)域
    UIEdgeInsets inset = scrollView.contentInset;
    float y = offset.y + bounds.size.height - inset.bottom;
//    float h = size.height;

    //在界面上顯示的高度
    float allH = _dataSource.count *170 + SCREEN_WIDTH/2+40;
    if (allH < SCREENH_HEIGHT) {
        _tableView.scrollEnabled = NO;
    }else{
        _tableView.scrollEnabled = YES;
        if (SCREENH_HEIGHT >= y) {
            _tableView.backgroundColor = RGB(255, 122, 82);
        }else{
            _tableView.backgroundColor = grayBG;
        }
        
    }
    
    
}

2、狀態(tài)欄的相關(guān)設(shè)置(UIStatusBar)

ios上狀態(tài)欄 就是指的最上面的20像素高的部分
狀態(tài)欄分前后兩部分,要分清這兩個概念,后面會用到:
前景部分:就是指的顯示電池、時間等部分;
背景部分:就是顯示黑色或者圖片的背景部分;

如下圖:前景部分為白色,背景部分為黑色


屏幕快照 2016-12-07 下午1.36.16.png

1.plist設(shè)置statusBar
在plist里增加一行 UIStatusBarStyle(或者是“Status bar style”也可以),這里可以設(shè)置兩個值,就是上面提到那兩個 UIStatusBarStyleDefault 和 UIStatusBarStyleLightContent
這樣在app啟動的launch頁顯示的時候,statusBar的樣式就是上面plist設(shè)置的風(fēng)格。

/
2.程序代碼里設(shè)置statusBar

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];  
或者
//相對于上面的接口,這個接口可以動畫的改變statusBar的前景色  
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];

/

不僅如此,ios還很貼心的在UIViewController也增加了幾個接口,
目的是讓狀態(tài)欄根據(jù)當(dāng)前顯示的UIViewController來定制statusBar的前景部分。

- (UIStatusBarStyle)preferredStatusBarStyle;

- (UIViewController *)childViewControllerForStatusBarStyle;

- (void)setNeedsStatusBarAppearanceUpdate

- (UIStatusBarStyle)preferredStatusBarStyle:

在你自己的UIViewController里重寫此方法,返回你需要的值(UIStatusBarStyleDefault 或者 UIStatusBarStyleLightContent);

注意:

這里如果你只是簡單的return一個固定的值,那么該UIViewController顯示的時候,程序就會馬上調(diào)用該方法,來改變statusBar的前景部分;
如果在該UIViewController已經(jīng)在顯示在當(dāng)前,你可能還要在當(dāng)前頁面不時的更改statusBar的前景色,那么,你首先需要調(diào)用下面的setNeedsStatusBarAppearanceUpdate方法(這個方法會通知系統(tǒng)去調(diào)用當(dāng)前UIViewController的preferredStatusBarStyle方法), 這個和UIView的setNeedsDisplay原理差不多(調(diào)用UIView對象的setNeedsDisplay方法后,系統(tǒng)會在下次頁面刷新時,調(diào)用重繪該view,系統(tǒng)最快能1秒刷新60次頁面,具體要看程序設(shè)置)。

- (UIViewController *)childViewControllerForStatusBarStyle:

這個接口也很重要,默認(rèn)返回值為nil。當(dāng)我們調(diào)用setNeedsStatusBarAppearanceUpdate時,系統(tǒng)會調(diào)用application.window的rootViewController的preferredStatusBarStyle方法,我們的程序里一般都是用UINavigationController做root,如果是這種情況,那我們自己的UIViewController里的preferredStatusBarStyle根本不會被調(diào)用;
這種情況下childViewControllerForStatusBarStyle就派上用場了,
我們要子類化一個UINavigationController,在這個子類里面重寫childViewControllerForStatusBarStyle方法,如下:

- (UIViewController *)childViewControllerForStatusBarStyle{
    return self.topViewController;
}

上面代碼的意思就是說,不要調(diào)用我自己(就是UINavigationController)的preferredStatusBarStyle方法,而是去調(diào)用navigationController.topViewController的preferredStatusBarStyle方法,這樣寫的話,就能保證當(dāng)前顯示的UIViewController的preferredStatusBarStyle方法能影響statusBar的前景部分。

另外,有時我們的當(dāng)前顯示的UIViewController可能有多個childViewController,重寫當(dāng)前UIViewController的childViewControllerForStatusBarStyle方法,讓childViewController的preferredStatusBarStyle生效(當(dāng)前UIViewController的preferredStatusBarStyle就不會被調(diào)用了)。

簡單來說,只要UIViewController重寫的的childViewControllerForStatusBarStyle方法返回值不是nil,那么,UIViewController的preferredStatusBarStyle方法就不會被系統(tǒng)調(diào)用,系統(tǒng)會調(diào)用childViewControllerForStatusBarStyle方法返回的UIViewController的preferredStatusBarStyle方法。

- (void)setNeedsStatusBarAppearanceUpdate:

讓系統(tǒng)去調(diào)用application.window的rootViewController的preferredStatusBarStyle方法,如果rootViewController的childViewControllerForStatusBarStyle返回值不為nil,則參考上面的講解。

另辟蹊徑
創(chuàng)建一個UIView,
設(shè)置該UIView的frame.size 和statusBar大小一樣,
設(shè)置該UIView的frame.origin 為{0,-20},
設(shè)置該UIView的背景色為你希望的statusBar的顏色,
在navigationBar上addSubView該UIView即可。

3、自適應(yīng)高度,關(guān)鍵代碼
_tableView.rowHeight = UITableViewAutomaticDimension;    
_tableView.estimatedRowHeight = 44;
4、調(diào)整cell分割線的位置
-(void)viewDidLayoutSubviews {

    if ([self.mytableview respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.mytableview setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];

    }
    if ([self.mytableview respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.mytableview setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
    }

}
5、設(shè)置滑動的時候隱藏navigationBar
navigationController.hidesBarsOnSwipe = Yes;

//=======================================================
//=======================================================
//=======================================================

【UIView】

漸變色
CAGradientLayer *layer = [CAGradientLayer layer];

layer.startPoint = CGPointMake(0.5 , 0);//(0,0)表示從左上角開始變化。默認(rèn)值是(0.5,0.0)表示從x軸為中間,y為頂端的開始變化

layer.endPoint = CGPointMake(0.5, 1);//(1,1)表示到右下角變化結(jié)束。默認(rèn)值是(0.5,1.0)  表示從x軸為中間,y為低端的結(jié)束變化

layer.colors = [NSArray arrayWithObjects:(id)RGB(255, 122, 82).CGColor,(id)RGB(254, 89, 85).CGColor, nil];

//    layer.locations = @[@0.0f,@0.6f,@1.0f];//漸變顏色的區(qū)間分布,locations的數(shù)組長度和color一致,這個值一般不用管它,默認(rèn)是nil,會平均分布

layer.frame = headerView.layer.bounds;

[headerView.layer insertSublayer:layer atIndex:0];

【UDID UUID】

UDID的全名為 Unique Device Identifier :設(shè)備唯一標(biāo)識符。從名稱上也可以看出,UDID這個東西是和設(shè)備有關(guān)的,而且是只和設(shè)備有關(guān)的,有點類似于MAC地址。需要把UDID這個東西添加到Provisoning Profile授權(quán)文件中,也就是把設(shè)備唯一標(biāo)識符添加進去,以此來識別某一臺設(shè)備。
但是如果我們代碼中要用到UDID,那么應(yīng)該怎么辦呢?很遺憾,自從iOS5之后,蘋果就禁止了通過代碼訪問UDID,在這之前,可以使用[[UIDevice cuurrent] uniqueIdenfier] 這個方法來獲取某設(shè)備UDID,現(xiàn)在是不可能了

NSUUID *uuid = [UIDevice currentDevice].identifierForVendor;
NSLog(@"uuid 1 = %@",uuid.UUIDString);

英文名稱是:Universally Unique Identifier,翻譯過來就是通用唯一標(biāo)識符。是一個32位的十六進制序列,使用小橫線來連接:8-4-4-4-12 。UUID在某一時空下是唯一的。比如在當(dāng)前這一秒,全世界產(chǎn)生的UUID都是不一樣的;當(dāng)然同一臺設(shè)備產(chǎn)生的UUID也是不一樣的。我在很早之前的一篇博客中使用了一種現(xiàn)在看起來非常愚蠢的方式來獲取當(dāng)前的UUID,下面也有讀者反映了這個情況,現(xiàn)在最簡單獲取UUID的代碼如下

 for (int i = 0; i < 10; i++)
    {
        NSString *uuid = [NSUUID UUID].UUIDString;
        NSLog(@"uuid 2 = %@",uuid);
    }

通過運行程序可以發(fā)現(xiàn),循環(huán)10次,每一次打印的值都是不一樣的,當(dāng)然循環(huán)的再多,這個值永遠不會出現(xiàn)兩個一樣的值。所以從某種程序上來說,UUID跟你的設(shè)備沒有什么關(guān)系了。

/

IDFA(identifierForIdentifier)廣告標(biāo)示符,適用于對外:例如廣告推廣,換量等跨應(yīng)用的用戶追蹤等。

#import <AdSupport/AdSupport.h>
NSString *adId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

【imageView】

1、獲取網(wǎng)絡(luò)image
//首先得拿到照片的路徑,也就是下邊的string參數(shù),轉(zhuǎn)換為NSData型。
 NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:string]];
//然后就是添加照片語句,這次不是`imageWithName`了,是 imageWithData。
 UIImageView.image = [UIImage imageWithData:data];
2、上傳多張圖片

上傳圖片到服務(wù)器我們一般使用AFNetWorking方法里面的下面這個方法,這是一般的上傳單張圖片。
如果我們需要上傳多張圖片,我們可以使用兩種方法:
1、使用for循環(huán),直接把這個網(wǎng)絡(luò)請求的方法直接寫在for循環(huán)里面
2、使用遞歸方法,記錄下總照片數(shù),在成功里面寫這個回歸主線程的方法,在成功的時候把總照片數(shù)減一,然后在重新調(diào)用這個方法,當(dāng)總數(shù)為0的時候return
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//通知主線程刷新
dispatch_async(dispatch_get_main_queue(), ^{
//回調(diào)或者說是通知主線程刷新,
});

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
 [manager POST:url parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

UILabel顯示定時器文本的跳動問題解決方案

image.png

上面的gif圖會發(fā)現(xiàn)在顯示驗證碼計數(shù)時出現(xiàn)跳動和閃爍的問題。目前大多數(shù)用來實現(xiàn)定時器顯示的控件都是UILabel。
在iOS9以前系統(tǒng)默認(rèn)的英文字體是Helvetica, 這種字體每個數(shù)字的寬度都是相等的。而在iOS9以后默認(rèn)的英文字體變?yōu)镾an Fransico字體,這種字體每個數(shù)字的寬度是不相等的。
正是因為數(shù)字寬度的不相等就導(dǎo)致了用UILabel來顯示定時器文本時出現(xiàn)文字跳動閃爍的問題。 因此解決的方案就是選用一種等寬數(shù)字字體顯示即可。為此有兩個解決方案:

1、用Helvetica字體代碼默認(rèn)字體。

  UILabel *label = [UILabel new];
  label.font = [UIFont fontWithName:@"Helvetica" size:16];

2、用UIFont的新API:+ (UIFont *)monospacedSystemFontOfSize:(CGFloat)fontSize weight:(UIFontWeight)weight;

  UILabel *label = [UILabel new];
  //記得這個API是iOS9以后才有效?。?!
  label.font = [UIFont monospacedSystemFontOfSize:16 weight:UIFontWeightRegular];  
最后編輯于
?著作權(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)容

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