iOS UIScrollView 实现轮播图

利用UIScrollView实现轮播图 , 需要三个ImageView轮流切换,具体原理就不讲解了.




具体实现代码如下:


<span style="font-size:24px;">//
//  ViewController.m
//  PhotosShowDemo
//
//  Created by 帝炎魔 on 16/5/29.
//  Copyright © 2016年 帝炎魔. All rights reserved.
//

#import "ViewController.h"

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeitht [UIScreen mainScreen].bounds.size.height

#define kRedColor [UIColor redColor]
#define kWhitColor [UIColor yellowColor]
#define kBlueColor [UIColor blueColor]
#define kGreenColor [UIColor greenColor]
#define kBrownColor [UIColor brownColor]



@interface ViewController () <UIScrollViewDelegate>


@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIPageControl *pageControl;

@property (nonatomic, strong) UIView *leftView;

@property (nonatomic, strong) UIView *currentView;

@property (nonatomic, strong) UIView *rightView;

@property (nonatomic, assign) int viewCount;

@property (nonatomic, assign) int currentViewIndex;

@property (nonatomic, copy) NSArray *viewColorArray;

@property (nonatomic, copy) NSTimer *timer;

@property (nonatomic, assign) BOOL isScroll;



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.viewColorArray = @[kRedColor, kBlueColor, kBrownColor, kWhitColor, kGreenColor ];
    
    
    
    // 添加scrollView
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageControl];
    
    [self loadImageView];
    
    // 创建定时器 实现轮播
    [self zidong];
    // Do any additional setup after loading the view, typically from a nib.

}

// 开启定时器
- (void)zidong
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(nextPage) userInfo:NULL repeats:YES];
}

// 定时器的方法
-(void)nextPage
{
    _isScroll = YES;
    int rightCout;

    [_scrollView setContentOffset:CGPointMake(kWidth * 2, 0)animated:YES];
   
    
    _currentViewIndex = (_currentViewIndex + 1) % 5;
    _currentView.backgroundColor = _viewColorArray[_currentViewIndex];
   
    rightCout = (_currentViewIndex + 1) % 5;
    
    
    _rightView.backgroundColor = _viewColorArray[rightCout];
   
    _pageControl.currentPage = _currentViewIndex;
    [_scrollView setContentOffset:CGPointMake(kWidth, 0) animated:NO];

    
    


}

// 添加滑动视图
- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        
        _scrollView.frame = CGRectMake(0, 0, kWidth, kHeitht);
        
        _scrollView.contentSize = CGSizeMake(kWidth * 3, kHeitht);
        _scrollView.pagingEnabled = YES;
        
        _scrollView.delegate = self;
       [_scrollView setContentOffset:CGPointMake(kWidth, 0)];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

// 添加按钮
-(UIPageControl *)pageControl
{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] init];
        _pageControl.frame = CGRectMake(0, kHeitht - 100, kWidth, 30);
        _pageControl.currentPage = _currentViewIndex;
        _pageControl.numberOfPages = 5;
        _pageControl.pageIndicatorTintColor = kBlueColor;
        _pageControl.currentPageIndicatorTintColor = kWhitColor;
    }
    return _pageControl;
}

// 加载图片
- (void)loadImageView
{
    _leftView = [[UIView alloc] init];
    _leftView.backgroundColor = _viewColorArray[4];
    _leftView.frame = CGRectMake(0, 0, kWidth, kHeitht);
    [_scrollView addSubview:_leftView];
    
    _currentView = [[UIView alloc] initWithFrame:CGRectMake(kWidth, 0, kWidth, kHeitht)];
    _currentView.backgroundColor = _viewColorArray[0];
    [_scrollView addSubview:_currentView];
    

    _rightView = [[UIView alloc] initWithFrame:CGRectMake(2 * kWidth, 0, kWidth, kHeitht)];
    _rightView.backgroundColor = _viewColorArray[1];
    [_scrollView addSubview:_rightView];
    
    _currentViewIndex = 0;
    
}


#pragma mark ---- 滚动将要停止的时候
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 滚动视图停止减速
    // contentOffset 偏移量
    // 开启定时器
    if (_isScroll == NO) {
        [self zidong];
        _isScroll = YES;
    }
    
    
    [self loadData];
    
    _pageControl.currentPage = _currentViewIndex;
    [_scrollView setContentOffset:CGPointMake(kWidth, 0)];
    
    
    // UIControl 响应的控件 的父类
   //  UIControl
    
    
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 移除定时器
    
     [self.timer invalidate];
    if (_isScroll == YES) {
         scrollView.contentOffset = CGPointMake(kWidth, 0);
        _currentView.backgroundColor = _viewColorArray[_currentViewIndex];
    }
    _isScroll = NO;
   
    // _currentView = _viewColorArray[_currentViewIndex];
}



- (void)loadData
{
    if (_scrollView.contentOffset.x > kWidth) {
        NSLog(@"右滑动");
        _currentViewIndex = (_currentViewIndex + 1) % 5;
        
    } else if (_scrollView.contentOffset.x < kWidth) {
        NSLog(@"左滑动");
        _currentViewIndex = (_currentViewIndex + 4 ) % 5;
    }
    _currentView.backgroundColor = _viewColorArray[_currentViewIndex];
    _leftView.backgroundColor = _viewColorArray[(_currentViewIndex + 4) % 5];
    _rightView.backgroundColor = _viewColorArray[(_currentViewIndex + 1) % 5];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
</span>


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值