iOS功能整理

持續(xù)更新中... (ps:開頭一開始是目錄索引斩熊,但是簡書不支持何荚,改成列表)

一 UI控件類

  • 1.1 textField的背景圖片(防止圖片復用)
  • 1.2 tableViewCell 側(cè)滑按鈕UITableViewCellDeleteConfirmationView樣式更改
  • 1.3 圖片按比例縮放
  • 1.4 CALayer圖層鏤空(可用于app內(nèi)新手指引)
  • 1.5解決UICollectionView ReloadData閃一下(隱式動畫)

二 Foundation基礎(chǔ)功能類

  • 2.1 應(yīng)用名 獲取
  • 2.2 在APP內(nèi)跳轉(zhuǎn)設(shè)置
  • 2.3 系統(tǒng)版本宏定義

<h2 id="一">一 UI控件類</h2>
<h4 id="1.1">1.textField的背景圖片(防止圖片復用)</h4>

- (UIImage *)textFieldBgImageStr:(NSString *)imageStr
{
    // 加載圖片
    UIImage *image = [UIImage imageNamed:imageStr];
    
    // 設(shè)置端蓋的值
    CGFloat top = image.size.height * 0.5;
    CGFloat left = image.size.width * 0.5;
    CGFloat bottom = image.size.height * 0.5;
    CGFloat right = image.size.width * 0.5;
    
    // 設(shè)置端蓋的值
    UIEdgeInsets edgeInsets = UIEdgeInsetsMake(top, left, bottom, right);
    // 設(shè)置拉伸的模式
    UIImageResizingMode mode = UIImageResizingModeStretch;
    
    // 拉伸圖片
    UIImage *newImage = [image resizableImageWithCapInsets:edgeInsets resizingMode:mode];
    
    return newImage;
}

<h4 id="1.2">2.tableViewCell 側(cè)滑按鈕UITableViewCellDeleteConfirmationView樣式更改</h4>

- (void)willTransitionToState:(UITableViewCellStateMask)state {
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        for (UIView *subView in self.subviews) {
            
            if ([NSStringFromClass([subView class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
                UIView *view = ((UIView *)[subView.subviews firstObject]);
                
                //view : <_UITableViewCellActionButton: 0x7fbe61f1b0d0; frame = (132 0; 66 44); opaque = NO; autoresize = H; layer = <CALayer: 0x7fbe61f1cd50>>
                //view.superview: <UITableViewCellDeleteConfirmationView: 0x7fbe61de34d0; frame = (415.333 0; 0 44); clipsToBounds = YES; autoresize = H; layer = <CALayer: 0x7fbe61de3680>>
                view.backgroundColor = [UIColor clearColor];
                view.superview.backgroundColor = [UIColor clearColor];
                
                // view.backgroundColor = self.backgroundColor;
                
                NSLog(@"%@", view.subviews[0]);
                /**
                 *view.subviews = {
                 <UIButtonLabel: 0x7fa769549440; frame = (15 11; 52.3333 21.6667); text = 'Delete'; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fa769548cd0>>
                 }
                 */
                //替換字體
                [view.subviews[0] setValue:@"刪除" forKey:@"text"];
                //替換字體顏色
                [view.subviews[0] setValue:[UIColor redColor] forKeyPath:@"textColor"];
                
                //也可以直接設(shè)置view.layer 但是不會出現(xiàn)邊框跟著移動的效果(下圖), 這也說明了, UITableViewCellDeleteConfirmationView的frame是跟著你的手指移動在變化的
                view.superview.layer.cornerRadius = 10.0;
                view.superview.layer.borderWidth = 2.0;
                view.superview.layer.borderColor = [UIColor greenColor].CGColor;
                view.superview.layer.masksToBounds = YES;             
            }
        }
    });    
}

//button 已經(jīng)展示出來button, 將要關(guān)閉的時候調(diào)用
- (void)didTransitionToState:(UITableViewCellStateMask)state {
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
}

- (void)willTransitionToState:(UITableViewCellStateMask)state {
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        for (UIView *subView in self.subviews) {
            
            if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
                subView.backgroundColor=[UIColor clearColor];
                for (UIView *view in subView.subviews) {
                    if ([view isKindOfClass:[UIButton class]]) {
                        UIButton *btn = (UIButton *)view;
                        btn.backgroundColor=[UIColor clearColor];
                        [btn setImage:[UIImage imageNamed:@"delete_like_user"] forState:UIControlStateNormal];
                        [btn setTitle:nil forState:UIControlStateNormal];
                    }
                }
            }
        }
    });
}

