Commit 53554b57 authored by lufei's avatar lufei

add:添加轮播组件库

parent c6946afe
#
# Be sure to run `pod lib lint DZXCarouselView.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'DZXCarouselView'
s.version = '0.1.0'
s.summary = '轮播组件'
# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
s.homepage = 'http://gitlab.wanwudezhi.work/lufei/dzxcarouselview'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'BoASir' => 'lufei@zhuangdianwenhua.com' }
s.source = { :git => 'git@gitlab.wanwudezhi.work:lufei/dzxcarouselview.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '9.0'
s.source_files = 'DZXCarouselView/Classes/**/*'
s.resource_bundles = {
'DZXCarouselView' => ['DZXCarouselView/Assets/*.png']
}
s.framework = 'UIKit'
end
//
// DZBannerViewPageControl.h
// Pods
//
// Created by Hydra on 2020/12/9.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface DZBannerViewPageControl : UIControl
@property (nonatomic, assign) NSInteger numberOfPages;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) BOOL hidesForSinglePage;
@property (nonatomic, assign) CGFloat pageIndicatorSpaing;
@property (nonatomic, assign) UIEdgeInsets contentInset;
@property (nonatomic, assign , readonly) CGSize contentSize;
// 设置颜色
@property (nullable, nonatomic,strong) UIColor *pageIndicatorTintColor;
@property (nullable, nonatomic,strong) UIColor *currentPageIndicatorTintColor;
// 设置图片
@property (nullable, nonatomic,strong) UIImage *pageIndicatorImage;
@property (nullable, nonatomic,strong) UIImage *currentPageIndicatorImage;
@property (nonatomic, assign) UIViewContentMode indicatorImageContentMode;
// 设置大小
@property (nonatomic, assign) CGSize pageIndicatorSize;
@property (nonatomic, assign) CGSize currentPageIndicatorSize;
@property (nonatomic, assign) CGFloat animateDuring;
- (void)setCurrentPage:(NSInteger)currentPage animate:(BOOL)animate;
@end
NS_ASSUME_NONNULL_END
//
// DZBannerViewPageControl.m
// Pods
//
// Created by Hydra on 2020/12/9.
//
#import "DZBannerViewPageControl.h"
@interface DZBannerViewPageControl ()
@property (nonatomic, strong) NSArray<UIImageView *> *indicatorViews;
@property (nonatomic, assign) BOOL forceUpdate;
@end
@implementation DZBannerViewPageControl
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self configurePropertys];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self configurePropertys];
}
return self;
}
- (void)configurePropertys {
self.userInteractionEnabled = NO;
_forceUpdate = NO;
_animateDuring = 0.3;
_pageIndicatorSpaing = 10;
_indicatorImageContentMode = UIViewContentModeCenter;
_pageIndicatorSize = CGSizeMake(6,6);
_currentPageIndicatorSize = _pageIndicatorSize;
_pageIndicatorTintColor = [UIColor colorWithRed:128/255. green:128/255. blue:128/255. alpha:1];
_currentPageIndicatorTintColor = [UIColor whiteColor];
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview:newSuperview];
if (newSuperview) {
_forceUpdate = YES;
[self updateIndicatorViews];
_forceUpdate = NO;
}
}
#pragma mark - getter setter
- (CGSize)contentSize {
CGFloat width = (_indicatorViews.count - 1) * (_pageIndicatorSize.width + _pageIndicatorSpaing) + _pageIndicatorSize.width + _contentInset.left +_contentInset.right;
CGFloat height = _currentPageIndicatorSize.height + _contentInset.top + _contentInset.bottom;
return CGSizeMake(width, height);
}
- (void)setNumberOfPages:(NSInteger)numberOfPages {
if (numberOfPages == _numberOfPages) {
return;
}
_numberOfPages = numberOfPages;
if (_currentPage >= numberOfPages) {
_currentPage = 0;
}
[self updateIndicatorViews];
if (_indicatorViews.count > 0) {
[self setNeedsLayout];
}
}
- (void)setCurrentPage:(NSInteger)currentPage {
if (_currentPage == currentPage || _indicatorViews.count <= currentPage) {
return;
}
_currentPage = currentPage;
if (!CGSizeEqualToSize(_currentPageIndicatorSize, _pageIndicatorSize)) {
[self setNeedsLayout];
}
[self updateIndicatorViewsBehavior];
if (self.userInteractionEnabled) {
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
}
- (void)setCurrentPage:(NSInteger)currentPage animate:(BOOL)animate {
if (animate) {
[UIView animateWithDuration:_animateDuring animations:^{
[self setCurrentPage:currentPage];
}];
}else {
[self setCurrentPage:currentPage];
}
}
- (void)setPageIndicatorImage:(UIImage *)pageIndicatorImage {
_pageIndicatorImage = pageIndicatorImage;
[self updateIndicatorViewsBehavior];
}
- (void)setCurrentPageIndicatorImage:(UIImage *)currentPageIndicatorImage {
_currentPageIndicatorImage = currentPageIndicatorImage;
[self updateIndicatorViewsBehavior];
}
- (void)setPageIndicatorTintColor:(UIColor *)pageIndicatorTintColor {
_pageIndicatorTintColor = pageIndicatorTintColor;
[self updateIndicatorViewsBehavior];
}
- (void)setCurrentPageIndicatorTintColor:(UIColor *)currentPageIndicatorTintColor {
_currentPageIndicatorTintColor = currentPageIndicatorTintColor;
[self updateIndicatorViewsBehavior];
}
- (void)setPageIndicatorSize:(CGSize)pageIndicatorSize {
if (CGSizeEqualToSize(_pageIndicatorSize, pageIndicatorSize)) {
return;
}
_pageIndicatorSize = pageIndicatorSize;
if (CGSizeEqualToSize(_currentPageIndicatorSize, CGSizeZero) || (_currentPageIndicatorSize.width < pageIndicatorSize.width && _currentPageIndicatorSize.height < pageIndicatorSize.height)) {
_currentPageIndicatorSize = pageIndicatorSize;
}
if (_indicatorViews.count > 0) {
[self setNeedsLayout];
}
}
- (void)setPageIndicatorSpaing:(CGFloat)pageIndicatorSpaing {
_pageIndicatorSpaing = pageIndicatorSpaing;
if (_indicatorViews.count > 0) {
[self setNeedsLayout];
}
}
- (void)setCurrentPageIndicatorSize:(CGSize)currentPageIndicatorSize {
if (CGSizeEqualToSize(_currentPageIndicatorSize, currentPageIndicatorSize)) {
return;
}
_currentPageIndicatorSize = currentPageIndicatorSize;
if (_indicatorViews.count > 0) {
[self setNeedsLayout];
}
}
- (void)setContentHorizontalAlignment:(UIControlContentHorizontalAlignment)contentHorizontalAlignment {
[super setContentHorizontalAlignment:contentHorizontalAlignment];
if (_indicatorViews.count > 0) {
[self setNeedsLayout];
}
}
- (void)setContentVerticalAlignment:(UIControlContentVerticalAlignment)contentVerticalAlignment {
[super setContentVerticalAlignment:contentVerticalAlignment];
if (_indicatorViews.count > 0) {
[self setNeedsLayout];
}
}
#pragma mark - update indicator
- (void)updateIndicatorViews {
if (!self.superview && !_forceUpdate) {
return;
}
if (_indicatorViews.count == _numberOfPages) {
[self updateIndicatorViewsBehavior];
return;
}
NSMutableArray *indicatorViews = _indicatorViews ? [_indicatorViews mutableCopy] :[NSMutableArray array];
if (indicatorViews.count < _numberOfPages) {
for (NSInteger idx = indicatorViews.count; idx < _numberOfPages; ++idx) {
UIImageView *indicatorView = [[UIImageView alloc]init];
indicatorView.contentMode = _indicatorImageContentMode;
[self addSubview:indicatorView];
[indicatorViews addObject:indicatorView];
}
}else if (indicatorViews.count > _numberOfPages) {
for (NSInteger idx = indicatorViews.count - 1; idx >= _numberOfPages; --idx) {
UIImageView *indicatorView = indicatorViews[idx];
[indicatorView removeFromSuperview];
[indicatorViews removeObjectAtIndex:idx];
}
}
_indicatorViews = [indicatorViews copy];
[self updateIndicatorViewsBehavior];
}
- (void)updateIndicatorViewsBehavior {
if (_indicatorViews.count == 0 || (!self.superview && !_forceUpdate)) {
return;
}
if (_hidesForSinglePage && _indicatorViews.count == 1) {
UIImageView *indicatorView = _indicatorViews.lastObject;
indicatorView.hidden = YES;
return;
}
NSInteger index = 0;
for (UIImageView *indicatorView in _indicatorViews) {
if (_pageIndicatorImage) {
indicatorView.contentMode = _indicatorImageContentMode;
indicatorView.image = _currentPage == index ? _currentPageIndicatorImage : _pageIndicatorImage;
}else {
indicatorView.image = nil;
indicatorView.backgroundColor = _currentPage == index ? _currentPageIndicatorTintColor : _pageIndicatorTintColor;
}
indicatorView.hidden = NO;
++index;
}
}
#pragma mark - layout
- (void)layoutIndicatorViews {
if (_indicatorViews.count == 0) {
return;
}
CGFloat orignX = 0;
CGFloat centerY = 0;
CGFloat pageIndicatorSpaing = _pageIndicatorSpaing;
switch (self.contentHorizontalAlignment) {
case UIControlContentHorizontalAlignmentCenter:
// ignore contentInset
orignX = (CGRectGetWidth(self.frame) - (_indicatorViews.count - 1) * (_pageIndicatorSize.width + _pageIndicatorSpaing) - _currentPageIndicatorSize.width)/2;
break;
case UIControlContentHorizontalAlignmentLeft:
orignX = _contentInset.left;
break;
case UIControlContentHorizontalAlignmentRight:
orignX = CGRectGetWidth(self.frame) - ((_indicatorViews.count - 1) * (_pageIndicatorSize.width + _pageIndicatorSpaing) + _currentPageIndicatorSize.width) - _contentInset.right;
break;
case UIControlContentHorizontalAlignmentFill:
orignX = _contentInset.left;
if (_indicatorViews.count > 1) {
pageIndicatorSpaing = (CGRectGetWidth(self.frame) - _contentInset.left - _contentInset.right - _pageIndicatorSize.width - (_indicatorViews.count - 1) * _pageIndicatorSize.width)/(_indicatorViews.count - 1);
}
break;
default:
break;
}
switch (self.contentVerticalAlignment) {
case UIControlContentVerticalAlignmentCenter:
centerY = CGRectGetHeight(self.frame)/2;
break;
case UIControlContentVerticalAlignmentTop:
centerY = _contentInset.top + _currentPageIndicatorSize.height/2;
break;
case UIControlContentVerticalAlignmentBottom:
centerY = CGRectGetHeight(self.frame) - _currentPageIndicatorSize.height/2 - _contentInset.bottom;
break;
case UIControlContentVerticalAlignmentFill:
centerY = (CGRectGetHeight(self.frame) - _contentInset.top - _contentInset.bottom)/2 + _contentInset.top;
break;
default:
break;
}
NSInteger index = 0;
for (UIImageView *indicatorView in _indicatorViews) {
if (_pageIndicatorImage) {
indicatorView.layer.cornerRadius = 0;
}else {
indicatorView.layer.cornerRadius = _currentPage == index ? _currentPageIndicatorSize.height/2 : _pageIndicatorSize.height/2;
}
CGSize size = index == _currentPage ? _currentPageIndicatorSize : _pageIndicatorSize;
indicatorView.frame = CGRectMake(orignX, centerY - size.height/2, size.width, size.height);
orignX += size.width + pageIndicatorSpaing;
++index;
}
}
- (void)layoutSubviews {
[super layoutSubviews];
[self layoutIndicatorViews];
}
@end
//
// DZXCarouselView.h
// Pods
//
//************************************************************
//* *
//* __ _ __ __ _ __ ______ _______ *
//* \ \/ \/ / \ \/ \/ / | __ \ \__ / *
//* \ _ / \ _ / | (__) | / /__ *
//* \/ \/ \/ \/ |_____/ /_____/ *
//* *
//* *
//************************************************************
//
// 代码千万行,规范第一条;编码不规范,队友两行泪!
//
// Created by 路飞 on 2021/2/4.
//
//
// @class DZXCarouselView
// @abstract 轮播组件
// @discussion 轮播组件
//
/** Demo
DZXCarouselView *carouselView = [DZXCarouselView.alloc initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH(), 100) configBlock:^(DZXCarouselConfig *carouselConfig) {
carouselConfig.timeInterval = 2.0;
carouselConfig.padding = UIEdgeInsetsMake(8, 8, 8, 8);
carouselConfig.cornerRadius = 2.0;
carouselConfig.autoScroll = YES;
carouselConfig.subViewClass = @"LFTestView";
carouselConfig.propertyName = @"content";
carouselConfig.showPageControl = YES;
carouselConfig.pageControlTheme = DZXCarouselPageControlThemeLeft;
} tapBlock:^(NSInteger index, id data) {
[DZXToast showMsg:[NSString stringWithFormat:@"%ld---%@",index,data]];
}];
[self.view addSubview:carouselView];
carouselView.backgroundColor = UIColor.greenColor;
carouselView.dataArr = @[@"路飞路飞路飞1111",@"佐隆佐隆佐隆2222",@"乔巴乔巴乔巴3333",@"娜美娜美娜美4444"];
*/
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSUInteger, DZXCarouselTheme) {
DZXCarouselThemeHorizontal,//水平
DZXCarouselThemeVertical,//垂直
};
typedef NS_ENUM(NSUInteger, DZXCarouselPageControlTheme) {
DZXCarouselPageControlThemeCenter,//居中
DZXCarouselPageControlThemeLeft,//居左
DZXCarouselPageControlThemeRight,//居右
};
@class DZXCarouselConfig;
typedef void(^DZXCarouselConfigBlock)(DZXCarouselConfig *carouselConfig);
typedef void(^DZXCarouselTapBlock)(NSInteger index, id data);
@interface DZXCarouselConfig : NSObject
/// 间隔时间 单位:秒
@property (nonatomic, assign) CGFloat timeInterval;
/// 滚动方向
@property (nonatomic, assign) DZXCarouselTheme theme;
/// 内容视图类名
@property(nonatomic, copy) NSString *subViewClass;
/// 视图类数据源属性名 需自实现set方法
@property (nonatomic, copy) NSString *propertyName;
/// 自动滚动
@property (nonatomic, assign) BOOL autoScroll;
/// pageControl
@property (nonatomic, assign) BOOL showPageControl;
/// pageControl普通状态的图片
@property (nonatomic, strong) UIImage *normalPageImage;
/// pageControl选中状态的图片
@property (nonatomic, strong) UIImage *selectedPageImage;
/// pageControl位置
@property (nonatomic, assign) DZXCarouselPageControlTheme pageControlTheme;
/// pageControl居左右时的间距(DZXCarouselPageControlThemeCenter时无用)
@property (nonatomic, assign) CGFloat pageControlLeftRightMargin;
/// pageControl距离底部的间距
@property(nonatomic, assign) CGFloat pageControlBottomMargin;
/// pageControl的indicator大小
@property(nonatomic, assign) CGSize pageIndicatorSize;
/// pageControl的incicator之间的间距
@property(nonatomic, assign) CGFloat pageIndicatorSpaing;
/// 内容视图边距
@property (nonatomic, assign) UIEdgeInsets padding;
/// 内容视图圆角
@property (nonatomic, assign) CGFloat cornerRadius;
@end
@interface DZXCarouselView : UIView
@property (nonatomic, strong) UIScrollView *scrollView;
/// 数据
@property (nonatomic, strong) NSArray *dataArr;
/// 初始化轮播组件
/// @param frame frame
/// @param configBlock 配置回调
/// @param tapBlock 点击回调
- (instancetype)initWithFrame:(CGRect)frame configBlock:(DZXCarouselConfigBlock)configBlock tapBlock:(DZXCarouselTapBlock)tapBlock;
/// 开始定时器
- (void)timerStart;
/// 结束定时器
- (void)timerStop;
@end
This diff is collapsed.
Copyright (c) 2021 BoASir <lufei@zhuangdianwenhua.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
# DZXCarouselView
轮播组件
\ No newline at end of file
[![CI Status](https://img.shields.io/travis/BoASir/DZXCarouselView.svg?style=flat)](https://travis-ci.org/BoASir/DZXCarouselView)
[![Version](https://img.shields.io/cocoapods/v/DZXCarouselView.svg?style=flat)](https://cocoapods.org/pods/DZXCarouselView)
[![License](https://img.shields.io/cocoapods/l/DZXCarouselView.svg?style=flat)](https://cocoapods.org/pods/DZXCarouselView)
[![Platform](https://img.shields.io/cocoapods/p/DZXCarouselView.svg?style=flat)](https://cocoapods.org/pods/DZXCarouselView)
## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.
## Requirements
## Installation
DZXCarouselView is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod 'DZXCarouselView'
```
## Author
BoASir, lufei@zhuangdianwenhua.com
## License
DZXCarouselView is available under the MIT license. See the LICENSE file for more info.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment