iOS Implementation of Carousel Graph banner Example
- 2021-10-25 00:00:05
- OfStack
The landlord project needs to have a carousel map, because it is relatively simple, I wrote it myself, because I got pictures from the Internet, so I used SDWebImage, a 3-party library, of course, I can also get rid of it myself
There is a * after the type. If you use it, please add it yourself. . . . .
Code:. h file
@protocol TJXViewDelegate<NSObject>
// Judge the one clicked
-(void)sendImageName:(TJXView *)TJXView andName:(NSInteger)selectImage;
@end
@interface TJXView : UIView
@property (nonatomic,weak)id<TJXViewDelegate>delegate;
// Biography 1 A frame And Array with picture names
// Parameter 1 : frame
// Parameter 2 Array containing picture names
// Parameter 3 : BOOL If it is YES Scroll automatically, if it is NO Do not scroll
-(id)initWithFrame:(CGRect)frame andImageNameArray:
(NSMutableArray * )imageNameArray andIsRunning:(BOOL)isRunning;
@end
. m file
@interface TJXView()<UIScrollViewDelegate>
{
NSInteger _currentPage; // Record the true number of pages
NSTimer *_timer; // Life 1 Global variables
}
@property (nonatomic,assign) BOOL isRun;
@property (nonatomic,strong) NSMutableArray *imageArray;// Store the name of the picture
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIPageControl *pageControl;
@property (nonatomic,assign) CGFloat width;//view Width of
@property (nonatomic,assign) CGFloat height;//view The height of
@end
-(id)initWithFrame:(CGRect)frame andImageNameArray:(NSMutableArray *)imageNameArray andIsRunning:(BOOL)isRunning{
self = [super initWithFrame:frame];
if (self) {
_width = self.frame.size.width;
_height = self.frame.size.height;
//arrayWithArray Put the contents of the array into the 1 Returns from an array of
self.imageArray = [NSMutableArray arrayWithArray:imageNameArray];
// At the end of the array, add the number of the original array 1 Elements
[self.imageArray addObject:[imageNameArray firstObject]];
// Add the end of the original array at the beginning of the array 1 Elements
[self.imageArray insertObject:[imageNameArray lastObject] atIndex:0];
self.isRun = isRunning;
_currentPage = 0;
[self createSro];
[self createPageControl];
[self createTimer];
}
return self;
}
-(void)createTimer{
if (_isRun == YES) {
_timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(change) userInfo:nil repeats:YES ];
[[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes]; }
}
-(void)change{
//1 Get the current point
CGPoint point = _scrollView.contentOffset;
//2 Find the point to be transformed
CGPoint endPoint = CGPointMake(point.x+_width, 0);
// Judge
if (endPoint.x == (self.imageArray.count-1)*_width) {
[UIView animateWithDuration:0.25 animations:^{
_scrollView.contentOffset = CGPointMake(endPoint.x, 0);
} completion:^(BOOL finished) {
// Animated completed block
_scrollView.contentOffset = CGPointMake(_width, 0);
CGPoint realEnd = _scrollView.contentOffset;
// Take 1 Number of page passes
_currentPage = realEnd.x/_width;
_pageControl.currentPage = _currentPage-1;
}];
}
else{
//0.25s Change in 1 Pictures
[UIView animateWithDuration:0.25 animations:^{
_scrollView.contentOffset = endPoint;
} completion:^(BOOL finished) {
}];
CGPoint realEnd = _scrollView.contentOffset;
// Take 1 Number of page passes
_currentPage = realEnd.x/_width;
_pageControl.currentPage = _currentPage-1;
}
}
// Create a page number indicator
-(void)createPageControl{
_pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(_width-200, _height-30, 100, 30)];
_pageControl.centerX = _width/2;
_pageControl.numberOfPages = self.imageArray.count-2;
_pageControl.pageIndicatorTintColor = WP_GRAY_COLOR;
_pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
_pageControl.userInteractionEnabled = NO;
[self addSubview:_pageControl];
}
// Create a scrolling view
-(void)createSro{
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _width, _height)];
_scrollView.contentSize = CGSizeMake(_width*self.imageArray.count, _height);
for (int i = 0; i < self.imageArray.count; i++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*_width, 0, _width, _height)];
// imageView.image = [UIImage imageNamed:self.imageArray[i]];
[imageView sd_setImageWithURL:self.imageArray[i] placeholderImage:[UIImage imageNamed:@"home_banner_blank"]];
imageView.userInteractionEnabled = YES;
imageView.tag = 200+i;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
[imageView addGestureRecognizer:tap];
[_scrollView addSubview:imageView];
}
// Horizontal indicator bar is not displayed
_scrollView.showsHorizontalScrollIndicator = NO;
// Close spring effect
_scrollView.bounces = NO;
// Set the user to see the 1 Zhang
_scrollView.contentOffset = CGPointMake(_width, 0);
// Set up proxy
_scrollView.delegate = self;
// Paging effect
_scrollView.pagingEnabled = YES;
[self addSubview:_scrollView];
}
-(void)tap:(UITapGestureRecognizer *)tap{
if(_delegate&&[_delegate respondsToSelector:@selector(sendImageName:andName:)]){
[_delegate sendImageName:self andName:tap.view.tag-201];
}else{
NSLog(@" Method without proxy or prior agreement ");
}
}
#pragma mark UIScrollViewDelegate
// Stop scrolling
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (_timer) {
[_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]];
}
// Number of pictures 1 2 3 4 5 6 7 8
// Real page number 0 1 2 3 4 5 6 7
// Page number displayed 0 1 2 3 4 5
CGPoint point = _scrollView.contentOffset;
if (point.x == (self.imageArray.count-1)*_width) {
scrollView.contentOffset = CGPointMake(_width, 0);
}
if (point.x == 0) {
scrollView.contentOffset = CGPointMake((self.imageArray.count-2)*_width, 0);
}
// Take 1 Number of page passes
CGPoint endPoint = scrollView.contentOffset;
_currentPage = endPoint.x/_width;
_pageControl.currentPage = _currentPage-1;
}
// Stop the timer when the finger starts touching
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
if (_timer) {
// If there is, stop it
[_timer setFireDate:[NSDate distantFuture]];
}
}
Import the header compliance agent in the project
TJXView * TJXView = [[TJXView alloc]initWithFrame:CGRectMake(0, 0, WPSCREEN_WIDTH, 100*WPSCREEN_HIGTH_RATIO) andImageNameArray:self.bannerImager andIsRunning:YES];
TJXView.delegate = self;
[self.view addSubview: TJXView];
#pragma mark TJXViewDelegate
-(void)sendImageName:(TJXView *) TJXView andName:(NSInteger)selectImage{
KKLog(@"%ld",(long)selectImage);
}