<h4 id="1.3">3.圖片按比例縮放</h4>

/** * @brief Scale Image / 縮放圖片 */
- (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)size {
    
    if (image.size.width < size.width) {
        return image;
    }
    
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

<h4 id="1.4">4.CALayer圖層鏤空(可用于app內(nèi)新手指引)</h4>

- (void)setupMask {
    // ----- 自己繪制路徑path
    self.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.5];
    self.toolView = [[UIView alloc] initWithFrame:CGRectMake(40, 100, 200, 50)];
    self.toolView.backgroundColor = [UIColor orangeColor];
    _toolView.layer.cornerRadius = _toolView.frame.size.height / 2;
    [self addSubview:self.toolView];

    CGPathRef path = CGPathCreateWithEllipseInRect(_toolView.frame, 0);
    
    CGMutablePathRef mutablePath = CGPathCreateMutable();
    CGPathAddRect(mutablePath, 0, self.frame);
    CGPathAddPath(mutablePath, 0, path);
    
    CAShapeLayer *mask = [CAShapeLayer layer];
    mask.path = mutablePath;
    mask.fillRule = kCAFillRuleEvenOdd;
    _toolView.layer.mask = mask;
    
    CGPathRelease(path);
    
}

- (void)testForMask {
    // ----- 利用鏤空控件的path
    self.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.5];
    self.toolView = [[UIView alloc] initWithFrame:CGRectMake(40, 100, 200, 50)];
    self.toolView.backgroundColor = [UIColor orangeColor];
    [self addSubview:self.toolView];

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.frame cornerRadius:0];
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:_toolView.frame cornerRadius:_toolView.frame.size.height/2];
    [path appendPath:circlePath];
    
    CAShapeLayer *fillLayer = [CAShapeLayer layer];
    fillLayer.path = path.CGPath;
    fillLayer.fillRule = kCAFillRuleEvenOdd;
    
    _toolView.layer.mask = fillLayer;
    _toolView.layer.cornerRadius = _toolView.frame.size.height / 2;

}
- (void)demoTest {
    
    self.toolView = [[UIView alloc] initWithFrame:CGRectMake(40, 100, 200, 50)];
    self.toolView.backgroundColor = [UIColor orangeColor];
    _toolView.layer.cornerRadius = _toolView.frame.size.height / 2;
    [self addSubview:self.toolView];

    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.frame cornerRadius:0];
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:_toolView.frame cornerRadius:_toolView.frame.size.height/2];
    [path appendPath:circlePath];
    [path setUsesEvenOddFillRule:YES];
    
    CAShapeLayer *fillLayer = [CAShapeLayer layer];
    fillLayer.path = path.CGPath;
    fillLayer.fillRule = kCAFillRuleEvenOdd;
    fillLayer.fillColor = [UIColor colorWithWhite:0.3 alpha:0.5].CGColor;
    
    [self.layer addSublayer:fillLayer];
}

<h4 id="1.5">5.解決UICollectionView ReloadData閃一下(隱式動畫)</h4>

這方法有坑:
在增刪cell的時候再調(diào)用 reloadData 刷新會 crash

也就是
所以方法中的reloadData換成reloadSections就好了

