zoukankan      html  css  js  c++  java
  • iOS开发学习--纯代码 UIScrollView 无限循环的实现——代码类封装

    一个简单的利用UIScrollView 实现的无线滚动banner,下面的代码实现,因为封装问题,对两个及一下的view 支持出了一点问题(view是传参进来的,不可以生成两份),但是原理是正确的,智者见智吧

    先说一下原理,看下面的图

    第一种原理       , 本文是用的原理

    为了方便看懂,这里做了2张动图

    下面是解析图

        

    第二种原理

    原理清晰了,代码附上,有问题可以留言,会尽快回复,

    .h文件

     1 //
     2 //  MyUIScrollView.h
     3 //  UIscrollView
     4 //
     5 //  Created by Ibokan on 15/8/25.
     6 //  Copyright (c) 2015年 Crazy凡. All rights reserved.
     7 //
     8 
     9 #import <UIKit/UIKit.h>
    10 
    11 @interface MyUIScrollView : NSObject
    12 @property (nonatomic,strong)UIScrollView *scrollView; //滚动框
    13 @property (nonatomic,strong)UILabel * label;//titlelabel,要想显示图片title 需要先将label 贴到view中,并且settitle,如果不调用settitle 方法,则不会显示
    14 @property (nonatomic,strong)UIPageControl * pageControl;//显示小点
    15 //以上三个控件都需要调用 [self.view addSubView:]方法,否则不会显示
    16 @property BOOL isCarousel;//是否有动画  默认有 View  数量为一张  会关闭动画
    17 @property double delay;//动画播放速度 默认1.5 最小应 > 0.2
    18 - (instancetype)initWithFrame:(CGRect)frame ViewNum:(int)num Margin:(int)margin;//frame是全部尺寸,num是View数量,margin是内容和外边框距离
    19 - (instancetype)initWithFrame:(CGRect)frame ViewNum:(int)num MarginV:(int)marginV MarginH:(int)marginH;//垂直和水平边距
    20 - (instancetype)initWithFrame:(CGRect)frame ViewNum:(int)num MarginT:(int)marginTop MarginR:(int)marginRight MarginB:(int)marginBottom MarginL:(int)marginLeft;//上右下左边距
    21 - (void)setSubView:(NSArray *)viewArray;//填充view,最好多于2个,数量为2的请复制成4个,数量为1的将不允许滑动,2个以上的不需要做特殊处理
    22 - (void)setTitle:(NSArray *)title;
    23 @end
    View Code

    .m 文件

      1 //
      2 //  MyUIScrollView.m
      3 //  UIscrollView
      4 //
      5 //  Created by Ibokan on 15/8/25.
      6 //  Copyright (c) 2015年 Crazy凡. All rights reserved.
      7 //
      8 
      9 #import "MyUIScrollView.h"
     10 
     11 @interface MyUIScrollView () <UIScrollViewDelegate>
     12 @property (nonatomic,strong)NSTimer * timer;
     13 @property (nonatomic,strong)NSMutableArray *labeltitle;
     14 @property (nonatomic,strong)NSMutableArray *viewArray;
     15 @property (nonatomic,strong)NSMutableArray *titleArray;
     16 @property int indexc;
     17 @property BOOL isOne; //记录图片是否只有一张
     18 @end
     19 
     20 @implementation MyUIScrollView
     21 - (instancetype)initWithFrame:(CGRect)frame ViewNum:(int)num Margin:(int)margin
     22 {
     23     [self makeWF:frame VN:num MT:margin MR:margin MB:margin ML:margin];
     24     return self;
     25 }
     26 - (instancetype)initWithFrame:(CGRect)frame ViewNum:(int)num MarginV:(int)marginV MarginH:(int)marginH
     27 {
     28     [self makeWF:frame VN:num MT:marginV MR:marginH MB:marginV ML:marginH];
     29     return self;
     30 }
     31 - (instancetype)initWithFrame:(CGRect)frame ViewNum:(int)num MarginT:(int)marginTop MarginR:(int)marginRight MarginB:(int)marginBottom MarginL:(int)marginLeft;
     32 {
     33     [self makeWF:frame VN:num MT:marginTop MR:marginRight MB:marginBottom ML:marginLeft];
     34     return self;
     35 }
     36 - (void) makeWF:(CGRect)frame VN:(int)num MT:(int)marginT MR:(int)marginR MB:(int)marginB ML:(int)marginL
     37 {
     38     _isOne = num == 1 ? YES : NO ;
     39     self.label = [[UILabel alloc] initWithFrame: CGRectMake(0,frame.size.height-60,frame.size.width,20)];
     40     self.label.textAlignment = NSTextAlignmentCenter;//label 初始化
     41     self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(frame.size.width - num * 17 ,frame.size.height-30,num * 15, 20)];
     42     self.pageControl.numberOfPages = num;
     43     self.indexc = 0; 
     44     num = 3;
     45     self.scrollView = [[UIScrollView alloc]initWithFrame:frame];
     46     self.scrollView.pagingEnabled = YES;
     47     self.scrollView.delegate = self;
     48     self.scrollView.contentSize = CGSizeMake(frame.size.width * num , frame.size.height);
     49     int i = 0;
     50     while(i < num){
     51         UIView *v = [[UIView alloc]initWithFrame:CGRectMake(i * frame.size.width + marginL, marginT, frame.size.width - marginL - marginR, frame.size.height - marginT - marginB)];
     52         v.clipsToBounds = YES;
     53         [self.scrollView addSubview:v];
     54         i++;
     55     }
     56     CGPoint point =  CGPointMake(frame.size.width,0);
     57     self.scrollView.contentOffset = point;
     58     self.scrollView.showsHorizontalScrollIndicator = NO;
     59     self.isCarousel = YES;//默认自动交换
     60     self.isCarousel ? self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(autochange) userInfo:nil repeats:YES] : nil;
     61 }
     62 - (int)checkindex:(int)temp
     63 {
     64     return (temp < 0 ? temp = (int)(self.viewArray.count -1) : (temp == (int)(self.viewArray.count) ? 0 : temp));
     65 }
     66 - (void)autochange//自动切换
     67 {
     68     _isOne ? nil : [UIView animateWithDuration:(self.delay > 0.2 ? self.delay : 1.5 )  animations:^{ self.scrollView.contentOffset = CGPointMake( self.scrollView.frame.size.width * 2 - 1 , 0); } completion:^(BOOL finished) { self.scrollView.contentOffset = CGPointMake( self.scrollView.frame.size.width * 2 , 0); }];
     69 }
     70 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
     71 {
     72     self.scrollView.contentOffset = _isOne ? CGPointMake(self.scrollView.frame.size.width,0) : self.scrollView.contentOffset ;// 只有一张图,强行不允许移动
     73     if((int)self.scrollView.contentOffset.x == 0)
     74     {
     75         self.indexc = [self checkindex:--self.indexc];
     76         [self changeimg];
     77     }
     78     if(fabs(self.scrollView.contentOffset.x - 2 * self.scrollView.frame.size.width) < 1e-10)
     79     {
     80         self.indexc = [self checkindex:++self.indexc];
     81         [self changeimg];
     82     }
     83 }
     84 - (void)changeimg
     85 {
     86     self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width,0);
     87     [self.scrollView.subviews[0] addSubview:_viewArray[[self checkindex:(self.indexc-1)]]];
     88     [self.scrollView.subviews[2] addSubview:_viewArray[[self checkindex:(self.indexc+1)]]]; 
     89     [self.scrollView.subviews[1] addSubview:_viewArray[self.indexc]];
     90     self.pageControl.currentPage = self.indexc;
     91     _labeltitle ? [self.label setText:_labeltitle[self.indexc]]:nil;
     92 }
     93 - (void)setSubView:(NSArray *)viewArray
     94 {
     95     _viewArray = [[NSMutableArray alloc]initWithArray:viewArray];
     96     [self changeimg];
     97 }
     98 - (void)setTitle:(NSArray *)title
     99 {
    100     _labeltitle = [[NSMutableArray alloc]initWithArray:title];
    101     [self.label setText:_labeltitle[0]];
    102 }
    103 @end
    View Code

    代码被封装了,可以直接用我写的这个类生成一个变量,代码中有对各个属性的解释

    代码源文件

  • 相关阅读:
    【AtCoder】ARC067 F
    【AtCoder】ARC095 E
    【BZOJ】4559: [JLoi2016]成绩比较 计数DP+排列组合+拉格朗日插值
    【CodeForces】961 F. k-substrings 字符串哈希+二分
    【CodeForces】961 G. Partitions 斯特林数
    【BZOJ】2310: ParkII 插头DP
    【BZOJ】2331: [SCOI2011]地板 插头DP
    webpack从0开始---(二)
    webpack从0开始---(一)
    前端基础知识(不应需要思考的知识点)三
  • 原文地址:https://www.cnblogs.com/kongkaikai/p/4759420.html
Copyright © 2011-2022 走看看