天天看點

IOS開發基礎知識--碎片14

1:ZIP檔案壓縮跟解壓,使用ZipArchive

建立/添加一個zip包

ZipArchive* zipFile = [[ZipArchive alloc] init];

//次數得zipfilename需要一個完整得路徑,例如***/Documents/demo.zip

[zipFile CreateZipFile2:@"zipfilename"]; 

//有兩種可選得方式進行建立壓縮包,帶密碼和不帶密碼的

[[zipFile CreateZipFile2:@"zipfilename" Password:@"your password"];

//接下來就是将需要壓縮的檔案添加到這個壓縮包中

//這裡第一個參數需要完整的路徑,例如:***/Documents/a.txt  newname是指檔案在壓縮包中的名字,不需要路徑,隻是一個名稱

[zipFile addFileToZip:@"fullpath of the file" newname:@"new name of the file without path"];

//如果需要将多個檔案進行壓縮,即壓縮檔案夾,重複addFileToZip方法即可

[zipFile CloseZipFile2];
 

解壓zip包:

ZipArchive* zipFile = [[ZipArchive alloc] init];

[zipFile UnzipOpenFile:@"zip file name"]; 

//同樣,對應的就有兩種打開zip包的方式,帶密碼和不帶密碼

[zipFile UnzipOpenFile:@"zip file name" Password:@"password" ];

//壓縮包釋放到的位置,需要一個完整路徑 

[zipFile UnzipFileTo:@"output path" overwrite:YES];

[zipFile UnzipCloseFile];

[zipFile release];

//記得釋放      
1. 壓縮:ZipArchive可以壓縮多個檔案,隻需要把檔案一一addFileToZip即可.

ZipArchive* zip = [[ZipArchive alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentpath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString* l_zipfile = [documentpath stringByAppendingString:@"/test.zip"] ;
 
NSString* image1 = [documentpath stringByAppendingString:@"/image1.jpg"] ;
NSString* image2 = [documentpath stringByAppendingString:@"/image2.jpg"] ;       
 
BOOL ret = [zip CreateZipFile2:l_zipfile];
ret = [zip addFileToZip:image1 newname:@"image1.jpg"];
ret = [zip addFileToZip:image2 newname:@"image2.jpg"];
if( ![zip CloseZipFile2] )
  {
     l_zipfile = @"";
  }
 

2. 解壓縮:

ZipArchive* zip = [[ZipArchive alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentpath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;  //路徑位址要注意
 
NSString* l_zipfile = [documentpath stringByAppendingString:@"/test.zip"] ;
NSString* unzipto = [documentpath stringByAppendingString:@"/test"] ;
if( [zip UnzipOpenFile:l_zipfile] )
 {
   BOOL ret = [zip UnzipFileTo:unzipto overWrite:YES];
   if( NO==ret )
   {
   }
   [zip UnzipCloseFile];
 }      

2:UITapGestureRecognizer傳值

UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];

    imageView.image=[UIImageimageNamed:@"filter_laozhaopian_a.png"];

    imageView.tag = 10000;  //可以通過這樣來給下邊的點選事件傳值

    imageView.userInteractionEnabled = YES;

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(UesrClicked:)];

    [imageView addGestureRecognizer:singleTap];

    [self.view addSubview:imageView];


- (void)UesrClicked:(UITapGestureRecognizer *)recognizer
{
   NSLog(@"%d",(recognizer.view.tag - 1000));
}       

 3:自定義self.navigationItem.titleView視圖

UIView *titleView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, KTitleViewHeight)];
    
    //最新定義左邊的按鍵
    UIButton *leftItemButton=[UIButton buttonWithType:UIButtonTypeCustom];
    leftItemButton.frame=CGRectMake(0, 0, KLeftItemButtonWidth, KLeftItemButtonHeight);
    [leftItemButton setBackgroundImage:[UIImage imageNamed:@"mapMarkNormal"] forState:UIControlStateNormal];
    [leftItemButton setBackgroundImage:[UIImage imageNamed:@"mapMarkSelected"] forState:UIControlStateHighlighted];
    [leftItemButton addTarget:self action:@selector(leftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:leftItemButton];
    [leftItemButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleView);
        make.left.equalTo(titleView.mas_left).with.offset(5);
        make.size.equalTo(CGSizeMake(KLeftItemButtonWidth, KLeftItemButtonHeight));
    }];
    
    UILabel *titleLabel=[UILabel new];
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.font=[UIFont systemFontOfSize:14];
    titleLabel.text=@"廈山中華公園廣場";
    [titleView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleView);
        make.left.equalTo(titleView.mas_left).with.offset((SCREEN_WIDTH-KTitleViewTitleWidte-10)/2);
    }];
    
    UIButton *downButton=[UIButton new];
    [downButton setImage:[UIImage imageNamed:@"FileDownload"] forState:UIControlStateNormal];
    [titleView addSubview:downButton];
    [downButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(titleView.mas_right).with.offset(-2);
        make.centerY.equalTo(titleView);
        make.size.equalTo(CGSizeMake(KTitleViewButtonWidth, KTitleViewButtonHeight));
    }];
    
    UILabel *proLabel=[UILabel new];
    proLabel.font=[UIFont systemFontOfSize:10];
    proLabel.textColor=[UIColor colorWithHexString:KWitTourism_AppTextColor];
    proLabel.textAlignment=NSTextAlignmentRight;
    proLabel.text=@"正在下載下傳";
    [titleView addSubview:proLabel];
    
    [proLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(downButton.mas_left).with.offset(-5);
        make.centerY.equalTo(titleView);
        make.size.equalTo(CGSizeMake(KTitleViewLabelWidth, KTitleViewLabelHeight));
    }];
    
    self.navigationItem.titleView=titleView;      