[UIView setAnimationsEnabled:NO];
 
[collectionView performBatchUpdates:^{
    [collectionView reloadData];
} completion:^(BOOL finished) {
    [UIView setAnimationsEnabled:YES];
}];
[UIView animateWithDuration:0 animations:^{
    [collectionView performBatchUpdates:^{
        [collectionView reloadData];
    } completion:nil];
}];

<h2 id="二">二 Foundation基礎(chǔ)功能類</h2>
<h4 id="2.1">2.1 應(yīng)用名 獲取</h4>

NSString *appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleDisplayName"];
if (!appName) {
    appName = [[NSBundle mainBundle] .infoDictionary valueForKey:@"CFBundleName"];
}

<h4 id="2.2">2.2 在APP內(nèi)跳轉(zhuǎn)設(shè)置</h4>

if (iOS8Later) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    } else {
        NSURL *privacyUrl = [NSURL URLWithString:@"prefs:root=Privacy&path=PHOTOS"];
        if ([[UIApplication sharedApplication] canOpenURL:privacyUrl]) {
            [[UIApplication sharedApplication] openURL:privacyUrl];
        } else {
            NSString *message = [NSBundle tz_localizedStringForKey:@"Can not jump to the privacy settings page, please go to the settings page by self, thank you"];
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:[NSBundle tz_localizedStringForKey:@"Sorry"] message:message delegate:nil cancelButtonTitle:[NSBundle tz_localizedStringForKey:@"OK"] otherButtonTitles: nil];
            [alert show];
        }
    }

<h4 id="2.3">2.3 系統(tǒng)版本宏定義</h4>

#define iOS7Later ([UIDevice currentDevice].systemVersion.floatValue >= 7.0f)
#define iOS8Later ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f)
#define iOS9Later ([UIDevice currentDevice].systemVersion.floatValue >= 9.0f)
#define iOS9_1Later ([UIDevice currentDevice].systemVersion.floatValue >= 9.1f)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末肯适,一起剝皮案震驚了整個濱河市阐虚,隨后出現(xiàn)的幾起案子烟勋,更是在濱河造成了極大的恐慌搜骡,老刑警劉巖拂盯,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異记靡,居然都是意外死亡谈竿,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進店門摸吠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來空凸,“玉大人,你說我怎么就攤上這事寸痢⊙街蓿” “怎么了?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵啼止,是天一觀的道長道逗。 經(jīng)常有香客問我,道長献烦,這世上最難降的妖魔是什么滓窍? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮巩那,結(jié)果婚禮上吏夯,老公的妹妹穿的比我還像新娘此蜈。我一直安慰自己,他們只是感情好噪生,可當我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布裆赵。 她就那樣靜靜地躺著,像睡著了一般跺嗽。 火紅的嫁衣襯著肌膚如雪顾瞪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天抛蚁,我揣著相機與錄音,去河邊找鬼惕橙。 笑死瞧甩,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的弥鹦。 我是一名探鬼主播肚逸,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼彬坏!你這毒婦竟也來了朦促?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤栓始,失蹤者是張志新(化名)和其女友劉穎务冕,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體幻赚,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡禀忆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了落恼。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片箩退。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖佳谦,靈堂內(nèi)的尸體忽然破棺而出戴涝,到底是詐尸還是另有隱情,我是刑警寧澤钻蔑,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布啥刻,位于F島的核電站,受9級特大地震影響咪笑,放射性物質(zhì)發(fā)生泄漏郑什。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一蒲肋、第九天 我趴在偏房一處隱蔽的房頂上張望蘑拯。 院中可真熱鬧钝满,春花似錦、人聲如沸申窘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽剃法。三九已至碎捺,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間贷洲,已是汗流浹背收厨。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留优构,地道東北人诵叁。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像钦椭,于是被迫代替她去往敵國和親拧额。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,914評論 2 355

推薦閱讀更多精彩內(nèi)容