iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动

1 //
  2
  7 //
  8 
  9 #import "GXViewController.h"
 10 
 11 #define kCount 6  //图片总张数
 12 
 13 static long step = 0; //记录时钟动画调用次数
 14 
 15 @interface GXViewController () <UIScrollViewDelegate>
 16 {
 17     UIScrollView *_scrollView;
 18     UIImageView     *_currentImageView; //当前视图
 19     UIImageView     *_nextImageView;    //下一个视图
 20     UIImageView     *_previousView;     //上一个视图
 21     CADisplayLink   *_timer;            //定时器
 22     
 23     BOOL _isDraging; //当前是否正在拖拽
 24 }
 25 
 26 @end
 27 
 28 @implementation GXViewController
 29 
 30 - (void)viewDidLoad
 31 {
 32     [super viewDidLoad];
 33     
 34     CGFloat width = self.view.bounds.size.width;
 35     CGFloat height = self.view.bounds.size.height;
 36     
 37     _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
 38     _scrollView.contentSize = CGSizeMake(3 * width, 0);
 39     _scrollView.showsHorizontalScrollIndicator = NO;
 40     _scrollView.pagingEnabled = YES;
 41     _scrollView.delegate = self;
 42     _scrollView.bounces = NO;
 43     _scrollView.contentOffset = CGPointMake(width, 0);
 44     [self.view addSubview:_scrollView];
 45     
 46     //初始化当前视图
 47     _currentImageView = [[UIImageView alloc] init];
 48     _currentImageView.image = [UIImage imageNamed:@"01.jpg"];
 49     _currentImageView.frame = CGRectMake(width, 0, width, height);
 50     _currentImageView.contentMode = UIViewContentModeScaleAspectFill;
 51     [_scrollView addSubview:_currentImageView];
 52     
 53     //初始化下一个视图
 54     _nextImageView = [[UIImageView alloc] init];
 55     _nextImageView.image = [UIImage imageNamed:@"02.jpg"];
 56     _nextImageView.frame = CGRectMake(width * 2, 0, width, height);
 57     _nextImageView.contentMode = UIViewContentModeScaleAspectFill;
 58     [_scrollView addSubview:_nextImageView];
 59     
 60     //初始化上一个视图
 61     _previousView = [[UIImageView alloc] init];
 62     _previousView.image = [UIImage imageNamed:@"06.jpg"];
 63     _previousView.frame = CGRectMake(0, 0, width, height);
 64     _previousView.contentMode = UIViewContentModeScaleAspectFill;
 65     [_scrollView addSubview:_previousView];
 66     
 67     // 时钟动画
 68     _timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(update:)];
 69     [_timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
 70     
 71 }
 72 
 73 #pragma mark 时钟动画调用方法
 74 - (void)update:(CADisplayLink *)timer
 75 {
 76     step++;
 77     
 78     if ((step % 120 != 0) || _isDraging) {
 79         return;
 80     }
 81     
 82     CGPoint offset = _scrollView.contentOffset;
 83     
 84     offset.x += 320;
 85     if (offset.x > 640) {
 86         offset.x = 320;
 87     }
 88     
 89     [_scrollView setContentOffset:offset animated:YES];
 90 }
 91 
 92 #pragma mark - 代理方法
 93 #pragma mark 准备开始拖动
 94 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
 95 {
 96     _isDraging = YES;
 97 }
 98 
 99 #pragma mark 视图停止滚动
100 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
101 {
102     _isDraging = NO;
103     step = 0;
104 }
105 
106 #pragma mark 已经拖动
107 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
108     
109     static int i = 1;//当前展示的是第几张图片
110     
111     float offset = scrollView.contentOffset.x;
112     if (_nextImageView.image == nil || _previousView.image == nil) {
113         
114         //加载下一个视图
115         NSString *imageName1 = [NSString stringWithFormat:@"0%d.jpg", i == kCount ? 1 : i + 1];
116         _nextImageView.image = [UIImage imageNamed:imageName1];
117         
118         //加载上一个视图
119         NSString *imageName2 = [NSString stringWithFormat:@"0%d.jpg", i == 1 ? kCount : i - 1];
120         _previousView.image = [UIImage imageNamed:imageName2];
121     }
122     
123     if (offset == 0) {
124         _currentImageView.image = _previousView.image;
125         scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);
126         _previousView.image = nil;
127         
128         if (i == 1) {
129             i = kCount;
130         }else{
131             i -= 1;
132         }
133         
134     }
135     
136     if (offset == scrollView.bounds.size.width * 2) {
137         _currentImageView.image = _nextImageView.image;
138         scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);
139         _nextImageView.image = nil;
140         
141         if (i == kCount) {
142             i = 1;
143         }else{
144             i += 1;
145         }
146         
147     }
148 }
149 
150 @end
  • 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、付费专栏及课程。

余额充值