iOS 一些常用的方法

圖片轉化
- (UIImage*) viewToImage:(UIView *) view{
    UIGraphicsBeginImageContext(view.bounds.size);
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:ctx];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
倒計時
-(void)runloop{
    // 延遲執(zhí)行
    NSLog(@"重復點擊");
    WeakSelf
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        for(NSInteger i = 59 ; i > 0 ; i--) {
            [NSThread sleepForTimeInterval:6];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf reloadData];
            });
            i = 59;
        }

    });
}


- (IBAction)msgClick:(UIButton *)sender {
    // 延遲執(zhí)行
    sender.userInteractionEnabled = NO;
    NSLog(@"重復點擊");
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        for(NSInteger i = 59 ; i > 0 ; i--) {
            [NSThread sleepForTimeInterval:1];
            dispatch_async(dispatch_get_main_queue(), ^{
                [sender setTitle:[NSString stringWithFormat:@"%ld 秒",i] forState:UIControlStateNormal];
            });
        }
        [NSThread sleepForTimeInterval:1];
        dispatch_async(dispatch_get_main_queue(), ^{
            [sender setTitle:@"獲取驗證碼" forState:UIControlStateNormal];
            sender.userInteractionEnabled = YES;
        });
    });
}
倒計時
// 倒計時
    func showLeftTime(btn:UIButton) {
        DispatchQueue.global().async {
            for i in (1..<60).reversed() {
                Thread.sleep(forTimeInterval: 1)
                DispatchQueue.main.async {
                    btn.setTitle("\(i)秒", for: .normal)
                }
            }
            Thread.sleep(forTimeInterval: 1)
            DispatchQueue.main.async {
                btn.setTitle("獲取驗證碼", for: .normal)
                btn.isUserInteractionEnabled = true
            }
        }
    }
富文本部分字體飄灰
+ (instancetype)sharedManager {
    static PayManager *_payManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _payManager = [[self alloc] init];
    });
    return _payManager;
}
富文本部分字體飄灰
- (NSMutableAttributedString *)setupAttributeString:(NSString *)text highlightText:(NSString *)highlightText {
    NSRange hightlightTextRange = [text rangeOfString:highlightText];
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:text];
    if (hightlightTextRange.length > 0) {
        [attributeStr addAttribute:NSForegroundColorAttributeName
                             value:[UIColor colorWithHexString:@"#ff1809"]
                             range:hightlightTextRange];
        [attributeStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:18.0f] range:hightlightTextRange];
        return attributeStr;
    }else {
        return [highlightText copy];
    }
}
手動設置tabar
[self.tabBarController setSelectedIndex:0]
底部陰影
self.bgView.layer.shadowColor=[UIColor colorWithHexString:@"E1E1E1"].CGColor;
    self.bgView.layer.shadowOffset=CGSizeMake(1, 3);
    self.bgView.layer.shadowOpacity= 0.5;
    self.bgView.layer.shadowRadius= 4;
獲取當前控件所在的控制器
+ (UIViewController *)currentViewController{
    UIViewController *VC = APPDELEGATE.window.rootViewController;
    if ([VC isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabBarController = (UITabBarController *)VC;
        UINavigationController *nav = (UINavigationController *)tabBarController.selectedViewController;
        UIViewController * baseVC = (UIViewController *)nav.visibleViewController;
        return baseVC;
    }
    else {
        return VC;
    }
}
標題左邊圖右邊
[_eyesBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -_eyesBtn.imageView.size.width, 0, _eyesBtn.imageView.size.width)];
        [_eyesBtn setImageEdgeInsets:UIEdgeInsetsMake(0, _eyesBtn.titleLabel.bounds.size.width, 0, -_eyesBtn.titleLabel.bounds.size.width)];
標簽富文本
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[contentString  dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName:[UIFont systemFontOfSize:25.0f] } documentAttributes:nil error:nil];
_contentLabel.attributedText = attrStr;
刷新指定行
  [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:1 inSection:0],nil] withRowAnimation:UITableViewRowAnimationFade];
圓角按鈕
self.addMeBtn.layer.cornerRadius = 5;
self.addMeBtn.clipsToBounds = YES;
打開應用
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"weixin://"]];
UITableViewCell*cell = [self.tableView cellForRowAtIndexPath:indexPath];
圓角圖像
cell.image.layer.cornerRadius = 25.5;
cell.image.layer.masksToBounds = YES;