4:實作無限滾動的uiscrollview

對滾動的圖檔數組頭尾各增加一張,頭則在其前面增加一張尾部的,尾部則插入一張第一張;并在滾動事件中進行處理,改變其位置;主體代碼如下(若不自寫也可以找相應插件,已封裝):

/**
 *  @author wujunyang, 15-06-05 13:06:12
 *
 *  @brief  頭部滾動視圖布局
 */
-(void)setupScrollView
{
    int imgCount=self.imgdatalist.count;
    
    //如果沒有值時 用一張預設的圖檔替換顯示
    if (imgCount==0) {
        UIImageView *featureImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"thirdEmpty"]];
        featureImageView.frame=self.headerImageView.frame;
        [self.headerImageView addSubview:featureImageView];
    }
    else{
        //實際的圖檔個數
        _actualImgNum=self.imgdatalist.count;
        //為實作無限滾動 首先在第一個前面插入一個元素 此元素為最後一個
        [self.imgdatalist insertObject:[self.imgdatalist objectAtIndex:([self.imgdatalist count]-1)] atIndex:0];
        //在第後一個增加一個原先的第一個 由于上面已被插入一個值 是以原先的第一個變成第二個 是以此處為1
        [self.imgdatalist addObject:[self.imgdatalist objectAtIndex:1]];
        //增加後的個數 用于處理定義滾動視圖内容的總寬度
        int imagDataListCount=self.imgdatalist.count;

        _scrollView=[[UIScrollView alloc]init];
        _scrollView.frame=self.headerImageView.frame;
        
        for (int i=0; i<self.imgdatalist.count; i++) {
            AroundImgBean* aroundimgbean=[self.imgdatalist objectAtIndex:i];
            // 擷取圖檔
            NSString *featureImageName = aroundimgbean.aroundimgurl;
            UIImageView *featureImageView = [[UIImageView alloc] init];
            [featureImageView sd_setImageWithURL:[NSURL URLWithString:featureImageName] placeholderImage:[UIImage imageNamed:@"thirdEmpty"]];
            
            // 設定圖檔尺寸位置
            CGFloat featureWidth = SCREEN_WIDTH;
            CGFloat featureHeight = self.headerImageView.frame.size.height;
            CGFloat featureX = SCREEN_WIDTH * i;
            CGFloat featureY = 0;
            featureImageView.frame = CGRectMake(featureX, featureY, featureWidth, featureHeight);
            [_scrollView addSubview:featureImageView];
        }
        
        // 設定scrollView功能屬性
        _scrollView.userInteractionEnabled = YES;
        _scrollView.bounces=NO;
        _scrollView.scrollEnabled = YES; // 支援滾動
        _scrollView.contentSize = CGSizeMake(self.headerImageView.frame.size.width * imagDataListCount, 0); // 隻需要水準滾動
        _scrollView.pagingEnabled = YES; // 支援分頁
        _scrollView.showsHorizontalScrollIndicator = NO; // 隐藏水準滾動條
        
        // 設定代理
        _scrollView.delegate = self;
        
        // 添加
        [self.headerImageView addSubview:_scrollView];
    }
}


