自动广告图
2019獨角獸企業重金招聘Python工程師標準>>>
#import <UIKit/UIKit.h>
@interface DIEPageView : UIView<UIScrollViewDelegate>{
}
@property (nonatomic, assign) id? delegate;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, strong) NSMutableArray *imageViewAry;
@property (nonatomic, readonly) UIScrollView *scrollView;
@property (nonatomic, readonly) UIPageControl *pageControl;
-(void)shouldAutoShow:(BOOL)shouldStart;
@end
@protocol WHcrollViewViewDelegate <NSObject>
@optional
- (void)didClickPage:(DIEPageView *)view atIndex:(NSInteger)index;
@end
#import "DIEPageView.h"
@interface DIEPageView(){
? ? UIView *_firstView;
? ? UIView *_middleView;
? ? UIView *_lastView;
?? ?
? ? float _viewWidth;
? ? float _viewHeight;
?? ?
? ? NSTimer *_autoScrollTimer;
?? ?
? ? UITapGestureRecognizer *_tap;
}
@end
@implementation DIEPageView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
? ? // Drawing code
}
*/
- (id)initWithFrame:(CGRect)frame
{ ?
? ? self = [super initWithFrame:frame];
? ? if (self) {
?? ? ? ?
? ? ? ? _viewWidth = self.bounds.size.width;
? ? ? ? _viewHeight = self.bounds.size.height;
?? ? ? ?
? ? ? ? //設置scrollview
? ? ? ? _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, _viewWidth, _viewHeight)];
? ? ? ? _scrollView.delegate = self;
? ? ? ? _scrollView.contentSize = CGSizeMake(_viewWidth * 3, _viewHeight);
? ? ? ? _scrollView.showsHorizontalScrollIndicator = NO;
? ? ? ? _scrollView.pagingEnabled = YES;
? ? ? ? _scrollView.backgroundColor = [UIColor blackColor];
? ? ? ? _scrollView.delegate = self;
? ? ? ? [self addSubview:_scrollView];
?? ? ? ?
? ? ? ? //設置分頁
? ? ? ? _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, _viewHeight-30, _viewWidth, 30)];
? ? ? ? _pageControl.userInteractionEnabled = NO;
? ? ? ? _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
? ? ? ? _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
? ? ? ? [self addSubview:_pageControl];
?? ? ? ?
? ? ? ? //設置單擊手勢
? ? ? ? _tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
? ? ? ? _tap.numberOfTapsRequired = 1;
? ? ? ? _tap.numberOfTouchesRequired = 1;
? ? ? ? [_scrollView addGestureRecognizer:_tap];
? ? }
? ? return self;
}
#pragma mark 單擊手勢
-(void)handleTap:(UITapGestureRecognizer*)sender
{
? ? if ([_delegate respondsToSelector:@selector(didClickPage:atIndex:)]) {
? ? ? ? [_delegate didClickPage:self atIndex:_currentPage+1];
? ? }
}
#pragma mark 設置imageViewAry
-(void)setImageViewAry:(NSMutableArray *)imageViewAry
{
? ? if (imageViewAry) {
? ? ? ? _imageViewAry = imageViewAry;
? ? ? ? _currentPage = 0; //默認為第0頁
?? ? ? ?
? ? ? ? _pageControl.numberOfPages = _imageViewAry.count;
? ? }
?? ?
? ? [self reloadData];
}
#pragma mark 刷新view頁面
-(void)reloadData
{
? ? [_firstView removeFromSuperview];
? ? [_middleView removeFromSuperview];
? ? [_lastView removeFromSuperview];
?? ?
? ? //從數組中取到對應的圖片view加到已定義的三個view中
? ? if (_currentPage==0) {
? ? ? ? _firstView = [_imageViewAry lastObject];
? ? ? ? _middleView = [_imageViewAry objectAtIndex:_currentPage];
? ? ? ? _lastView = [_imageViewAry objectAtIndex:_currentPage+1];
? ? }
? ? else if (_currentPage == _imageViewAry.count-1)
? ? {
? ? ? ? _firstView = [_imageViewAry objectAtIndex:_currentPage-1];
? ? ? ? _middleView = [_imageViewAry objectAtIndex:_currentPage];
? ? ? ? _lastView = [_imageViewAry firstObject];
? ? }
? ? else
? ? {
? ? ? ? _firstView = [_imageViewAry objectAtIndex:_currentPage-1];
? ? ? ? _middleView = [_imageViewAry objectAtIndex:_currentPage];
? ? ? ? _lastView = [_imageViewAry objectAtIndex:_currentPage+1];
? ? }
?? ?
? ? //設置三個view的frame,加到scrollview上
? ? _firstView.frame = CGRectMake(0, 0, _viewWidth, _viewHeight);
? ? _middleView.frame = CGRectMake(_viewWidth, 0, _viewWidth, _viewHeight);
? ? _lastView.frame = CGRectMake(_viewWidth*2, 0, _viewWidth, _viewHeight);
? ? [_scrollView addSubview:_firstView];
? ? [_scrollView addSubview:_middleView];
? ? [_scrollView addSubview:_lastView];
?? ?
? ? //設置當前的分頁
? ? _pageControl.currentPage = _currentPage;
?? ?
? ? //顯示中間頁
? ? _scrollView.contentOffset = CGPointMake(_viewWidth, 0);
}
#pragma mark scrollvie停止滑動
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
? ? //手動滑動時候暫停自動替換
? ? [_autoScrollTimer invalidate];
? ? _autoScrollTimer = nil;
? ? _autoScrollTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES];
?? ?
? ? //得到當前頁數
? ? float x = _scrollView.contentOffset.x;
?? ?
? ? //往前翻
? ? if (x<=0) {
? ? ? ? if (_currentPage-1<0) {
? ? ? ? ? ? _currentPage = _imageViewAry.count-1;
? ? ? ? }else{
? ? ? ? ? ? _currentPage --;
? ? ? ? }
? ? }
?? ?
? ? //往后翻
? ? if (x>=_viewWidth*2) {
? ? ? ? if (_currentPage==_imageViewAry.count-1) {
? ? ? ? ? ? _currentPage = 0;
? ? ? ? }else{
? ? ? ? ? ? _currentPage ++;
? ? ? ? }
? ? }
?? ?
? ? [self reloadData];
}
#pragma mark 自動滾動
-(void)shouldAutoShow:(BOOL)shouldStart
{
? ? if (shouldStart)? //開啟自動翻頁
? ? {
? ? ? ? if (!_autoScrollTimer) {
? ? ? ? ? ? _autoScrollTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES];
? ? ? ? }
? ? }
? ? else ? //關閉自動翻頁
? ? {
? ? ? ? if (_autoScrollTimer.isValid) {
? ? ? ? ? ? [_autoScrollTimer invalidate];
? ? ? ? ? ? _autoScrollTimer = nil;
? ? ? ? }
? ? }
}
#pragma mark 展示下一頁
-(void)autoShowNextImage
{
? ? if (_currentPage == _imageViewAry.count-1) {
? ? ? ? _currentPage = 0;
? ? }else{
? ? ? ? _currentPage ++;
? ? }
?? ?
? ? [self reloadData];
}
@end
#import "AdvertiseViewController.h"
#import "DIEPageView.h"
@interface AdvertiseViewController (){
? ? DIEPageView *_whView;
}
@end
@implementation AdvertiseViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // Do any additional setup after loading the view.
? ? //創建view (view中包含UIScrollView、UIPageControl,設置frame)
? ? _whView = [[DIEPageView alloc] initWithFrame:CGRectMake(0, 44, 320, 400)];
?? ?
? ? //把N張圖片放到imageview上
? ? NSMutableArray *tempAry = [NSMutableArray array];
? ? for (int i=1; i<6; i++) {
? ? ? ? UIImageView *imageView = [[UIImageView alloc] init];
? ? ? ? imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];
? ? ? ? [tempAry addObject:imageView];
? ? }
?? ?
? ? //把imageView數組存到whView里
? ? [_whView setImageViewAry:tempAry];
?? ?
? ? //把圖片展示的view加到當前頁面
? ? [self.view addSubview:_whView];
?? ?
? ? //開啟自動翻頁
? ? [_whView shouldAutoShow:YES];
?? ?
? ? //遵守協議
? ? _whView.delegate = self;
}
#pragma mark 協議里面方法,點擊某一頁
-(void)didClickPage:(DIEPageView *)view atIndex:(NSInteger)index
{
? ? NSLog(@"點擊了第%li頁",index);
}
#pragma mark 界面消失的時候,停止自動滾動
-(void)viewDidDisappear:(BOOL)animated
{
? ? [_whView shouldAutoShow:NO];
}
@end
轉載于:https://my.oschina.net/u/2534536/blog/536071
總結
- 上一篇: 揪出占用磁盘空间的罪魁祸首-企业生产实战
- 下一篇: iOS - Swift Foundati