退到主控制器

UIViewController * baseVC = [self currentViewController];
[baseVC.navigationController popToRootViewControllerAnimated:YES];


- (UIViewController *)currentViewController
{
    UIViewController *VC = self.window.rootViewController;
    if ([VC isKindOfClass:[RTJFTabBarController class]]) {
        RTJFTabBarController *tabBarController = (RTJFTabBarController *)VC;
        RTJFNavigationController *nav = (RTJFNavigationController *)tabBarController.selectedViewController;
        UIViewController * baseVC = (UIViewController *)nav.visibleViewController;
        return baseVC;
    }
    else {
        return VC;
    }
}

圖標旋轉

_imageview.transform=CGAffineTransformMakeRotation(M_PI*2);

異步線程

dispatch_async(dispatch_get_global_queue(0, 0), ^{  
    // 處理耗時操作的代碼塊...  
      
    //通知主線程刷新  
    dispatch_async(dispatch_get_main_queue(), ^{  
        //回調或者說是通知主線程刷新有梆,  
    });  
      
});  
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末积仗,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子奋岁,更是在濱河造成了極大的恐慌,老刑警劉巖肛搬,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件踪央,死亡現(xiàn)場離奇詭異伞矩,居然都是意外死亡,警方通過查閱死者的電腦和手機尽纽,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進店門咐蚯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人弄贿,你說我怎么就攤上這事春锋。” “怎么了挎春?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵看疙,是天一觀的道長。 經常有香客問我直奋,道長能庆,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任脚线,我火速辦了婚禮搁胆,結果婚禮上,老公的妹妹穿的比我還像新娘邮绿。我一直安慰自己渠旁,他們只是感情好,可當我...
    茶點故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布船逮。 她就那樣靜靜地躺著顾腊,像睡著了一般。 火紅的嫁衣襯著肌膚如雪挖胃。 梳的紋絲不亂的頭發(fā)上杂靶,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天梆惯,我揣著相機與錄音,去河邊找鬼吗垮。 笑死垛吗,一個胖子當著我的面吹牛,可吹牛的內容都是我干的烁登。 我是一名探鬼主播怯屉,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼饵沧!你這毒婦竟也來了锨络?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤捷泞,失蹤者是張志新(化名)和其女友劉穎足删,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體锁右,經...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡失受,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了咏瑟。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拂到。...
    茶點故事閱讀 39,773評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖码泞,靈堂內的尸體忽然破棺而出兄旬,到底是詐尸還是另有隱情,我是刑警寧澤余寥,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布领铐,位于F島的核電站,受9級特大地震影響宋舷,放射性物質發(fā)生泄漏绪撵。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一祝蝠、第九天 我趴在偏房一處隱蔽的房頂上張望音诈。 院中可真熱鬧,春花似錦绎狭、人聲如沸细溅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽喇聊。三九已至,卻和暖如春蹦狂,著一層夾襖步出監(jiān)牢的瞬間承疲,已是汗流浹背邻耕。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留燕鸽,地道東北人。 一個月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓啼辣,卻偏偏與公主長得像啊研,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子鸥拧,可洞房花燭夜當晚...
    茶點故事閱讀 44,689評論 2 354

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,097評論 25 707
  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫党远、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,098評論 4 62
  • 從大早上起來就覺得難過的不行 因為感覺憋屈所以開始mao勁喝水富弦, 眼睛腫的老高慶幸這一次不是因為掉眼淚 盡管課上的...
    小小荷葉水中游閱讀 125評論 0 0
  • 想法有了沟娱,那就先寫一個文案吧,否則誰知道你想做什么腕柜。 如果你具有:創(chuàng)業(yè)的夢想济似;融入團隊的熱情;讓人信服的產品研發(fā)能...
    何必閱讀 5,730評論 2 3
  • 以前喜歡寫字記錄的我現(xiàn)在心里萬馬奔騰般想要表達現(xiàn)狀卻不知從何寫起 每天都告訴自己 一定會好起來的
    大瑤大瑤下雨不愁閱讀 128評論 0 0