标签:width shu def turn sch 滑动 ram scheduled targe
创建一个View
#import <UIKit/UIKit.h>
typedef void(^clickLabelBlock)(NSInteger index,NSString *titleString);
@interface CCPScrollView : UIView
/**
* 文字数组
*/
@property (nonatomic,strong) NSArray *titleArray;
/**
* 拼接后的文字数组
*/
@property (nonatomic,strong) NSMutableArray *titleNewArray;
/**
* 是否可以拖拽
*/
@property (nonatomic,assign) BOOL isCanScroll;
/**
* block回调
*/
@property (nonatomic,copy)void(^clickLabelBlock)(NSInteger index,NSString *titleString);
/**
* 字体颜色
*/
@property (nonatomic,strong) UIColor *titleColor;
/**
* 背景颜色
*/
@property (nonatomic,strong) UIColor *BGColor;
/**
* 字体大小
*/
@property (nonatomic,assign) CGFloat titleFont;
/**
* 关闭定时器
*/
- (void)removeTimer;
/**
* 添加定时器
*/
- (void)addTimer;
/**
* label的点击事件
*/
- (void) clickTitleLabel:(clickLabelBlock) clickLabelBlock;
@end
#import "CCPScrollView.h"
@interface CCPScrollView ()<UIScrollViewDelegate>
/**
* 滚动视图
*/
@property (nonatomic,strong) UIScrollView *ccpScrollView;
/**
* label的宽度
*/
@property (nonatomic,assign) CGFloat labelW;
/**
* label的高度
*/
@property (nonatomic,assign) CGFloat labelH;
/**
* 定时器
*/
@property (nonatomic,strong) NSTimer *timer;
/**
* 记录滚动的页码
*/
@property (nonatomic,assign) int page;
@end
@implementation CCPScrollView
- (UIScrollView *)ccpScrollView {
if (_ccpScrollView == nil) {
_ccpScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_ccpScrollView.showsHorizontalScrollIndicator = NO;
_ccpScrollView.showsVerticalScrollIndicator = NO;
_ccpScrollView.scrollEnabled = NO;
_ccpScrollView.pagingEnabled = YES;
[self addSubview:_ccpScrollView];
[_ccpScrollView setContentOffset:CGPointMake(0 , self.labelH) animated:YES];
}
return _ccpScrollView;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.labelW = frame.size.width;
self.labelH = frame.size.height;
self.ccpScrollView.delegate = self;
[self addTimer];
}
return self;
}
//重写set方法 创建对应的label
- (void)setTitleArray:(NSArray *)titleArray {
_titleArray = titleArray;
if (titleArray == nil) {
[self removeTimer];
return;
}
if (titleArray.count == 1) {
[self removeTimer];
}
id lastObj = [titleArray lastObject];
NSMutableArray *objArray = [[NSMutableArray alloc] init];
[objArray addObject:lastObj];
[objArray addObjectsFromArray:titleArray];
self.titleNewArray = objArray;
//CGFloat contentW = 0;
CGFloat contentH = self.labelH *objArray.count;
self.ccpScrollView.contentSize = CGSizeMake(0, contentH);
CGFloat labelW = self.ccpScrollView.frame.size.width;
self.labelW = labelW;
CGFloat labelH = self.ccpScrollView.frame.size.height;
self.labelH = labelH;
CGFloat labelX = 0;
// //防止重复赋值数据叠加
// for (id label in self.ccpScrollView.subviews) {
//
// [label removeFromSuperview];
//
// }
[self.ccpScrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj removeFromSuperview];
}];
for (int i = 0; i < objArray.count; i++) {
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.userInteractionEnabled = YES;
titleLabel.tag = 100 + i;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTheLabel:)];
[titleLabel addGestureRecognizer:tap];
titleLabel.textAlignment = NSTextAlignmentLeft;
CGFloat labelY = i * labelH;
titleLabel.frame = CGRectMake(labelX, labelY, labelW, labelH);
titleLabel.text = objArray[i];
[self.ccpScrollView addSubview:titleLabel];
}
}
- (void)clickTheLabel:(UITapGestureRecognizer *)tap {
if (self.clickLabelBlock) {
NSInteger tag = tap.view.tag - 1;
if (tag < 100) {
tag = 100 + (self.titleArray.count - 1);
}
self.clickLabelBlock(tag - 100,self.titleArray[tag - 100]);
}
}
- (void) clickTitleLabel:(clickLabelBlock) clickLabelBlock {
self.clickLabelBlock = clickLabelBlock;
}
- (void)setIsCanScroll:(BOOL)isCanScroll {
if (isCanScroll) {
self.ccpScrollView.scrollEnabled = YES;
} else {
self.ccpScrollView.scrollEnabled = NO;
}
}
- (void)setTitleColor:(UIColor *)titleColor {
_titleColor = titleColor;
for (UILabel *label in self.ccpScrollView.subviews) {
label.textColor = titleColor;
}
}
- (void)setTitleFont:(CGFloat )titleFont {
_titleFont = titleFont;
for (UILabel *label in self.ccpScrollView.subviews) {
label.font = [UIFont systemFontOfSize: titleFont];;
}
}
- (void)setBGColor:(UIColor *)BGColor {
_BGColor = BGColor;
self.backgroundColor = BGColor;
}
- (void)nextLabel {
CGPoint oldPoint = self.ccpScrollView.contentOffset;
oldPoint.y += self.ccpScrollView.frame.size.height;
[self.ccpScrollView setContentOffset:oldPoint animated:YES];
}
//当滚动时调用scrollView的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.ccpScrollView.contentOffset.y == self.ccpScrollView.frame.size.height*(self.titleArray.count )) {
[self.ccpScrollView setContentOffset:CGPointMake(0, 0) animated:NO];
}
}
// 开始拖拽的时候调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self removeTimer];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//开启定时器
[self addTimer];
}
- (void)addTimer{
/*
scheduledTimerWithTimeInterval: 滑动视图的时候timer会停止
这个方法会默认把Timer以NSDefaultRunLoopMode添加到主Runloop上,而当你滑tableView的时候,就不是NSDefaultRunLoopMode了,这样,你的timer就会停了。
self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(nextLabel) userInfo:nil repeats:YES];
*/
self.timer = [NSTimer timerWithTimeInterval:3.0 target:self selector:@selector(nextLabel) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)removeTimer {
[self.timer invalidate];
self.timer = nil;
}
- (void)dealloc {
[self.timer invalidate];
self.timer = nil;
}
@end
你需要显示的界面
#import "CCPScrollView.h"
下边的东西起一个方法,调用就好
CCPScrollView *ccpView = [[CCPScrollView alloc] initWithFrame:CGRectMake(65, 0, self.testView.frame.size.width - 60, self.testView.frame.size.height)];
ccpView.titleArray = _gonggaoshuzu;
ccpView.titleFont = 14;
ccpView.titleColor = [UIColor blackColor];
ccpView.BGColor = CCPRGBColor(255, 255, 255);
[ccpView clickTitleLabel:^(NSInteger index,NSString *titleString) {
NSLog(@"%ld-----%@",(long)index,titleString);
//自定义的弹出view
UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 15 * 2, 250)];
alertView.backgroundColor = [UIColor whiteColor];
alertView.layer.cornerRadius = 8;
alertView.layer.masksToBounds = YES;
UILabel *alertLabel = [[UILabel alloc] init];
alertLabel.textAlignment = NSTextAlignmentLeft;
alertLabel.text = titleString;
alertLabel.font = [UIFont systemFontOfSize:17];
alertLabel.numberOfLines = 0;
alertLabel.width = [UIScreen mainScreen].bounds.size.width - 15 * 2;
[alertLabel sizeToFit];
alertLabel.centerX = alertView.centerX;
alertLabel.centerY = alertView.centerY;
[alertView addSubview:alertLabel];
//弹出自定义弹窗
CCPActionSheetView *actionSheetView = [[CCPActionSheetView alloc] initWithAlertView:alertView];
actionSheetView.viewAnimateStyle = ViewAnimateScale;
}];
[self.testView addSubview:ccpView];
标签:width shu def turn sch 滑动 ram scheduled targe
原文地址:http://www.cnblogs.com/wangqitze/p/7771580.html