上下滚动的公告图
来源:互联网 发布:淘宝代购订单管理系统 编辑:程序博客网 时间:2024/06/05 17:22
创建一个基于UIView的类
.h文件中
.h文件中#import <UIKit/UIKit.h>@class GYChangeTextView;@protocol GYChangeTextViewDelegate <NSObject>
//点击公告方法- (void)gyChangeTextView:(GYChangeTextView *)textView didTapedAtIndex:(NSInteger)index;@end@interface GYChangeTextView : UIView@property (nonatomic, assign) id<GYChangeTextViewDelegate> delegate;- (void)animationWithTexts:(NSArray *)textAry;- (void)stopAnimation;@end
.m文件中
#import "GYChangeTextView.h"#define DEALY_WHEN_TITLE_IN_MIDDLE 3.0#define DEALY_WHEN_TITLE_IN_BOTTOM 0.0typedef NS_ENUM(NSUInteger, GYTitlePosition) { GYTitlePositionTop = 1, GYTitlePositionMiddle = 2, GYTitlePositionBottom = 3};@interface GYChangeTextView ()@property (nonatomic, strong) UILabel *textLabel;@property (nonatomic, strong) NSArray *contentsAry;@property (nonatomic, assign) CGPoint topPosition;@property (nonatomic, assign) CGPoint middlePosition;@property (nonatomic, assign) CGPoint bottomPosition;/* *1.控制延迟时间,当文字在中间时,延时时间长一些,如5秒,这样可以让用户浏览清楚内容; *2.当文字隐藏在底部的时候,不需要延迟,这样衔接才流畅; *3.通过上面的宏定义去更改需要的值 */@property (nonatomic, assign) CGFloat needDealy;@property (nonatomic, assign) NSInteger currentIndex; /*当前播放到那个标题了*/@property (nonatomic, assign) BOOL shouldStop; /*是否停止*/@end@implementation GYChangeTextView//重新系统方法- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.topPosition = CGPointMake(self.frame.size.width/2, self.frame.size.height/2-23); self.middlePosition = CGPointMake(self.frame.size.width/2, self.frame.size.height/2); self.bottomPosition = CGPointMake(self.frame.size.width/2, self.frame.size.height/2+23); self.shouldStop = NO; UIImageView *advertiView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 20, 20)]; advertiView.image = [UIImage imageNamed:@"advertis"]; [self addSubview:advertiView]; _textLabel = [[UILabel alloc] init]; _textLabel.layer.bounds = CGRectMake(0, 0, CGRectGetWidth(frame)-100, CGRectGetHeight(frame)); _textLabel.layer.position = self.middlePosition; _textLabel.textAlignment = NSTextAlignmentLeft; [self addSubview:_textLabel]; self.clipsToBounds = YES; /*保证文字不跑出视图*/ self.needDealy = DEALY_WHEN_TITLE_IN_MIDDLE; /*控制第一次显示时间*/ self.currentIndex = 0; } return self;}//给动画添加内容- (void)animationWithTexts:(NSArray *)textAry { self.contentsAry = textAry; self.textLabel.text = [textAry objectAtIndex:0]; [self startAnimation];}//开始动画- (void)startAnimation { __weak typeof(self) weakSelf = self; [UIView animateWithDuration:0.3 delay:self.needDealy options:UIViewAnimationOptionCurveEaseInOut animations:^{ if ([weakSelf currentTitlePosition] == GYTitlePositionMiddle) { weakSelf.textLabel.layer.position = weakSelf.topPosition; } else if ([weakSelf currentTitlePosition] == GYTitlePositionBottom) { weakSelf.textLabel.layer.position = weakSelf.middlePosition; } } completion:^(BOOL finished) { if ([weakSelf currentTitlePosition] == GYTitlePositionTop) { weakSelf.textLabel.layer.position = weakSelf.bottomPosition; weakSelf.needDealy = DEALY_WHEN_TITLE_IN_BOTTOM; weakSelf.currentIndex ++; weakSelf.textLabel.text = [weakSelf.contentsAry objectAtIndex:[weakSelf realCurrentIndex]]; } else { weakSelf.needDealy = DEALY_WHEN_TITLE_IN_MIDDLE; } if (!weakSelf.shouldStop) { [weakSelf startAnimation]; } else { //停止动画后,要设置label位置和label显示内容 weakSelf.textLabel.layer.position = weakSelf.middlePosition; weakSelf.textLabel.text = [weakSelf.contentsAry objectAtIndex:[weakSelf realCurrentIndex]]; } }];}//停止动画- (void)stopAnimation { self.shouldStop = YES;}- (NSInteger)realCurrentIndex { return self.currentIndex % [self.contentsAry count];}- (GYTitlePosition)currentTitlePosition { if (self.textLabel.layer.position.y == self.topPosition.y) { return GYTitlePositionTop; } else if (self.textLabel.layer.position.y == self.middlePosition.y) { return GYTitlePositionMiddle; } return GYTitlePositionBottom;}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { if ([self.delegate respondsToSelector:@selector(gyChangeTextView:didTapedAtIndex:)]) { [self.delegate gyChangeTextView:self didTapedAtIndex:[self realCurrentIndex]]; }}@end
在控制器里调用
GYChangeTextView *tView = [[GYChangeTextView alloc] initWithFrame:CGRectMake(0, JYZScreenH*0.3, JYZScreenW, 30)]; tView.backgroundColor = [UIColor colorWithHexString:@"eeeeee"]; tView.delegate = self; [view addSubview:tView]; self.tView = tView;[self.tView animationWithTexts:self.advertisArr];#pragma mark 公告视图代理- (void)gyChangeTextView:(GYChangeTextView *)textView didTapedAtIndex:(NSInteger)index{}
0 0
- 上下滚动的公告图
- 图片上下循环滚动的公告导航
- 上下滚动公告
- 上下轮流滚动公告代码
- Android上下滚动的textview(可作为网站公告)
- 左右滚动公告代码和上下滚动公告代码
- 利用flipview控件实现上下滚动达到公告条的效果
- 通知公告TextSwitcher自动上下滚动带点击事件
- jQuery网站新闻公告上下滚动自动轮播代码
- 上下滚动的文字
- 简单的上下滚动
- 上下滚动的代码
- 滚动 公告
- 实现滚动的公告标题以及公告详情文本框
- 实现上下滚动的TextView
- 实现上下滚动的TextView
- 实现上下滚动的TextView
- 使用上下的滚动条
- 设置C++控制台输出行数
- SAP中使用BDC创建或修改采购信息记录
- c++第五次上机作业
- 如何恢复系统桌面文件
- 标准对数视力表与小数记录视力表的换算
- 上下滚动的公告图
- Effective C++ 读书笔记(一)
- web项目中的跨域问题解决方法
- 前端知识体系架构
- 二叉树面试题
- 561. Array Partition I
- 深度神经网络算法--资料视频代码全套卖,发了EI和SCI,博主担保(CNN,DBN,CDBN,DNN等)
- Android中radioButton的默认选中
- 进制转换、位运算、逻辑运算