iOS 上下滚动轮播的实现

上次写了一个关于左右滚动使用scroll实现的轮播,今天闲着没事也搞了一个上下滚动的字体轮播,大致思路是一样的。

亦菲表演机器猫
图片发自简书App

如果想实现上下滚动的轮播,首先要确定几点

1.scroll可见范围(可滚动范围)
2.contentSize(最大滚动范围)
3.当然还有数据源,和左右滚动类似,item+2

好了,确定了这几点有了思路就可以直接来代码了
声明属性
@property (strong, nonatomic) UIScrollView * verticalScroll;//承载数据的父视图
@property (strong, nonatomic) NSArray * titleArrays;//数据源
@property (strong, nonatomic) NSTimer * myTimer;//定时器管控轮播
数据源(这里对数据做了处理,数量+2)
-(NSArray *)titleArrays{
    if (!_titleArrays) {
        _titleArrays = [NSArray arrayWithObjects:@"今天是一个好天气",@"温度达到了30度以上",@"可是我并没有感觉很热",@"因为什么呢",@"公司开空调了",@"这个是不是可以有啊",@"今天是一个好天气",@"温度达到了30度以上", nil];
    }
    return _titleArrays;

}
父视图包括数据的创建
-(UIScrollView *)verticalScroll{
    if (!_verticalScroll) {
        _verticalScroll = [[UIScrollView alloc]init];
        _verticalScroll.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
        _verticalScroll.bounds = CGRectMake(0, 0, 130, 60);
        //_verticalScroll.pagingEnabled = YES;
        _verticalScroll.showsVerticalScrollIndicator = NO;
        _verticalScroll.scrollEnabled = NO;
        _verticalScroll.bounces = NO;
        _verticalScroll.delegate = self;

        [self.view addSubview:_verticalScroll];
        CGFloat scaleH = 20;
        CGFloat Height = 20;
        CGFloat H = 0;
        for (int i =0; i<self.titleArrays.count; i++) {
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(10, H+scaleH, CGRectGetWidth(_verticalScroll.frame)-20, Height);
            [button setTitle:self.titleArrays[i] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            button.tag = i+10;
            [_verticalScroll addSubview:button];


            H = button.frame.origin.y+button.frame.size.height+scaleH;
        }
        _verticalScroll.contentSize = CGSizeMake(0, H);

    }
    return _verticalScroll;
}
还差一个管控无限轮播的定时器(我是在进入界面的时候就创建的,可根据项目需求来)
-(void)viewWillAppear:(BOOL)animated{
    self.verticalScroll.backgroundColor = [UIColor whiteColor];

    _myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(changeScrollContentOffSetY) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_myTimer forMode:NSRunLoopCommonModes];
}
实现定时器方法
-(void)changeScrollContentOffSetY{
    //启动定时器
    CGPoint point = self.verticalScroll.contentOffset;
    [self.verticalScroll setContentOffset:CGPointMake(0, point.y+CGRectGetHeight(self.verticalScroll.frame)) animated:YES];
}
当然了,滚动代理也是要有的。因为这里没有考虑手动滑动可以滚动的情况,所以只写一个代理协议即可,代码如下

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSLog(@"endani");
    if (scrollView.contentOffset.y==scrollView.contentSize.height-CGRectGetHeight(self.verticalScroll.frame)){
        [scrollView setContentOffset:CGPointMake(0, CGRectGetHeight(self.verticalScroll.frame))];
    }
}
最后定时器记得在退出本界面的时候记得销毁
-(void)viewWillDisappear:(BOOL)animated{
    [_myTimer invalidate];
    _myTimer = nil;
}
到此,一个上下字体无限滚动的轮播,就做成了。当然代码的话写的还是比较烂的,有不足地方请大家指出,我会及时更改。最后记录本文仅供参考,Demo地址
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 `UICollectionView` 或 `UIPageViewController` 来实现 iOS 图片轮播。 使用 `UICollectionView` 实现的方法如下: 1. 在您的视图控制器中,创建一个 `UICollectionView` 实例,并将其作为子视图添加到您的视图控制器的视图中。 2. 使用自定义布局实现循环滚动。可以参考以下代码: ``` class LoopCollectionViewFlowLayout: UICollectionViewFlowLayout { override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint { guard let collectionView = collectionView else { return super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity) } let collectionViewSize = collectionView.bounds.size let proposedContentOffsetCenterX = proposedContentOffset.x + collectionViewSize.width * 0.5 let proposedRect = CGRect(x: proposedContentOffset.x, y: 0, width: collectionViewSize.width, height: collectionViewSize.height) guard let layoutAttributes = layoutAttributesForElements(in: proposedRect) else { return super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity) } let centerX = proposedContentOffsetCenterX let offset = CGPoint(x: proposedContentOffset.x + nearestTargetOffset(for: layoutAttributes, with: centerX), y: proposedContentOffset.y) return offset } private func nearestTargetOffset(for layoutAttributes: [UICollectionViewLayoutAttributes], with centerX: CGFloat) -> CGFloat { let targetAttributes = layoutAttributes.sorted { abs($0.center.x - centerX) < abs($1.center.x - centerX) } let nearestAttribute = targetAttributes.first return nearestAttribute?.center.x ?? 0 - centerX } } ``` 3. 创建自定义 `UICollectionViewCell` 类,并在其中添加一个 `UIImageView` 用于显示图片。 4. 实现 `UICollectionViewDataSource` 协议中的方法,用于设置图片数据源和自定义的 `UICollectionViewCell`。 5. 实现定时器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值