前两天一哥们儿让我帮他写一下:可循环滚动并突出中间图片,并且可点击的一种滑动视图的效果,今天放在这里给大家展示一下,具体文字代码中都有注解,代码还有待完善,不喜勿喷,转载请注明,下载请点星,谢谢~

-(void)addItemViewWithArray:(NSArray *)imgArr titleArray:(NSArray *)titleArr
{
    //这里因为我要保证屏幕上至少能够出现三个完整的,两边两个部分内容,所以我写了4
    if (_imgArray.count<4) {
        return;
    }
    //设计item大小,中间比两边的宽、高各大30,间距为10,距离左右边界为30
    itemWidthF = (_frameE.size.width-30*2-30-20)/3;
    itemHeightF = _frameE.size.height-30;
    bigItemWidthF = itemWidthF+30;
    bigItemHeightF = itemHeightF+30;
    
    //中心值
    xCenterF = _frameE.size.width/2.0;
    
    //创建新的数组
    NSMutableArray *newImgArr = [NSMutableArray new];
    NSMutableArray *newTitleArr = [NSMutableArray new];
    for (int i = 0; i < imgArr.count+4; i++) {
        if (i < 2) {//0,1
            [newImgArr addObject:imgArr[imgArr.count+i-2]];
            [newTitleArr addObject:titleArr[titleArr.count+i-2]];
        }else if(i >= 2 && i < imgArr.count+2){//2,3,.....(imgArr.count-1)
            [newImgArr addObject:imgArr[i-2]];
            [newTitleArr addObject:titleArr[i-2]];
        }else{
            [newImgArr addObject:imgArr[i-imgArr.count-2]];
            [newTitleArr addObject:titleArr[i-imgArr.count-2]];
        }
    }
    
    //记录数组数量
    NSInteger totalNum = newImgArr.count;
    for (int i = 0; i < totalNum; i++) {
        
        UIButton *bgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [bgBtn setTitle:newTitleArr[i] forState:UIControlStateNormal];
        [bgBtn setBackgroundColor:[UIColor blueColor]];
        bgBtn.layer.cornerRadius = 5;
        bgBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        bgBtn.layer.borderWidth = 1.0;
        [bgBtn setBackgroundImageForState:UIControlStateNormal withURL:[NSURL URLWithString:newImgArr[i]] placeholderImage:[UIImage imageNamed:@""]];
        [bgBtn addTarget:self action:@selector(bgBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        bgBtn.userInteractionEnabled = NO;
        if (i == 2) {
            bgBtn.frame = CGRectMake(30+(itemWidthF+10)*i, 0, bigItemWidthF, bigItemHeightF);
            bgBtn.userInteractionEnabled = YES;
            
            [_bgScrollV setContentOffset:CGPointMake(((30+(itemWidthF+10)*i)+bigItemWidthF/2)-xCenterF, 0) animated:YES];
            
            _currentInt = i;
            
        }else if(i>2){
            bgBtn.frame = CGRectMake(30+(itemWidthF+10)*i+30, 15, itemWidthF, itemHeightF);
        }else{
            bgBtn.frame = CGRectMake(30+(itemWidthF+10)*i, 15, itemWidthF, itemHeightF);
        }
        
        bgBtn.tag = i+100;
        
        [_bgScrollV addSubview:bgBtn];
        
    }
    _bgScrollV.contentSize = CGSizeMake((itemWidthF+10)*totalNum+30+30*2, bigItemHeightF);
    
    [self addgesture];
}

//手势方法
-(void)transitionPush:(UISwipeGestureRecognizer *)swipeGes
{
    [UIView animateWithDuration:0.2 animations:^{
        UIButton *oldBtn = (UIButton *)[_bgScrollV viewWithTag:_currentInt+100];
        //取消旧按钮的交互
        oldBtn.userInteractionEnabled = NO;
        //NSLog(@"输出1111--------%ld",_currentInt);
        if (swipeGes.direction == UISwipeGestureRecognizerDirectionLeft){
            if (_currentInt==_imgArray.count+2-1) {
                NSLog(@"已经是最后一张了....");
                
                //回到最原始的位置,记录的是数组中的索引值
                _currentInt = 2;
                
                //变大的后面的按钮到最后一个的位置都变小
                for (int i = 3; i<_imgArray.count+2; i++) {
                    UIButton *allOldBtn = (UIButton *)[_bgScrollV viewWithTag:i+100];
                    allOldBtn.frame = CGRectMake(30+(itemWidthF+10)*i+30, 15, itemWidthF, itemHeightF);
                }
            }else{
                
                _currentInt = _currentInt+1;
                
                //旧的按钮变小,比下面的差了30的距离
                oldBtn.frame = CGRectMake(30+(itemWidthF+10)*(_currentInt-1), 15, itemWidthF, itemHeightF);
            }
            
        }
        if(swipeGes.direction == UISwipeGestureRecognizerDirectionRight){
            if (_currentInt==2) {
                NSLog(@"已经是第一张了....");
                
                //回到最后的位置,记录的是数组中的索引值
                _currentInt = _imgArray.count+2-1;
                
                //变大的后面的按钮到_currentInt==2的位置都变小
                for (int i = 2; i<_imgArray.count+2-1; i++) {
                    UIButton *allOldBtn = (UIButton *)[_bgScrollV viewWithTag:i+100];
                    allOldBtn.frame = CGRectMake(30+(itemWidthF+10)*i, 15, itemWidthF, itemHeightF);
                }
            }else{
                _currentInt = _currentInt-1;
                
                //旧的按钮变小
                oldBtn.frame = CGRectMake(30+(itemWidthF+10)*(_currentInt+1)+30, 15, itemWidthF, itemHeightF);
            }
        }
        
        //新的变大
        UIButton *newBtn = (UIButton *)[_bgScrollV viewWithTag:_currentInt+100];
        newBtn.frame = CGRectMake(30+(itemWidthF+10)*_currentInt, 0, bigItemWidthF, bigItemHeightF);
        //添加新按钮的交互
        newBtn.userInteractionEnabled = YES;
        
        [_bgScrollV setContentOffset:CGPointMake(((30+(itemWidthF+10)*_currentInt)+bigItemWidthF/2)-xCenterF, 0) animated:YES];
        
        //NSLog(@"输出2222--------%ld",_currentInt);
    }];
}

效果图:(源码下载:https://github.com/hbblzjy/OCSelectImgDemo)

      

OC基础之可循环滚动并突出中间图片,并且可点击的更多相关文章

  1. 【OC基础语法考试】

    OC基础语法已经全部学完,但是这些知识只是最基础的,还有很多高级知识,这个可能需要后面慢慢的去学习才能体会到.接下来我会总结前面的OC基础语法,如果大家发现有什么不正确的地方,请指正,小弟是新生,多请 ...

  2. iOS 阶段学习第11天笔记(OC基础知识)

    iOS学习(OC语言)知识点整理 一.OC基础知识 1)#import  用于导入头文件,预处理阶段加载引用,只加载一次. 2)OC 依赖于Foundation框架下的头文件Foundation.h, ...

  3. OC基础笔记目录

    OC基础(1) Objective-C简介 OC和C对比 第一个OC程序 面向对象思想 OC基础(2) 类与对象 类的设计 第一个OC类 对象方法的声明和实现 类方法的声明和实现 OC基础(3) 对象 ...

  4. OC基础 文件管理

    OC基础  文件管理 1.文件管理类NSFileManager对象的创建: NSFileManager *fm = [NSFileManager defaultManager]; 2.文件操作: (1 ...

  5. OC基础 NSDate

    OC基础  NSDate #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @auto ...

  6. OC基础 NSData

    OC基础 NSData 1.NSString转NSData //NSString转NSData NSString *string = @"abcd12345"; NSData *d ...

  7. OC基础 代理和协议

    OC基础 代理和协议 1.协议 (1)oc语言中得协议:一组方法列表,不需要我们自己实现,由遵守协议的类来实现协议所定制的方法. (2)协议的使用步骤:制定协议-->遵守协议-->实现协议 ...

  8. OC基础 内存管理

    OC基础  内存管理 我们所了解的c语言内存管理,如下: (1)c语言的内存分配:char *p = (char*)malloc(100*sizeof(char)); (2)c语言的内存释放:free ...

  9. OC基础 类的三大特性

    OC基础  类的三大特性 OC的类和JAVA一样,都有三大特性:继承,封装,多态,那么我们就来看一下OC中类的三大特性. 1.继承 继承的特点: (1)子类从父类继承了属性和方法. (2)子类独有的属 ...

随机推荐

  1. MIT许可证

    MIT许可证(The MIT License)是许多软件授权条款中,被广泛使用的其中一种.与其他常见的软件授权条款(如GPL.LGPL.BSD)相比,MIT是相对宽松的软件授权条款. MIT 许可证几 ...

  2. Java图形界面学习---------简易登录界面

    /** * @author Administrator * Java图形界面学习---------简易登录界面 * date:2015/10/31 */ import java.awt.BorderL ...

  3. 确认过眼神,你是喜欢Stream的人

    摘要:在学习Node的过程中,Stream流是常用的东东,在了解怎么使用它的同时,我们应该要深入了解它的具体实现.今天的主要带大家来写一写可读流的具体实现,就过来,就过来,上码啦! 码前准备 在写代码 ...

  4. [LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词

    Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...

  5. [HNOI 2011]数学作业

    Description 小 C 数学成绩优异,于是老师给小 C 留了一道非常难的数学作业题: 给定正整数 N 和 M,要求计算 Concatenate (1 .. N) Mod M 的值,其中 Con ...

  6. [HNOI2010]MATRIX 矩阵

    Description Input 第一行包含三个正整数N M P表示矩阵的行数列数以及每个数的范围,接下来N行每行包含M个非负整数,其中第i行第j个数表示以格子(i,j)为右下角的2*2子矩阵中的数 ...

  7. Chtholly Nota Seniorious

    题目背景 大样例下发链接: https://pan.baidu.com/s/1nuVpRS1 密码: sfxg こんなにも.たくさんの幸せをあの人に分けてもらった だから.きっと 今の.私は 谁が何と ...

  8. POJ 3233 (矩阵)

    题意:对于矩阵A,求A^1 + ...... + A^k 按照矩阵十大经典题的思路大致做了下. 在k为奇数时:  A^( k / 2+1)+ 1) * (A^1 + ....... A^(k/2)) ...

  9. hzoj 2301(莫比乌斯反演)

    题意 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公 数. 思路: 与先前的那个相比,这次a,c并不一定 ...

  10. .net如何引用System.Drawing.Drawing2D 命名空间和System.Drawing.Image及其相关概念

    其实这个很简单,直接在引用那里单击右键选择添加框架,然后找到System.Drawing就OK了, 其实并没有网上所说的那样需要下载什么Drawing.BLL. 首先Syetem.Drawing.Dr ...