loopview(死循环实现轮播)

//
//  WMLoopView.m
//  WMLoopView
//
//  Created by Mark on 15/3/30.
//  Copyright (c) 2015年 yq. All rights reserved.
//

#import "WMLoopView.h"
#define kIdentifier @"WMLoopViewCellIdentifier"
#define kPageH 20
@interface WMLoopView() <UIScrollViewDelegate>
@property (nonatomic, strong) NSMutableArray *currentImages;
@property (nonatomic, assign) int currentPage;
@property (nonatomic, weak) UIPageControl *pageControl;
@property (nonatomic, weak) UIScrollView *scrollView;
@end

@implementation WMLoopView
- (instancetype)initWithFrame:(CGRect)frame images:(NSArray *)images autoPlay:(BOOL)isAuto delay:(NSTimeInterval)timeInterval {
    if (self = [super initWithFrame:frame]) {
        _autoPlay = isAuto;
        _timeInterval = timeInterval;
        _images = images;
        _currentPage = 0;

        [self addScrollView];
        [self addPageControl];
        if (self.autoPlay == YES) {
            [self toPlay];
        }
    }
    return self;
}

#pragma mark - Public Methods
- (void)addPageControl {
    CGFloat height = self.frame.size.height;
    CGFloat width = self.frame.size.width;
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, height-kPageH, width, kPageH)];
    bgView.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.2];
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, width, kPageH)];
    pageControl.numberOfPages = self.images.count;
    pageControl.currentPage = 0;
    pageControl.userInteractionEnabled = NO;
    _pageControl = pageControl;
    [bgView addSubview:self.pageControl];
    [self addSubview:bgView];
}

#pragma mark - Private Methods
- (void)toPlay {
    [self performSelector:@selector(autoPlayToNextPage) withObject:nil afterDelay:_timeInterval];
}

- (void)autoPlayToNextPage {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoPlayToNextPage) object:nil];
    [self.scrollView setContentOffset:CGPointMake(self.frame.size.width * 2, 0) animated:YES];
    [self performSelector:@selector(autoPlayToNextPage) withObject:nil afterDelay:_timeInterval];
}

- (NSMutableArray *)currentImages {
    if (_currentImages == nil) {
        _currentImages = [[NSMutableArray alloc] init];
    }
    [_currentImages removeAllObjects];
    NSInteger count = self.images.count;
    int i = (int)(_currentPage + count - 1)%count;
    [_currentImages addObject:self.images[i]];
    [_currentImages addObject:self.images[_currentPage]];
    i = (int)(_currentPage + 1)%count;
    [_currentImages addObject:self.images[i]];
    return _currentImages;
}

- (void)addScrollView {
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    for (int i = 0; i < 3; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * width, 0, width, height)];
        imageView.image = [UIImage imageNamed:self.currentImages[i]];
        [scrollView addSubview:imageView];
    }
    scrollView.contentSize = CGSizeMake(3*width, height);
    scrollView.contentOffset = CGPointMake(width, 0);
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.delegate = self;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapped:)];
    [scrollView addGestureRecognizer:tap];

    [self addSubview:scrollView];
    _scrollView = scrollView;
}

- (void)refreshImages {
    NSArray *subViews = self.scrollView.subviews;
    for (int i = 0; i < subViews.count; i++) {
        UIImageView *imageView = (UIImageView *)subViews[i];
        imageView.image = [UIImage imageNamed:self.currentImages[i]];
    }

    [self.scrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];
}

