UIScrollerView当前显示3张图

本文介绍了一个基于UIView封装的WSLScrollView组件,支持循环轮播、自动轮播等功能,并详细阐述了其实现原理,包括解决触摸事件响应问题、循环轮播连贯性问题及自动轮播实现。

WSLScrollView功能描述:这是在继承UIView的基础上利用UIScrollerView进行了封装,支持循环轮播、自动轮播、自定义时间间隔、图片间隔、当前页码和图片大小,采用Block返回当前页码和处理当前点击事件的一个View。

结构示意图.png

直接上总的效果图,需要或感兴趣的各路大神朋友请指教:

总效果.gif

①、首先像往常一样写一个基本的UIScrollerView,会得到下图:

    _scrollerView = [[UIScrollView alloc] init];
    _scrollerView.frame = CGRectMake((SELF_WIDTH - _currentPageSize.width) / 2, 0,    _currentPageSize.width, _currentPageSize.height);
    _scrollerView.delegate = self;
    _scrollerView.pagingEnabled = YES;
    _scrollerView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_scrollerView];

基本UIScrollerView.png

然后设置我们通常会忽略UIScrollerView的一个属性clipsToBounds为NO,默认是Yes,你会看到_scrollerView其它部分相邻的图片,但是你会发现那部分相邻的图片不会响应在它上面的任何触摸事件,因为那部分子视图超出了它的父视图,可以用响应链机制解决这个问题:

_scrollerView.clipsToBounds = NO;

//处理超过父视图部分不能点击的问题,重写UIView里的这个方法
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if ([self pointInside:point withEvent:event]) {
        CGPoint newPoint = [_scrollerView convertPoint:point fromView:self];
        for (UIImageView * imageView in _scrollerView.subviews) {
            if (CGRectContainsPoint(imageView.frame, newPoint)) {
                CGPoint newSubViewPoint = [imageView convertPoint:point fromView:self];
                return [imageView hitTest:newSubViewPoint withEvent:event];
            }
        }
    }
    return nil;
}

①效果.gif

②、接下来实现循环的功能:我相信好多人也都会想到 《 4 + 0 - 1 - 2 - 3 - 4 + 0 》这个方案,也就是先在数组的最后插入原数组的第一个元素,再在第一个位置插入原数组的最后一个元素;得到如下图效果:(注意看:第一个向最后一个,最后向第一个循环过渡的时候有个Bug哦)

    self.imageArray = [NSMutableArray arrayWithArray:_images];
    [self.imageArray addObject:_images[0]];
    [self.imageArray insertObject:_images.lastObject atIndex:0];
    //初始化时的x偏移量要向前多一个单位的_currentPageSize.width
    _scrollerView.contentOffset = CGPointMake(_currentPageSize.width * (self.currentPageIndex + 1), 0);

Bug.gif

解决上述Bug的方案就是利用UIScrollView的两个代理方法;在前后循环过渡处,刚开始拖拽时就在Bug的位置画上对应的视图;即《 3 + 4 + 0 - 1 - 2 - 3 - 4 + 0 + 1》,结束拖拽之后,再改变UIScrollView的contentOffset,不带动画;

//开始拖拽时执行
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

   //开始拖拽时停止计时器
    [self.timer invalidate];
    self.timer = nil;

    // 3 + 4 + 0 - 1 - 2 - 3 - 4 + 0 + 1
    NSInteger index = scrollView.contentOffset.x/_currentPageSize.width;

    //是为了解决循环滚动的连贯性问题
    if (index == 1) {
        [self.scrollerView addSubview:self.lastView];
    }
    if (index == self.imageArray.count - 2) {
        [self.scrollerView addSubview:self.firstView];
    }
}

//结束拖拽时执行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    NSInteger index = scrollView.contentOffset.x/_currentPageSize.width;
   //停止拖拽时打开计时器
    if (_isTimer) {
        [self statrScroll:_second];
    }
  //是为了解决循环滚动的连贯性问题
    if (index == 0) {
        scrollView.contentOffset = CGPointMake(_currentPageSize.width * (self.imageArray.count - 2) , 0);
    }
    if (index == self.imageArray.count - 1) {
        scrollView.contentOffset = CGPointMake(_currentPageSize.width  , 0);
    }
}

③实现定时器自动循环轮播功能,需要解决的问题就是首尾过渡的时候,
如下图所示:解决的思路和上述类似,主要代码已标明→WSLScrollView

③效果.gif

- (void)statrScroll:(CGFloat)second{
    if (_timer == nil && _isTimer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:second target:self selector:@selector(autoNextPage) userInfo:nil repeats:YES];
    }
}

- (void)autoNextPage{
    [_scrollerView setContentOffset:CGPointMake( _currentPageSize.width * (_currentPageIndex + 1 + 1), 0) animated:YES]; 

 if (_currentPageIndex + 2 == self.imageArray.count - 1) {
        //是为了解决自动滑动到最后一页再从头开始的连贯性问题
        [_scrollerView addSubview:self.firstView];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat index = scrollView.contentOffset.x/_currentPageSize.width;
    if (index == 0 ) {
        _currentPageIndex = self.imageArray.count - 1- 2;
    }else if(index < 1){

    }else if(index == self.imageArray.count - 1 || index == 1){
        _currentPageIndex = 0;
        //是为了解决自动滑动到最后一页再从头开始的连贯性问题
        [_scrollerView setContentOffset:CGPointMake( _currentPageSize.width , 0) animated:NO];  
    }else if(index == ceil(index)){
        _currentPageIndex = index - 1 ;
    }
    if (self.scrollEndBlock != nil) {
        self.scrollEndBlock(_currentPageIndex);
    }
}

快来赞我啊.gif

不同的开发场景和工具,显示片的方法不同,以下是几种常见的方法: ### LVGL 显示片 ```c // 显示片 void test_img() { lv_obj_t* screen = lv_scr_act(); // 准备片 LV_IMG_DECLARE(ni2); // 创建个 img 对象 lv_obj_t* img = lv_img_create(screen); // 设置片 lv_img_set_src(img, &ni2); } ``` 此代码通过 LVGL 库显示片,先获取当前幕对象,声明片资源,创建片对象并设置其源为声明的片资源[^1]。 ### OpenCV C++ 显示片 ```cpp #include<opencv2/opencv.hpp> using namespace cv; int main() { Mat src = imread("C:\\Users\\32498\\Pictures\\R-C(2).jpg"); namedWindow("测试", WINDOW_NORMAL); imshow("测试", src); waitKey(0); return 0; } ``` 使用 OpenCV 库,通过 `imread` 函数读取片,创建个窗口,使用 `imshow` 函数在窗口中显示片,最后使用 `waitKey` 等待按键事件[^2]。 ### Qt 使用 `setPixmap` 显示片 ```cpp // 假设 label 重命名为 picture ui->picture->setPixmap(QPixmap(":/new/prefix1/pic.bmp")); ``` 此代码在 Qt 中使用 `setPixmap` 方法将指定路径的显示在 `QLabel` 上[^3]。 ### Qt 加载并显示片 ```cpp // mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include <QLabel> #include <QMovie> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QImage myImage; myImage.load("C:\\Users\\rw\\Desktop\\test.png"); ui->labelImage->setPixmap(QPixmap::fromImage(myImage).scaled(ui->labelImage->size())); } MainWindow::~MainWindow() { delete ui; } ``` 在 Qt 项目中,创建 `QImage` 对象加载片,将其转换为 `QPixmap` 并调整大小后显示在 `QLabel` 上[^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值