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

来源:互联网 发布:淘宝u站九块九包邮 编辑:程序博客网 时间:2024/06/06 17:44
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上的按钮
     [viewremoveFromSuperview];
     }
2.动画修改选择视图的坐标
  [UIViewanimateWithDuration:0.3
                    animations:^{
                  _selectedImgView.center = item.center;
                     }];
3.第一次添加到控制器视图上的子视图如果是ScrollView的对象(包含ScrollView子类的对象),则视图的内容偏移量会根据是否有导航栏和标签栏进行扩充
4.配置翻转动画(此方法与下面方法比较需要去求得翻转图片的下标)
        [UIViewtransitionWithView:imgView
                         duration:0.5
                          options:UIViewAnimationOptionTransitionFlipFromLeft
                       
animations:^{
                            [
self.viewexchangeSubviewAtIndex:index1withSubviewAtIndex:index2];
                        }
                       completion:nil];
5.翻转forView的两个子视图(forView:需要被翻转视图的父视图;flag:是否从左往右翻转)
//翻转视图
[selfflipView:self.viewleft:btn1.hidden];

- (void)flipView:(UIView*)forView left:(BOOL)flag
{
   
UIViewAnimationOptions flip;
   
if(flag)
    {
      flip =UIViewAnimationOptionTransitionFlipFromLeft;
    }
   
else
    {
     flip =UIViewAnimationOptionTransitionFlipFromRight;
    }
   
   
//翻转
    [
UIViewtransitionWithView:forViewduration:0.5options:flipanimations:^{
       
    }
completion:nil];
}
6.iOS5.0之前解析json数据 ——> 使用第三方json解析工具: jsonKit/TouchJson/SBJson
 iOS5.0之后 ——> 使用NSJSONSerialization解析
7.//找到json数据路径
NSString *filePath = [[NSBundlemainBundle] pathForResource:us_box ofType:@"json"];
//从文件路径中获取数据
NSData *data = [NSDatadataWithContentsOfFile:filePath];
//解析json----》转换成NSDictionary或者是NSArray
NSError*error = nil;
NSDictionary *jsonDic = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableContainerserror:&error];
8.//拿到图片地址
NSString *urlStr = [self.movie.imagesobjectForKey:@"medium"];
//构建URL方法一
NSURL*imgurl = [NSURLURLWithString:urlStr];
[_imgViewsd_setImageWithURL:imgurl];
//构建URL方法二
NSURL*imgurl = [NSURLURLWithString:urlStr];
NSData*data = [NSDatadataWithContentsOfURL: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 = [UIColorwhiteColor].CGColor;
14.把头像设置为圆形, 设置圆角半径
    userImage.layer.cornerRadius = 27;//27为正方形图片边长的一半)
   userImage.layer.masksToBounds =YES; //剪切多余部分
15.把图片的宽从0、高从10像素开始画,直到填充整个imgview,可以避免图片拉伸
   UIImage*img = [ImgView.imagestretchableImageWithLeftCapWidth:0topCapHeight:10];
   ImgView.image = img;
 16./****判断是否是第一次启动,用于设置第一次启动的启动页面****/
   //获取沙盒目录
   
NSString *filePath = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents/wx47.plist"];
   
//区分第一次启动还是以后启动的
   
NSDictionary *dic = [NSDictionarydictionaryWithContentsOfFile:filePath];
   
if (dic == nil) {
       
//第一次运行
        [
self_initFirstOpenView];
        dic =
@{@"first":@"YES"};
       
//把一个字典写入到文件
        [dic
writeToFile:filePathatomically:YES];
    }
17.清除缓存
- (void)_countCacheSize
{
   
//获取应用程序的沙盒路径
   
NSString *homePath =NSHomeDirectory();
//    NSLog(@"homePath is %@", homePath);
   
   
//拼接出缓存路径
   
NSString *imgPath = [homePathstringByAppendingPathComponent:@"Library/Caches/default/com.hackemist.SDWebImageCache.default"];
   
   
//文件管理助手(管家)---->单利对象
   
NSFileManager *fileManager = [NSFileManagerdefaultManager];
   
   
//取得一个文件夹下所有的文件路径
   
NSError *error = nil;
   NSArray *subPath = [fileManager subpathsOfDirectoryAtPath:imgPath error:&error];
   
   
long long sum = 0;
   
   
for (NSString*filePath in subPath) {
       
//取得一个文件的路径
       
NSString *path = [imgPathstringByAppendingPathComponent:filePath];
       
       
//拿到文件的属性
       
NSDictionary *attributes = [fileManagerattributesOfItemAtPath:patherror:&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= [NSStringstringWithFormat:@"%.1fM",cacheSize];
    }
}
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
   
if (indexPath.row== 0) {
       
//弹出清除对话框
       
UIAlertController *alertCtrl = [UIAlertControlleralertControllerWithTitle:@"清空缓存"message:@"确定要清空缓存"preferredStyle:UIAlertControllerStyleAlert];
       
       
UIAlertAction *cancleAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];
       
       
UIAlertAction *sureAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {
           
//确定清空缓存
            [[SDImageCachesharedImageCache] clearDisk];
           
           
//刷新tableView
            [
self.tableViewreloadData];
           
        }];
       
        [alertCtrl
addAction:cancleAction];
        [alertCtrl
addAction:sureAction];
        [
selfpresentViewController:alertCtrlanimated:YEScompletion:nil];
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [
superviewWillAppear:animated];
   
//刷新tableView
    [
self.tableViewreloadData];
}
18.label宽度自适应
- (CGSize)labelAutoCalculateRectWith:(NSString*)text FontSize:(CGFloat)fontSize MaxSize:(CGSize)maxSize
{
   
   
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStylealloc]init];
   
    paragraphStyle.
lineBreakMode= NSLineBreakByWordWrapping;
   
   
NSDictionary* attributes =@{NSFontAttributeName:[UIFontsystemFontOfSize:fontSize],NSParagraphStyleAttributeName:paragraphStyle.copy};
   
   
CGSize labelSize = [textboundingRectWithSize:maxSizeoptions:NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLineattributes:attributescontext:nil].size;
   
    labelSize.
height= ceil(labelSize.height);
   
    labelSize.
width= ceil(labelSize.width);
   
   
return labelSize;
   
}
0 0
原创粉丝点击