#pragma mark - delegate
- (void)singleTapped:(UITapGestureRecognizer *)recognizer {
    if ([self.delegate respondsToSelector:@selector(loopViewDidSelectedImage:index:)]) {
        [self.delegate loopViewDidSelectedImage:self index:_currentPage];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat x = scrollView.contentOffset.x;
    CGFloat width = self.frame.size.width;
    if (x >= 2 * width) {
        _currentPage = (++_currentPage) % self.images.count;
        self.pageControl.currentPage = _currentPage;
        [self refreshImages];
    }
    if (x <= 0) {
        _currentPage = (int)(_currentPage + self.images.count - 1)%self.images.count;
        self.pageControl.currentPage = _currentPage;
        [self refreshImages];
    }

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [scrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:YES];
}

@end
//
//  WMLoopView.h
//  WMLoopView
//
//  Created by Mark on 15/3/30.
//  Copyright (c) 2015年 yq. All rights reserved.
//

#import <UIKit/UIKit.h>
@class WMLoopView;
@protocol WMLoopViewDelegate <NSObject>
@optional
- (void)loopViewDidSelectedImage:(WMLoopView *)loopView index:(int)index;
@end

@interface WMLoopView : UIView
@property (nonatomic, weak) id<WMLoopViewDelegate> delegate;
@property (nonatomic, assign) BOOL autoPlay;
@property (nonatomic, assign) NSTimeInterval timeInterval;
@property (nonatomic, strong) NSArray *images;

- (instancetype)initWithFrame:(CGRect)frame images:(NSArray *)images autoPlay:(BOOL)isAuto delay:(NSTimeInterval)timeInterval;
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LoopView 是一个强大的轮转大图控件,并且提供了许多配置方法来达到您的显示效果和需求。github地址:https://github.com/xuehuayous/Android-LoopView介绍博客地址:http://blog.csdn.net/xuehuayous/article/details/50518393在项目中使用 LoopView如果您的项目使用 Gradle 构建, 只需要在您的build.gradle文件添加下面一行到 dependencies :    compile 'com.kevin:loopview:1.0.5'简单使用在layout.xml 中配置LoopView在Layout文件添加<com.kevin.loopview.AdLoopView<com.kevin.loopview.AdLoopView     android:id="@ id/main_act_adloopview"     android:layout_width="match_parent"     android:layout_height="192dp"> </com.kevin.loopview.AdLoopView>在代码中配置AdLoopView mLoopView = (AdLoopView) this.findViewById(R.id.main_act_adloopview); String json = LocalFileUtils.getStringFormAsset(this, "loopview_date.json"); // 使用 JsonTool 封装 JSON 数据到实体对象 LoopData loopData = JsonTool.toBean(json, LoopData.class); // 通过对象的方式设置数据 mLoopView.refreshData(loopData); // 开始轮转 mLoopView.startAutoLoop(); // 设置点击监听 mLoopView.setOnClickListener(new BaseLoopAdapter.OnItemClickListener() {         @Override         public void onItemClick(PagerAdapter parent, View view,              int position, int realPosition) {             // 获取数据             LoopData loopData = mLoopView.getLoopData();             String url = loopData.items.get(position).link;             // 通过系统浏览器打开跳转链接             Intent intent = new Intent();             intent.setData(Uri.parse(url));             intent.setAction(Intent.ACTION_VIEW);             startActivity(intent);         }     });更多配置XML 配置在XML中使用AdLoopView,可以有如下配置:<com.kevin.loopview.AdLoopView     android:id="@ id/adloop_act_adloopview"     android:layout_width="match_parent"     android:layout_height="192dp"     kevin:loop_interval="5000"     kevin:loop_dotMargin="5dp"     kevin:loop_autoLoop="[true|false]"     kevin:loop_dotSelector="@drawable/ad_dots_selector"     kevin:loop_layoutId="@layout/ad_loopview_layout"> </com.kevin.loopview.AdLoopView>在代码中配置// 设置ViewPager页面切换时间 mLoopView.setScrollDuration(1000); // 设置轮转时间间隔 mLoopView.setInterval(3000); // 以集合的方式初始化数据 mLoopView.setLoopViewPager(List<Map<String, String>> data); // 以JSON的方式初始化数据 mLoopView.setLoopViewPager(String jsonData); // 以数据实体的方式初始化数据 mLoopView.setLoopViewPager(LoopData rotateData); // 以集合的方式刷新数据 mLoopView.refreshData(final List<Map<String, String>> data); // 以数据实体的方式刷新数据 mLoopView.refreshData(LoopData loopData); // 以JSON的方式刷新数据 mLoopView.refreshData(String jsonData); // 获取配置的轮转大图数据 mLoopView.getLoopData(); // 开始自动轮转 mLoopView.startAutoLoop(); // 在指定时间延迟后自动轮转 mLoopView.startAutoLoop(long delayTimeInMills); // 停止自动轮转 mLoopView.stopAutoLoop(); // 设置自定义布局 mLoopView.setLoopLayout(int layoutResId);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值