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{
    // 延迟执行
    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 {
    // 延迟执行
    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(), ^{  
        //回调或者说是通知主线程刷新,  
    });  
      
});  

你可能感兴趣的:(iOS 一些常用的方法)