轮播器

无限轮播器,可在LJBHeadScrollView.m中自定义修改为SDWebImage加载方式,计时器和分页器可选设置。

1.

@protocol LJBHeadScrollViewDelegate <NSObject>


@optional

- (void)LJBHeadScrollView:(UIView *)view index:(NSInteger)index;


@end


@interface LJBHeadScrollView : UIView


/** 是否显示UIPageControl */

@property(assign)BOOL showPage;


/** 是否启用计时器 */

@property(assign)BOOL useTimer;


/** 计时器时间 */

@property(assign)NSTimeInterval times;


/** 图片集合 */

@property(nonatomic,strong)NSArray *imageLists;


@property(assign)id<LJBHeadScrollViewDelegate>delegate;

2.

@interface LJBHeadScrollView ()<UIScrollViewDelegate>


@property(nonatomic,weak)UIScrollView *scrollView;

@property(nonatomic,weak)UIPageControl *page;

@property(nonatomic,strong)NSTimer *timer;

@end


@implementation LJBHeadScrollView


- (void)setUpVc {

    if (self.scrollView) {

        [self.scrollView removeFromSuperview];

    }

    CGFloat padding = 10;

    UIScrollView *scrollView = [[UIScrollView alloc] init];

    self.scrollView = scrollView;

    scrollView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);

    scrollView.pagingEnabled = YES;

    scrollView.showsHorizontalScrollIndicator = NO;

    scrollView.showsVerticalScrollIndicator = NO;

    scrollView.delegate = self;

    [self addSubview:scrollView];

    if (self.page) {

        [self.page removeFromSuperview];

    }

    if (self.showPage) {

        UIPageControl *page = [[UIPageControl alloc] init];

        self.page = page;

        page.numberOfPages = self.imageLists.count;

        page.currentPage = 0;

        CGFloat pageWidth = padding * page.numberOfPages;

        CGFloat pageHeight = padding;

        page.frame = CGRectMake(self.frame.size.width * 0.5 - pageWidth * 0.5, self.frame.size.height - pageHeight - 2 * padding, pageWidth, pageHeight);

        page.pageIndicatorTintColor = [UIColor lightGrayColor];

        page.currentPageIndicatorTintColor = [UIColor orangeColor];

        [self addSubview:page];

    }

}


- (void)setImageLists:(NSArray *)imageLists {

    _imageLists = imageLists;

    [self setUpVc];

    

    NSInteger count = imageLists.count < 2?imageLists.count:(imageLists.count + 2);

    for (int i = 0; i < count; i++) {

        UIImageView *imageView = [[UIImageView alloc] init];

        NSString *image = nil;

        if (i == 0) {

            image = [imageLists lastObject];

        } else if (i == count - 1) {

            image = [imageLists firstObject];

        } else {

            image = imageLists[i - 1];

        }

        imageView.image = [UIImage imageNamed:image];

        imageView.frame = CGRectMake(i * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);

        imageView.tag = i - 1;

        imageView.contentMode = UIViewContentModeScaleAspectFit;

        imageView.userInteractionEnabled = YES;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)];

        [imageView addGestureRecognizer:tap];

        [self.scrollView addSubview:imageView];

    }

    self.scrollView.contentSize = CGSizeMake(count * self.scrollView.frame.size.width, 0);

    self.scrollView.contentOffset = CGPointMake(count < 2?0:self.scrollView.frame.size.width, 0);

    

    [self createTimer];

}


- (void)createTimer {

    [self.timer invalidate];

    self.timer = nil;

    if (self.useTimer && self.times) {

        self.timer = [NSTimer scheduledTimerWithTimeInterval:self.times target:self selector:@selector(changeImage) userInfo:nil repeats:YES];

        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];

    }

}