//滾動事件
- (void)scrollViewDidScroll:(UIScrollView *)myscrollView {
    if ([myscrollView isEqual:_scrollView]) {
    // 四舍五入,讓圖檔滾動超過中線的時候改變頁碼
    if (self.imgdatalist.count>0) {
        CGFloat pageWidth = _scrollView.frame.size.width;
        int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        _currentPageIndex=page;

        //目前頁數要是沒有超過實際的值時
        if (_currentPageIndex<_actualImgNum) {
            _imageCount=[NSString stringWithFormat:@"%d/%d",_currentPageIndex+1,_actualImgNum];
        }
        else
        {
            _imageCount=[NSString stringWithFormat:@"%d/%d",1,_actualImgNum];
        }
        
        self.imageLabel.text=_imageCount;
    }
    else
    {
        _imageCountView.hidden=YES;
        _imageLabel.hidden=YES;
    }
    }
}


/**
 *  @author wujunyang, 15-06-05 11:06:06
 *
 *  @brief  滾動事件處理 無限滾動 修改滾動的目前位置
 *  @param myscrollView <#myscrollView description#>
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)myscrollView
{
    if ([myscrollView isEqual:_scrollView]) {
        if (_currentPageIndex==0) {
            
            [_scrollView setContentOffset:CGPointMake(([self.imgdatalist count]-2)*myscrollView.frame.size.width, 0)];
        }
        if (_currentPageIndex==([self.imgdatalist count]-1)) {
            
            [_scrollView setContentOffset:CGPointMake(myscrollView.frame.size.width, 0)];
        }
    }
}      

 5:IOS開發實作歌詞自動滾動功能一種實作方式

_timer = [NSTimer scheduledTimerWithTimeInterval:0.09f target:self selector:@selector(rollLyc) userInfo:nil repeats:YES];

- (void)rollLyc
{
    CGFloat currentTime = _audio.currentTime;
    CGFloat lycTime = [_lycList[currentLine + 1][@"time"] floatValue];//_lycList是存儲有每句歌詞和對應的時間的字典集合  currentLine設定為成員變量,在ViewDidLoad中初始為0
    
    NSLog(@"%f --- %f ",currentTime,lycTime);
    
    if (currentTime >= lycTime) {
        currentLine = currentLine + 1;
    }
    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:currentLine inSection:0];
    [self.tableView selectRowAtIndexPath:indexpath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}      

6:iOS 從url中擷取檔案名以及字尾

//這裡有一個模拟器沙盒路徑(完整路徑)

NSString* index=@"/Users/junzoo/Library/Application Support/iPhone Simulator/7.0.3/Applications/63925F20-AF97-4610-AF1C-B6B4157D1D92/Documents/DownLoad/books/2013_50.zip";


對路徑截取的9種操作

    NSLog(@"1=%@",[index lastPathComponent]);

    NSLog(@"2=%@",[index stringByDeletingLastPathComponent]);

    NSLog(@"3=%@",[index pathExtension]);

    NSLog(@"4=%@",[index stringByDeletingPathExtension]);

    NSLog(@"5=%@",[index stringByAbbreviatingWithTildeInPath]);

    NSLog(@"6=%@",[index stringByExpandingTildeInPath]);

    NSLog(@"7=%@",[index stringByStandardizingPath]);

    NSLog(@"8=%@",[index stringByResolvingSymlinksInPath]);

    NSLog(@"9=%@",[[index lastPathComponent] stringByDeletingPathExtension]);


對應結果

 1=2013_50.zip

 2=/Users/junzoo/Library/Application Support/iPhone Simulator/7.0.3/Applications/63925F20-AF97-4610-AF1C-B6B4157D1D92/Documents/DownLoad/books

3=zip

 4=/Users/junzoo/Library/Application Support/iPhone Simulator/7.0.3/Applications/63925F20-AF97-4610-AF1C-B6B4157D1D92/Documents/DownLoad/books/2013_50

5=~/Documents/DownLoad/books/2013_50.zip

 6=/Users/junzoo/Library/Application Support/iPhone Simulator/7.0.3/Applications/63925F20-AF97-4610-AF1C-B6B4157D1D92/Documents/DownLoad/books/2013_50.zip

 7=/Users/junzoo/Library/Application Support/iPhone Simulator/7.0.3/Applications/63925F20-AF97-4610-AF1C-B6B4157D1D92/Documents/DownLoad/books/2013_50.zip

8=/Users/junzoo/Library/Application Support/iPhone Simulator/7.0.3/Applications/63925F20-AF97-4610-AF1C-B6B4157D1D92/Documents/DownLoad/books/2013_50.zip

9=2013_50      
iOS