In me the tiger sniffs the rose.
心有猛虎,细嗅蔷薇。
欢迎来到我的博客: http://LeoDev.me
这是用于展示广告或信息的界面。
您可以使用来自 本地 或 互联网 的图片。
并且它不会影响其他滚动视图的 scrollsToTop
属性。
如果您觉得好,请给我一个星标,非常感谢!
LCBannerView 可在 CocoaPods 上使用。只需将以下内容添加到您的项目 Podfile
pod "LCBannerView" # Podfile
通过包含以下导入使用
#import "LCBannerView.h"
演示代码
// 1. From internet
[scrollView addSubview:({
LCBannerView *bannerView = [LCBannerView bannerViewWithFrame:CGRectMake(0, 300.0f, [UIScreen mainScreen].bounds.size.width, 200.0f)
delegate:self
imageURLs:URLs
placeholderImageName:nil
timeInterval:2.0f
currentPageIndicatorTintColor:[UIColor redColor]
pageIndicatorTintColor:[UIColor whiteColor]];
bannerView.pageDistance = 20.0f;
bannerView;
})];
// 2. From local
// If you using images from local, you should let images named: `[email protected]`, `[email protected]`...
// Than, you just give me a image named: `banner`. 😜
[scrollView addSubview:({
LCBannerView *bannerView = [[LCBannerView alloc] initWithFrame:CGRectMake(0, 20.0f, [UIScreen mainScreen].bounds.size.width, 200.0f)
delegate:self
imageName:@"banner"
count:3
timeInterval:3.0f
currentPageIndicatorTintColor:[UIColor orangeColor]
pageIndicatorTintColor:[UIColor whiteColor]];
bannerView;
})];
运行演示的建议
您应该在演示代码中注释掉 block 代码或 delegate 代码中的一个,这将帮助您更清晰地阅读代码。
更多信息见以下两个要点。
Block: (@optional
)
@property (nonatomic, copy) LCBannerViewDidClickedImageIndexBlock didClickedImageIndexBlock;
@property (nonatomic, copy) LCBannerViewDidScrollToIndexBlock didScrollToIndexBlock;
例如
bannerView.didClickedImageIndexBlock = ^(LCBannerView *bannerView, NSInteger index) {
NSLog(@"Block: Clicked image in %p at index: %d", bannerView, (int)index);
};
bannerView.didScrollToIndexBlock = ^(LCBannerView *bannerView, NSInteger index) {
NSLog(@"Block: Scrolled in %p to index: %d", bannerView, (int)index);
};
// Logs
2016-07-29 15:41:00.344 LCBannerViewDemo[3251:295032] Block: Scrolled in 0x7ff473538ec0 to index: 1
2016-07-29 15:41:03.343 LCBannerViewDemo[3251:295032] Block: Scrolled in 0x7ff473538ec0 to index: 2
2016-07-29 15:41:05.132 LCBannerViewDemo[3251:295032] Block: Clicked image in 0x7ff473538ec0 at index: 2
2016-07-29 15:41:06.344 LCBannerViewDemo[3251:295032] Block: Scrolled in 0x7ff473538ec0 to index: 0
2016-07-29 15:41:09.344 LCBannerViewDemo[3251:295032] Block: Scrolled in 0x7ff473538ec0 to index: 1
2016-07-29 15:41:12.342 LCBannerViewDemo[3251:295032] Block: Scrolled in 0x7ff473538ec0 to index: 2
Delegate: (@optional
)
- (void)bannerView:(LCBannerView *)bannerView didClickedImageIndex:(NSInteger)index;
- (void)bannerView:(LCBannerView *)bannerView didScrollToIndex:(NSInteger)index;
例如
- (void)bannerView:(LCBannerView *)bannerView didClickedImageIndex:(NSInteger)index {
NSLog(@"Delegate: Clicked image in %p at index: %d", bannerView, (int)index);
}
- (void)bannerView:(LCBannerView *)bannerView didScrollToIndex:(NSInteger)index {
NSLog(@"Delegate: Scrolled in %p to index: %d", bannerView, (int)index);
}
// Logs
2016-07-29 15:41:45.296 LCBannerViewDemo[3293:296197] Delegate: Scrolled in 0x7f915b7349b0 to index: 1
2016-07-29 15:41:47.300 LCBannerViewDemo[3293:296197] Delegate: Scrolled in 0x7f915b7349b0 to index: 0
2016-07-29 15:41:48.429 LCBannerViewDemo[3293:296197] Delegate: Clicked image in 0x7f915b7349b0 at index: 0
2016-07-29 15:41:49.308 LCBannerViewDemo[3293:296197] Delegate: Scrolled in 0x7f915b7349b0 to index: 1
2016-07-29 15:41:51.297 LCBannerViewDemo[3293:296197] Delegate: Scrolled in 0x7f915b7349b0 to index: 0
自定义参数
// Distance to bottom of pageControl. Default is `10.0f`.
@property (nonatomic, assign) CGFloat pageDistance;
// Not allow scrolling. Default `No`, allow scrolling.
@property (nonatomic, assign) BOOL notScrolling;
隐藏 pageControl 支持
// Hide or show pageControl. Default is `NO`, show pageControl.
@property (nonatomic, assign) BOOL hidePageControl;
实现要求:由 问题 6 by skyboy1342 提出的问题。
现在,您可以通过 Block 或 Delegate 获取点击图片的索引和滚动到的索引,请参见 用法。
计算:当您将 bannerView 添加到 SUPERVIEW 时,didScrollToIndex
方法不会在索引 0 上调用,因为还没有滚动过,而且事实上您知道索引是 0,因为您这样做。
更改一些属性和方法的名字
// Properties' name.
@property (nonatomic, assign) CGFloat timerInterval;
->
@property (nonatomic, assign) CGFloat timeInterval;
@property (nonatomic, copy ) NSString *placeholderImage;
->
@property (nonatomic, copy ) NSString *placeholderImageName;
// All methods' name.
timerInterval -> timeInterval
placeholderImage -> placeholderImageName
现在可以定制滚动时应该显示的图片,感谢 ac1dpax。例如
LCBannerView *bannerView = [[LCBannerView alloc] initWithFrame:CGRectMake(0, 20.0f, [UIScreen mainScreen].bounds.size.width, 200.0f)
delegate:self
imageName:@"banner"
count:3
timerInterval:3.0f
currentPageIndicatorTintColor:[UIColor orangeColor]
pageIndicatorTintColor:[UIColor whiteColor]];
bannerView.pageDistance = 20.0f;
bannerView.notScrolling = YES; // THIS LINE ⬅️
bannerView;
现在可以自定义页面控制底部的距离。例如
// ...
bannerView.pageDistance = 20.0f;
如果您有任何问题,只需 提交问题!谢谢!
博客: http://LeoDev.me
与支付宝或微信支付友好赞助,谢谢!