日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自动广告图

發布時間:2025/3/17 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自动广告图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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];

? ? }

?? ?

? ? //設置三個viewframe,加到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中包含UIScrollViewUIPageControl,設置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

總結

以上是生活随笔為你收集整理的自动广告图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。