“买菜大王”通过精心收集,向本站投稿了10篇IOS实现文字水平无间断滚动,下面是小编整理后的IOS实现文字水平无间断滚动,欢迎阅读分享,希望对大家有所帮助。
- 目录
篇1:IOS实现文字水平无间断滚动
IOS实现文字水平无间断滚动
IOS跑马灯效果,实现文字水平无间断滚动,示例代码如下:
ViewController.h
#import
ViewController.m
#import ViewController.h#pragma mark - Class define variable#define K_MAIN_VIEW_SCROLL_HEIGHT 80.0f#define K_MAIN_VIEW_SCROLL_TEXT_TAG 300#define K_MAIN_VIEW_TEME_INTERVAL 0.35 //计时器间隔时间(单位秒)#define K_MAIN_VIEW_SCROLLER_SPACE 20 //每次移动的距离#define K_MAIN_VIEW_SCROLLER_LABLE_WIDTH 280 //字体宽度#define K_MAIN_VIEW_SCROLLER_LABLE_MARGIN 20 //前后间隔距离@interface ViewController @end@implementation ViewController#pragma mark - Class property@synthesize arrData;- (void)viewDidLoad { [super viewDidLoad]; [self initView];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark - Custom method//初始化数据-(void) initView{ if (!self.arrData) { self.arrData = @[ @{@newsId :@201507070942261935,@newsImg :@bg.fx678.com/HTMgr/upload/UpFiles/20150707/sy_2015070709395519.jpg,@newsTitle:@三大理由欧元任性抗跌,欧元区峰会将为希腊定调 }, @{@newsId :@201507070929021220,@newsImg :@bg.fx678.com/HTMgr/upload/UpFiles/20150707/sy_2015070709273545.jpg,@newsTitle :@欧盟峰会或现希腊转机,黄金打响1162保卫战 }, @{@newsId :@201507070656471857,@newsImg :@bg.fx678.com/HTMgr/upload/UpFiles/20150707/2015070706533134.jpg,@newsTitle :@希腊困局欧元不怕,油价服软暴跌8% } ]; } //文字滚动 [self initScrollText]; //开启滚动 [self startScroll];}//文字滚动初始化-(void) initScrollText{ //获取滚动条 scrollViewText = (UIScrollView *)[self.view viewWithTag:K_MAIN_VIEW_SCROLL_TEXT_TAG]; if(!scrollViewText){ scrollViewText = [[UIScrollView alloc] initWithFrame.:CGRectMake(0, 0, self.view.frame.size.width, K_MAIN_VIEW_SCROLL_HEIGHT)]; scrollViewText.showsHorizontalScrollIndicator = NO; //隐藏水平滚动条 scrollViewText.showsVerticalScrollIndicator = NO; //隐藏垂直滚动条 scrollViewText.tag = K_MAIN_VIEW_SCROLL_TEXT_TAG; [scrollViewText setBackgroundColor:[UIColor grayColor]]; //清除子控件 for (UIView *view in [scrollViewText subviews]) {[view removeFromSuperview]; } //添加到当前视图 [self.view addSubview:scrollViewText]; } if (self.arrData) { CGFloat ffsetX = 0 ,i = 0, h = 30; //设置滚动文字 UILabel *labText = nil; for (NSDictionary *dicTemp in self.arrData) {labText = [[UILabel alloc] initWithFrame.:CGRectMake( i * (K_MAIN_VIEW_SCROLLER_LABLE_WIDTH + K_MAIN_VIEW_SCROLLER_LABLE_MARGIN), (K_MAIN_VIEW_SCROLL_HEIGHT - h) / 2, K_MAIN_VIEW_SCROLLER_LABLE_WIDTH, h)];[labText setFont:[UIFont systemFontOfSize:18]];[labText setTextColor:[UIColor redColor]];labText.text = dicTemp[@newsTitle];offsetX += labText.frame.origin.x;//添加到滚动视图[scrollViewText addSubview:labText];i++; } //设置滚动区域大小 [scrollViewText setContentSize:CGSizeMake(offsetX, 0)]; }}//开始滚动-(void) startScroll{ if (!timer) timer = [NSTimer scheduledTimerWithTimeInterval:K_MAIN_VIEW_TEME_INTERVAL target:self selector:@selector(setScrollText) userInfo:nil repeats:YES]; [timer fire];}//滚动处理-(void) setScrollText{ CGFloat startX = scrollViewText.contentSize.width - K_MAIN_VIEW_SCROLLER_LABLE_WIDTH - K_MAIN_VIEW_SCROLLER_LABLE_MARGIN; [UIView animateWithDuration:K_MAIN_VIEW_TEME_INTERVAL * 2 animations:^{ CGRect rect; CGFloat ffsetX = 0.0; for (UILabel *lab in scrollViewText.subviews) {rect = lab.frame;ffsetX = rect.origin.x - K_MAIN_VIEW_SCROLLER_SPACE;if (offsetX < -K_MAIN_VIEW_SCROLLER_LABLE_WIDTH) ffsetX = startX;lab.frame. = CGRectMake(offsetX, rect.origin.y, rect.size.width, rect.size.height); } NSLog(@offsetX:%f,offsetX); }];}@end
篇2:无间断滚动效果 htc
作者: 字体:[增加 减小] 类型:转载
marquee.htc
代码如下:
// 描述 : 无间断滚动字幕
// 版本 : 2.0
// 作者 : 宝玉(WebUC.NET)
// 最新更新 : 2004-10-26
// 备 :
HTML文件
代码如下:
无间断滚动
每个标题间有停留
鼠标移入停止,鼠标移出继续滚动
1. 宝玉(www.webuc.net)作品
2. 强强联手,助推上海建筑领域信息化建设
3. 广联达清单招标投标策略研讨会――河南站圆满成功
4. 足球友谊赛:河北电建一公司 VS 广联达石家庄分公司
5. 广联达――清单算量软件 GCL7.0新版出炉!
6. 喜报:广联达公司顺利通过ISO9000质量管理体系三年复审
7. 广联达-清单整体解决方案在北京求实造价咨询公司的成功应用
8. 广联达-施工项目成本管理系统(GCM)在荣尊堡工程中的应用
9. 广联达-工程概预算软件在北京建工集团总公司东方广场工程的应用
10. asdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
篇3:iOS 关于滚动视图contentSize、contentOffset、contentInset
iOS 关于滚动视图contentSize、contentOffset、contentInset 的整理
contentSize 是scrollview可以滚动的区域,比如frame. = (0 ,0 ,320 ,480) contentSize = (320 ,960),代表你的scrollview可以上下滚动,滚动区域为frame大小的两倍,
contentOffset 是scrollview当前显示区域顶点相对于frame顶点的偏移量,比如上个例子你拉到最下面,contentoffset就是(0 ,480),也就是y偏移了480
contentInset 是scrollview的contentview的顶点相对于scrollview的位置,例如你的contentInset = (0 ,100),那么你的contentview就是从scrollview的(0 ,100)开始显示
另外UITableView是UIScrollView的子类,它们在上述属性又有所不同,tabelview的contentsize是由它的下列方法共同实现的
- (NSInteger)numberOfSections;
- (NSInteger)numberOfRowsInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
它会自动计算所有的高度和来做为它的contentsize的height.
例如你在delegate方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
那么你的tabelview的contentsize就是(320, 4400)
篇4:iOS实现抽屉效果
1.开始启动的时候,新建3个不同颜色的View的
1.设置3个成员属性,记录三种颜色的View
@property (nonatomic,weak) UIView* redView;@property (nonatomic,weak) UIView* greenView;@property (nonatomic,weak) UIView* blueView;
2.初始化3个View
- (void)setUpthreeViews{ UIView *blueView = [[UIView alloc]initWithFrame.:self.view.bounds]; blueView.backgroundColor = [UIColor blueColor]; [self.view addSubview:blueView]; _blueView = blueView; UIView *greenView = [[UIView alloc]initWithFrame.:self.view.bounds]; greenView.backgroundColor = [UIColor greenColor]; [self.view addSubview:greenView]; _greenView = greenView; UIView *redView = [[UIView alloc]initWithFrame.:self.view.bounds]; redView.backgroundColor = [UIColor redColor]; [self.view addSubview:redView]; _redView = redView;}
2.实现滑动的效果
1.通过-(void)touchesMoved:(NSSet)touches withEvent:(UIEvent)event方法来获得当前点和初始点,从而计算出偏移量,然后计算redView的frame的值:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; //获得当前点 CGPoint currentPoint = [touch locationInView:_redView]; //获得起点 CGPoint prePoint = [touch previousLocationInView:_redView]; //计算在x轴方向上的偏移量 CGFloat moveX = currentPoint.x - prePoint.x; //然后通过在x轴上的偏移量,计算redView的frame. _redView.frame. = [self framWithOffsetX:moveX];}
假设x移到320时,y移动到60,算出每移动一点x,移动多少y
ffsetY = offsetX * 600 / 320 手指每移动一点,x轴偏移量多少,y偏移多少
为了好看,x移动到320,距离上下的高度需要保持一致,而且有一定的比例去缩放他的尺寸,iOS实现抽屉效果
。 touchMove只能拿到之前的frame.当前的高度 = 之前的高度 * 这个比例缩放比例:当前的高度/之前的高度 (screenH - 2 * offsetY) / screenH。
当前的宽度保持不变就行。 y值,计算比较特殊,不能直接用之前的y,加上offsetY,往左滑动,主视图应该往下走,但是offsetX是负数,导致主视图会往上走。所以需要判断是左滑还是右滑
- (CGRect)framWithOffsetX:(CGFloat)offsetX{ //计算在y轴方向上的偏移量 CGFloat ffsetY = offsetX/SCREENWIDTH * MAXYOFFSET; //根据y方向的偏移量计算缩放比例 CGFloat scale = (SCREENHEIGHT - 2*offsetY)/SCREENHEIGHT; //如果x < 0表示左滑 if (_redView.frame.origin.x < 0) { scale = (SCREENHEIGHT + 2*offsetY)/SCREENHEIGHT; } CGRect frame. = _redView.frame; //计算滑动之后的frame. CGFloat height = frame.size.height*scale; CGFloat width = frame.size.width; CGFloat x = frame.origin.x + offsetX; CGFloat y = (SCREENHEIGHT- height)* 0.5; return CGRectMake(x, y, width, height);}
2.通过KVO来监听redView的frame的变化,从而判断redView是左滑还是右滑,
电脑资料
往左移动,显示右边,隐藏左边 往右移动,显示左边,隐藏右边
- (void)viewDidLoad { [super viewDidLoad]; [self setUpthreeViews]; // 利用KVO时刻监听_redView.frame改变 // Observer:谁需要观察 // KeyPath:监听的属性名称 // options: NSKeyValueObservingOptionNew监听这个属性新值 [_redView addObserver:self forKeyPath:@frame. options:NSKeyValueObservingOptionNew context:nil];}// 只要监听的属性有新值的时候,只要redView.frame一改变就会调用- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ if (self.redView.frame.origin.x >0) { _greenView.hidden = NO; } else if(self.redView.frame.origin.x < 0){ _greenView.hidden = YES; }}// 当对象销毁的时候,一定要移除观察者- (void)dealloc{ [_redView removeObserver:self forKeyPath:@frame];}
3.设置触摸结束的时候,redView的frame。如果redView侧滑没有到屏幕的一半,则自动返回到初始位置。如果超过屏幕的一半,则停留在一个新的位置
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ CGFloat xPos = _redView.frame.origin.x; //大于屏幕的一半进入新的位置 if (xPos >SCREENWIDTH*0.5) { [UIView animateWithDuration:0.5 animations:^{self.redView.frame. = [self framWithBigThanX:ENDRIGHTX]; }]; return ; } //小于屏幕的一半,大于屏幕负一半的时候,则恢复到初始状态 if (xPos < SCREENWIDTH*0.5 && xPos >-SCREENWIDTH*0.5) { [UIView animateWithDuration:0.5 animations:^{self.redView.frame. = [UIScreen mainScreen].bounds; }]; return ; } //xPos < -SCREENWIDTH*0.5的时候,进入新的位置 [UIView animateWithDuration:0.5 animations:^{ self.redView.frame. = [self framWithSmallThanX:ENDLEFTX]; }];}- (CGRect)framWithBigThanX:(CGFloat)offsetX{ CGFloat ffsetY = offsetX/SCREENWIDTH * MAXYOFFSET; CGFloat scale = (SCREENHEIGHT - 2*offsetY)/SCREENHEIGHT; CGFloat height = SCREENHEIGHT*scale; CGFloat width = SCREENWIDTH; CGFloat x = offsetX; CGFloat y = (SCREENHEIGHT- height)* 0.5; return CGRectMake(x, y, width, height);}- (CGRect)framWithSmallThanX:(CGFloat)offsetX{ CGFloat ffsetY = offsetX/SCREENWIDTH * MAXYOFFSET; CGFloat scale = (SCREENHEIGHT + 2*offsetY)/SCREENHEIGHT; CGFloat height = SCREENHEIGHT*scale; CGFloat width = SCREENWIDTH; CGFloat x = offsetX; CGFloat y = (SCREENHEIGHT- height)* 0.5; return CGRectMake(x, y, width, height);}
篇5:IOS 如何实现画虚线
因为项目需要画虚线,起初想用图片重复叠加实现,
IOS 如何实现画虚线
。搜罗了一圈还是封装一个UIView
代码如下.h文件
#import
.m文件
#import DashesLineView.h#define kInterval 10 // 全局间距@implementation DashesLineView- (id)initWithFrame.:(CGRect)frame{ self= [super initWithFrame.:frame]; if(self) { _lineColor = [UIColor redColor]; _startPoint = CGPointMake(0, 1); _endPoint = CGPointMake(screen_width , 1); } return self;}- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext; CGContextBeginPath(context); CGContextSetLineWidth(context,0.5);//线宽度 CGContextSetStrokeColorWithColor(context,self.lineColor.CGColor); CGFloat lengths[] = {4,2};//先画4个点再画2个点 CGContextSetLineDash(context,0, lengths,2);//注意2(count)的值等于lengths数组的长度 CGContextMoveToPoint(context,self.startPoint.x,self.startPoint.y); CGContextAddLineToPoint(context,self.endPoint.x,self.endPoint.y); CGContextStrokePath(context); CGContextClosePath(context); }@end
篇6:Word97 下实现动态滚动
在 Office 2000 中,当用户拉动滚动条时,页面内容会动态更新,但是在 Word97 中却不会,
Word97 下实现动态滚动
,
其实我们可以通过修改注册表来使 Word97 具有这项功能:打开注册表编辑器,找到HKEY_CURRENT_USERSoftwareMicrosoftOffice8.0WordOptions,在“编辑”下的“新建”中找到“字符串值”,输入名字 LiveScrolling,并设置其值为 1,单击“确定”,退出注册表编辑器即可。