IOS開發基礎知識--片段14,ios基礎知識--片段

來源:互聯網
上載者:User

IOS開發基礎知識--片段14,ios基礎知識--片段

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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.