码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发基础知识--碎片14

时间:2016-08-21 11:05:45      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

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)];
        }
    }
}
技术分享

iOS开发基础知识--碎片14

标签:

原文地址:http://www.cnblogs.com/LiLihongqiang/p/5792241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!