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

 

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傳值ide

複製代碼
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:實現無限滾動的uiscrollviewpost

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

/**
 *  @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)];
        }
    }
}
複製代碼
相關文章
相關標籤/搜索