iOS - Useful Tips (原创整理,持续更新)

<span style="color: rgb(255, 102, 102); font-family: Arial, Helvetica, sans-serif; font-size: 18px; background-color: rgb(255, 255, 255);">索引:</span>
1.图片模糊化处理
2.在release版本禁止输出NSLog内容
3.通过GestureRecognizer实现点击任意区域隐藏键盘
4.UITableView初始化代码
5.判断日期是 昨天/今天/明天
6.拉伸图像(小图片拉伸,减小图片占用资源)
7.UIImageView 添加手势实现点击
8.添加到cell上的button 获取点击事件
9.使图片完整填充到imageview 并不产生拉伸效果
10.tableview 避免cell中的textfield键盘遮挡
11.不显示无内容的cell

12.加载页不显示statusbar(2015.2.10更新)

  • **
     *01:图片模糊化处理
     */
    +(UIImage *)scale:(UIImage *)image toSize:(CGSize)size
    {
        UIGraphicsBeginImageContext(size);
        [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
        UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return scaledImage;
    }
    /**
     *02:在release版本禁止输出NSLog内容
     */
    #ifdef DEBUG
    #define NSLog(...) NSLog(__VA_ARGS__)
    #define debugMethod() NSLog(@"%s", __func__)
    #else
    #define NSLog(...)
    #define debugMethod()
    #endif
    /**
     *03:通过GestureRecognizer实现点击任意区域隐藏键盘
     *基本思想如下:
     *1. 在ViewController载入的时候,将键盘显示和消失的Notification添加到self.view里。
     *2. 分别在键盘显示和消失时添加和删除TapGestureRecognizer
     */
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self setKeyBoardAutoHidden];
    }
    
    - (void)setKeyBoardAutoHidden{
        NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
        //SingleTap Gesture
        UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapDismissKeyboard:)];
        
        NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
        
        //UIKeyboardWillShowNotification
        [notificationCenter addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) {
            [self.view addGestureRecognizer:singleTapGesture];
        }];
        
        //UIKeyboardWillHideNotification
        [notificationCenter addObserverForName:UIKeyboardWillHideNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) {
            [self.view addGestureRecognizer:singleTapGesture];
        }];
        
    }
    - (void) backgroundTapDismissKeyboard:(UIGestureRecognizer *) gestureRecognizer{
        //将self.view里所有的subview的first responder 都resign掉
        [self.view endEditing:YES];
    }
    
    /**
     *04:UITableView初始化代码
     */
    - (void)initContentView
    {
        CGRect frame = self.view.frame;
        CGRect tableFrame = CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height);
        
        UITableView *contentTableView= [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStyleGrouped];
    	contentTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        /*
         contentTableView.dataSource = self;
         contentTableView.delegate = self;
         contentTableView.backgroundColor = [UIColor whiteColor];
         contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
         self.tableview = contentTableView;
         */
    	[self.view addSubview:contentTableView];
    }
    
    /**
     *05:判断日期是 昨天/今天/明天
     */
    -(NSString *)compareDate:(NSDate *)date{
        
        NSTimeInterval secondsPerDay = 24 * 60 * 60;
        NSDate *today = [[NSDate alloc] init];
        NSDate *tomorrow, *yesterday;
        
        tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
        yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
        
        // 10 first characters of description is the calendar date:
        NSString * todayString = [[today description] substringToIndex:10];
        NSString * yesterdayString = [[yesterday description] substringToIndex:10];
        NSString * tomorrowString = [[tomorrow description] substringToIndex:10];
        
        NSString * dateString = [[date description] substringToIndex:10];
        
        if ([dateString isEqualToString:todayString])
        {
            return @"今天";
        } else if ([dateString isEqualToString:yesterdayString])
        {
            return @"昨天";
        }else if ([dateString isEqualToString:tomorrowString])
        {
            return @"明天";
        }
        else
        {
            return dateString;
        }
    }
    /**
     *06:拉伸图像(小图片拉伸,减小图片占用资源)
     */
    - (UIImage *)stretchPicture:(NSString *)imageName
    {
        return [[UIImage imageNamed:imageName] stretchableImageWithLeftCapWidth:15 topCapHeight:0];
    }
    /**
     *07:高保真压缩图片方法
     */
    - (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
    {
        NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
        // 获取沙盒目录
        NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
        // 将图片写入文件
        [imageData writeToFile:fullPath atomically:NO];
    }
    /**
     *08.UIImageView 添加手势实现点击
     */
    //创建控件
    UIImageView *avatarImgView = [[UIImageView alloc] init];
    [avatarImgView setImage:[UIImage imageNamed:@"arrow_1"]];
    [avatarImgView setUserInteractionEnabled:TRUE];  //设置UIImageView允许点击
    [self.view addSubview:arrowImgView];
    
    //添加手势
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickImage)];
    [avatarImgView addGestureRecognizer:singleTap];
    
    - (void)onClickImage {
        //点击操作内容
    }
    /**
     *09.添加到cell上的button 获取点击事件
     */
    [tableView setDelaysContentTouches:NO];

     
    /**
     *10.使图片完整填充到imageview 并不产生拉伸效果
     */
    avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
    avatarImageView.clipsToBounds = YES;

     
    /**
     *11.tableview 避免cell中的textfield键盘遮挡
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification object:nil];
    }
    
    - (void)viewWillDisappear:(BOOL)animated{
        [super viewWillDisappear:animated];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    - (void)keyboardWillShow:(NSNotification *)note
    {
        UIEdgeInsets insets = UIEdgeInsetsMake(0.0f,
                                               0.0f,
                                               self.view.frame.size.height - 220,
                                               0.0f);
        self.tableView.contentInset = insets;
        self.tableView.scrollIndicatorInsets = insets;
        cvos = self.tableView.contentOffset;
    
    }
    
    - (void)keyboardWillHide:(NSNotification *)note
    {
        UIEdgeInsets insets = UIEdgeInsetsMake(0.0f,
                                               0.0f,
                                               self.view.frame.size.height+220,
                                               0.0f);
        self.tableView.contentInset = insets;
        self.tableView.scrollIndicatorInsets = insets;
        }
    
    //隐藏键盘
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [textField resignFirstResponder];
        self.tableView.contentOffset = cvos;
        self.tableView.contentSize = CGSizeMake(UISCREENWIDTH, 0);
        return YES;
    }
     
    <span style="font-family: Arial, Helvetica, sans-serif;">/** 
     *12.加载页不显示statusbar
     */
    </span><pre name="code" class="objc">info.plist文件中设置 初始化时隐藏状态栏, 加载之后需要将statusbar恢复, didFinishLaunchingWithOptions 加入下行代码
     [UIApplication sharedApplication].statusBarHidden = NO;
     
  • 
    
  • <span style="font-family: Arial, Helvetica, sans-serif;">
    </span>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值