[UIScrollview 卡片式无限自动轮播]

news/2024/7/20 20:09:03 标签: ios, uiscrollview, 分页, 无限轮播, 卡片轮播

卡片式无限自动轮播源码参考

scrollView的属性设置及解释

1.pagingEnable = yes  ; 开启分页模式,(ps为达到分页效果,可以设置contentSize为subView的整数倍width等)
 (iOS 自定义scrollView的pagingEnabled http://blog.csdn.net/liuxu0718/article/details/48344295)

2.clipsToBounds = NO; 关闭剪切图片,达到轮播的效果 (UIView里面都是默认开启的,只有UIScrollView默认关闭

3.ScrollsToTop  = YES;  该属性在垂直滚动时, 如果为YES则 点击状态栏会使UIScrollView滚动回初始位置
(谈一谈UIScrollView的scrollsToTop属性 http://www.jianshu.com/p/b4ac529950af)

4
CGRectInset CGRectoffset UIEdgeInsetsInsetRect 这三个函数的使用情况
(http://blog.csdn.net/ys410900345/article/details/42924827)

无限轮播的实现">第一部分:无限轮播的实现

下面是我画的图解,希望大家能看懂
这里写图片描述

RotateScrollview类的源码

仅用于解释上面的过程,复制过去可以自己实现效果

这里写图片描述
源码

#import "RotateScrollview.h"

@interface RotateScrollview ()<UIScrollViewDelegate>
/**
 *  原始页数
 */
@property(nonatomic, assign) NSInteger orginPageCount;

/**
 *  总页数
 */
@property(nonatomic, assign) NSInteger pageCount;

/**
 *  一页的尺寸
 */
@property(nonatomic, assign) CGSize pageSize;

/**
 左右间距,默认20
 */
@property(nonatomic, assign) CGFloat leftRightMargin;

/**
 上下间距,默认30
 */
@property(nonatomic, assign) CGFloat topBottomMargin;



@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation RotateScrollview
@synthesize orginPageCount;
@synthesize pageCount;
@synthesize pageSize;
@synthesize leftRightMargin;
@synthesize topBottomMargin;


-(instancetype)initWithFrame:(CGRect)frame{

    if ([super initWithFrame:frame]) {
        [self initsilize];
    }return self;

}

- (void)initsilize{
    leftRightMargin = 10;
    topBottomMargin = 15;

    pageSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height);
    orginPageCount = 5;
    pageCount = orginPageCount*3;
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    self.scrollView.clipsToBounds = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.showsVerticalScrollIndicator = YES;
    self.scrollView.showsHorizontalScrollIndicator = YES;

    [self addSubview:self.scrollView];

    for (int i = 0; i<15; i++) {
        UIImageView *imgV = [[UIImageView alloc] initWithFrame:self.bounds];
        //第一组
        if (i<=4) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Yosemite%02d",i]];
            imgV.image = image;
        }
        //第二组
        if (i<=10&&i>4) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Yosemite%02d",i-5]];
            imgV.image = image;
        }
        //第三组
        if (i<15&&i>9) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Yosemite%02d",i-10]];
            imgV.image = image;
        }
        imgV.frame = CGRectMake(pageSize.width*i, 0, pageSize.width, pageSize.height);
        [self.scrollView addSubview:imgV];
    }

    self.scrollView.contentSize = CGSizeMake(pageSize.width*pageCount, 0);

    //设置scrollview的当前显示状态为 第二组
    [self.scrollView setContentOffset:CGPointMake(pageSize.width*orginPageCount*2, 0)];
}

#pragma  - mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    //获取拖拽的下标
    NSInteger pageIndex;
    pageIndex = (int)round(_scrollView.contentOffset.x / pageSize.width) % orginPageCount;


    //向左滑动实现无限滚动
    if (scrollView.contentOffset.x / pageSize.width >= 2 * self.orginPageCount) {
        NSLog(@"%.f",scrollView.contentOffset.x / pageSize.width);
        //回到
        [scrollView setContentOffset:CGPointMake(pageSize.width * self.orginPageCount, 0) animated:NO];

    }
    //向右滑动实现无限滚动
    if (scrollView.contentOffset.x/ pageSize.width < self.orginPageCount -1) {

        [scrollView setContentOffset:CGPointMake(pageSize.width * (self.orginPageCount*2-1), 0) animated:NO];
    }

}

第二部分:卡片式动画的实现

(节后来补, ^(* ̄(oo) ̄)^大家节日快乐)


http://www.niftyadmin.cn/n/1557269.html

相关文章

上传图片并在页面回显图片

这是根据自己写的一个教师管理系统实现的 由于主要用在头像上&#xff0c;把上传的头像存储到本地 为了方便&#xff0c;设置一个虚拟地址&#xff1a; 然后添加虚拟路径和实际路径即可 前台&#xff1a; <div class"pic" style"opacity: 1"><a i…

[iOS 16进制颜色转换RGB](转)

宏定义方法 #define RGB(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]#define HexToRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/25…

WPF 自定义IconButton

原文:WPF 自定义IconButton自定义一个按钮控件 按钮控件很简单&#xff0c;我们在项目中有时把样式封装起来&#xff0c;添加依赖属性&#xff0c;也是为了统一。 这里举例&#xff0c;单纯的图标控件怎么设置 1、UserControl界面样式 <UserControl x:Class"WpfApplica…

[ViewController在导航控制器下, UIScrollview及其子类下移64问题]

NavigationController中控制器视图的自动64px偏移详解 http://www.jianshu.com/p/c060932cde33 // http://www.cnblogs.com/small-octopus/p/4746411.html 比较简单的解决方法 //配置所有ScrollviewView子类不被navgationController下压64self.automaticallyAdjustsScrollV…

吴恩达机器学习笔记-反向传播算法练习

直观感受反向传播的概念 上篇文章讲述了神经网络的反向传播算法的基本概念&#xff0c;现在来详细的对此算法进行一些讲解。回忆一下神经网络的代价函数&#xff1a; 如果我们只考虑一个简单的只有一个输出单元的情况&#xff0c;即k1&#xff0c;那么代价函数则变成&#xff1…

[收藏]iPhone屏幕尺寸、分辨率及适配

http://blog.csdn.net/phunxm/article/details/42174937/ //UI设计常用iOS 机型参数 安卓机型参数 http://tool.lanrentuku.com/guifan/ui.html

应用上云可以有多快?

1 摘要本文介绍了为什么在一个好的公有云或私有云中必须要有一个编排系统来支持云上自动化&#xff0c;以及实现这个编排系统的困难和各家的努力。同时提供了一套实现编排系统的原型&#xff0c;它包括了理论分析及主体插件框架&#xff0c;还给出一些细节控制的建议。希望有助…

[UIKBBlurredKeyView candidateList]:报错

场景介绍 在ViewController的View上添加Scrollview-全屏 在该Scrollview上添加textfiled控件,为了便捷退出键盘.添加 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{} 方法. 发现touchsBegan方法不能被相应,原因是Scrollview将事件捕…