- (void)changeImage {

    NSInteger count = self.imageLists.count < 2?self.imageLists.count:(self.imageLists.count + 2);

    if (count >= 2) {

        CGPoint point = self.scrollView.contentOffset;

        if (point.x == (count - 1) * self.scrollView.frame.size.width

            || point.x == (count - 2) * self.scrollView.frame.size.width) {

            point.x = self.scrollView.frame.size.width;

            self.scrollView.contentOffset = point;


        } else {

            point.x += self.scrollView.frame.size.width;

            [UIView animateWithDuration:0.5 animations:^{

                self.scrollView.contentOffset = point;

            }];


        }

    }

}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    [self createTimer];

}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    CGPoint point = scrollView.contentOffset;

    NSInteger count = self.imageLists.count < 2?self.imageLists.count:(self.imageLists.count + 2);

    if (count >= 2) {

        if (point.x >= scrollView.frame.size.width * (count - 1)) {

            scrollView.contentOffset = CGPointMake(scrollView.frame.size.width, 0);

        }

        if (point.x <= 0) {

            scrollView.contentOffset = CGPointMake(scrollView.frame.size.width * (count - 2), 0);

        }

        point = scrollView.contentOffset;

        NSInteger currentPage = (point.x + self.scrollView.frame.size.width * 0.5) / self.scrollView.frame.size.width;


        self.page.currentPage = currentPage - 1;

        

    }

}


- (void)tapImage:(UITapGestureRecognizer *)tap {

    NSInteger tag = tap.view.tag;

    if ([self.delegate respondsToSelector:@selector(LJBHeadScrollView:index:)]) {

        [self.delegate LJBHeadScrollView:self index:tag];

    }

}

为了实现Django图片轮播,你可以按照以下步骤进行操作: 1.在你的Django项目中创建一个名为“media”的文件夹,用于存储你的图片。 2.在你的项目根目录的urls.py文件中添加以下代码,以便在调试模式下提供对媒体文件的访问: ```python from django.conf import settings from django.conf.urls.static import static if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ``` 3.在你的应用程序中创建一个名为“models.py”的文件,并定义一个名为“Image”的模型,该模型将包含你要在轮播中显示的图像。例如: ```python from django.db import models class Image(models.Model): title = models.CharField(max_length=200) image = models.ImageField(upload_to='images/') def __str__(self): return self.title ``` 4.运行以下命令以创建数据库表: ```python python manage.py makemigrations python manage.py migrate ``` 5.在你的应用程序中创建一个名为“views.py”的文件,并定义一个名为“home”的视图,该视图将从数据库中检索所有图像并将它们传递给模板。例如: ```python from django.shortcuts import render from .models import Image def home(request): images = Image.objects.all() return render(request, 'home.html', {'images': images}) ``` 6.在你的应用程序中创建一个名为“templates”的文件夹,并在其中创建一个名为“home.html”的文件。在该文件中,你可以使用Bootstrap或其他CSS框架来创建一个轮播,并使用Django模板标记来循环遍历所有图像。例如: ```html <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel"> <ol class="carousel-indicators"> {% for image in images %} <li data-target="#carouselExampleIndicators" data-slide-to="{{ forloop.counter0 }}" {% if forloop.first %}class="active"{% endif %}></li> {% endfor %} </ol> <div class="carousel-inner"> {% for image in images %} <div class="carousel-item {% if forloop.first %}active{% endif %}"> <img class="d-block w-100" src="{{ image.image.url }}" alt="{{ image.title }}"> <div class="carousel-caption d-none d-md-block"> <h5>{{ image.title }}</h5> </div> </div> {% endfor %} </div> <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev"> <span class="carousel-control-prev-icon" aria-hidden="true"></span> <span class="sr-only">Previous</span> </a> <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next"> <span class="carousel-control-next-icon" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> </div> ``` 7.在你的应用程序中创建一个名为“urls.py”的文件,并定义一个名为“home”的URL模式,该模式将指向你在第5步中创建的视图。例如: ```python from django.urls import path from . import views urlpatterns = [ path('', views.home, name='home'), ] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值