项目一(电影APP)——iOS学习连载26

1. 删除系统的 tabbarItem
NSArray *array = self.tabBar.subviews;
// 注意: UITabBarButton 是一个私有的 API ,没有公开出来
// 遍历 tabbar 中所有的子视图,并且移除 tabbarItem
for ( UIView *view in array) {
    Class cls = NSClassFromString ( @"UITabBarButton" );
    if ([view isKindOfClass : cls]) {
     // 移除 tabbar 上的按钮
     [view removeFromSuperview ];
     }
2. 动画修改选择视图的坐标
  [UIView animateWithDuration:0.3
                     animations :^{
                   _selectedImgView.center = item.center;
                     }];
3. 第一次添加到控制器视图上的子视图如果是 ScrollView 的对象(包含 ScrollView 子类的对象) , 则视图的内容偏移量会根据是否有导航栏和标签栏进行扩充
4. 配置翻转动画(此方法与下面方法比较需要去求得翻转图片的下标)
        [ UIView transitionWithView : imgView
                          duration:0.5
                           options : UIViewAnimationOptionTransitionFlipFromLeft
                       
animations :^{
                            [
self . view exchangeSubviewAtIndex :index1 withSubviewAtIndex :index2];
                        }
                        completion:nil];
5 . 翻转 forView 的两个子视图( forView :需要被翻转视图的父视图; flag :是否从左往右翻转)
// 翻转视图
[self flipView:self.view left:btn1.hidden];

- ( void )flipView:( UIView *)forView left:( BOOL )flag
{
   
UIViewAnimationOptions flip;
   
if (flag)
    {
      flip = UIViewAnimationOptionTransitionFlipFromLeft;
    }
   
else
    {
     flip = UIViewAnimationOptionTransitionFlipFromRight;
    }
   
   
// 翻转
    [
UIView transitionWithView :forView duration : 0.5 options :flip animations :^{
       
    }
completion : nil ];
}
6. iOS5.0 之前解析 json 数据  ——>  使用第三方 json 解析工具 : jsonKit/TouchJson/SBJson
  iOS5 .0之后 ——> 使用NSJSONSerialization解析
7. // 找到 json 数据路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:us_box ofType:@"json"];
// 从文件路径中获取数据
NSData *data = [NSData dataWithContentsOfFile:filePath];
//解析json----》转换成NSDictionary或者是NSArray
NSError *error = nil ;
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
8. // 拿到图片地址
NSString *urlStr = [self.movie.images objectForKey:@"medium"];
// 构建 URL方法一
NSURL *imgurl = [ NSURL URLWithString :urlStr];
[_imgView sd_setImageWithURL:imgurl];
// 构建 URL方法二
NSURL *imgurl = [ NSURL URLWithString :urlStr];
NSData *data = [ NSData dataWithContentsOfURL :imgurl];
self.imageView.image = [UIImage imageWithData:data];
9. 当视图修改了 transform 后,坐标会变,需要重新恢复坐标
     CGPointMake(0, 0) CGPointZero 等效
10.只要改变数据源,就要刷新tableView
11. 隐藏状态栏
- ( BOOL )prefersStatusBarHidden
{
   
return self . navigationController . navigationBarHidden ;
}

- (
UIStatusBarAnimation )preferredStatusBarUpdateAnimation
{
   
return UIStatusBarAnimationFade ;
}
12. view =  layer + 事件接收
13. 设置边框宽度
  userImage . layer . borderWidth = 1 ;
  // 设置边框颜色
  userImage.layer.borderColor = [UIColor whiteColor].CGColor;
14. 把头像设置为圆形, 设置圆角半径
    userImage.layer.cornerRadius = 27;//27为正方形图片边长的一半)
    userImage.layer.masksToBounds = YES; // 剪切多余部分
15. 把图片的宽从 0 、高从 10 像素开始画,直到填充整个 imgview ,可以避免图片拉伸
   UIImage *img = [ Img View . image stretchableImageWithLeftCapWidth : 0 topCapHeight : 10 ];
    Img View .image = img;
 16. /** ** 判断是否是第一次启动,用于设置第一次启动的启动页面 ****/
    // 获取沙盒目录
   
NSString *filePath = [ NSHomeDirectory () stringByAppendingPathComponent : @"Documents/wx47.plist" ];
   
// 区分第一次启动还是以后启动的
   
NSDictionary *dic = [ NSDictionary dictionaryWithContentsOfFile :filePath];
   
if (dic == nil ) {
       
// 第一次运行
        [
self _initFirstOpenView ];
        dic =
@{ @"first" : @"YES" } ;
       
// 把一个字典写入到文件
        [dic
writeToFile :filePath atomically : YES ];
    }
17. 清除 缓存
- ( void )_countCacheSize
{
   
// 获取应用程序的沙盒路径
   
NSString *homePath = NSHomeDirectory ();
//    NSLog(@"homePath is %@", homePath);
   
   
// 拼接出缓存路径
   
NSString *imgPath = [homePath stringByAppendingPathComponent : @"Library/Caches/default/com.hackemist.SDWebImageCache.default" ];
   
   
// 文件管理助手(管家) ----> 单利对象
   
NSFileManager *fileManager = [ NSFileManager defaultManager ];
   
   
// 取得一个文件夹下所有的文件路径
   
NSError *error = nil ;
    NSArray *subPath = [fileManager subpathsOfDirectoryAtPath:imgPath error:&error];
   
   
long long sum = 0 ;
   
   
for ( NSString *filePath in subPath) {
       
// 取得一个文件的路径
       
NSString *path = [imgPath stringByAppendingPathComponent :filePath];
       
       
// 拿到文件的属性
       
NSDictionary *attributes = [fileManager attributesOfItemAtPath :path error :&error];
       
       
// 取出文件的大小
       
NSNumber *filesize = attributes[ NSFileSize ];
       
        sum += [filesize
longLongValue ];
       
    }
   
   
cacheSize = sum / ( 1000.0 * 1000 );
   
NSLog ( @"cacheSize is %.2f" , cacheSize );
   
}
- ( void )tableView:( UITableView *)tableView willDisplayCell:( UITableViewCell *)cell forRowAtIndexPath:( NSIndexPath *)indexPath
{
   
if (indexPath. row == 0 ) {
        [
self _countCacheSize ];
       
UILabel *label = ( UILabel *)[cell viewWithTag : 100 ];
        label.
text = [ NSString stringWithFormat : @"%.1fM" , cacheSize ];
    }
}
- ( void )tableView:( UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath *)indexPath
{
   
if (indexPath. row == 0 ) {
       
// 弹出清除对话框
       
UIAlertController *alertCtrl = [ UIAlertController alertControllerWithTitle : @" 清空缓存 " message : @" 确定要清空缓存 " preferredStyle : UIAlertControllerStyleAlert ];
       
       
UIAlertAction *cancleAction = [ UIAlertAction actionWithTitle : @" 取消 " style : UIAlertActionStyleCancel handler : nil ];
       
       
UIAlertAction *sureAction = [ UIAlertAction actionWithTitle : @" 确定 " style : UIAlertActionStyleDefault handler :^( UIAlertAction *action) {
           
// 确定清空缓存
            [[SDImageCache sharedImageCache] clearDisk];
           
           
// 刷新 tableView
            [
self . tableView reloadData ];
           
        }];
       
        [alertCtrl
addAction :cancleAction];
        [alertCtrl
addAction :sureAction];
        [
self presentViewController :alertCtrl animated : YES completion : nil ];
    }
}
- ( void )viewWillAppear:( BOOL )animated
{
    [
super viewWillAppear :animated];
   
// 刷新 tableView
    [
self . tableView reloadData ];
}
18.label宽度自适应
- ( CGSize )labelAutoCalculateRectWith:( NSString *)text FontSize:( CGFloat )fontSize MaxSize:( CGSize )maxSize
{
   
   
NSMutableParagraphStyle * paragraphStyle = [[ NSMutableParagraphStyle alloc ] init ];
   
    paragraphStyle.
lineBreakMode = NSLineBreakByWordWrapping ;
   
   
NSDictionary * attributes = @{ NSFontAttributeName :[ UIFont systemFontOfSize :fontSize], NSParagraphStyleAttributeName :paragraphStyle. copy } ;
   
   
CGSize labelSize = [text boundingRectWithSize :maxSize options : NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine attributes :attributes context : nil ]. size ;
   
    labelSize.
height = ceil (labelSize. height );
   
    labelSize.
width = ceil (labelSize. width );
   
   
return labelSize;
   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值