iOS实现文字水平无间断滚动效果

所属分类: 软件编程 / IOS 阅读数: 1282
收藏 0 赞 0 分享

IOS跑马灯效果,实现文字水平无间断滚动,示例代码如下:

ViewController.h

#import <UIKit/UIKit.h>
 
@interface ViewController : UIViewController{
  NSTimer      *timer;
  UIScrollView   *scrollViewText;
}
 
@property (nonatomic ,strong) NSArray *arrData;
 
@end

ViewController.m

//
// 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.0f       //每次移动的距离
#define K_MAIN_VIEW_SCROLLER_LABLE_WIDTH   18.0f  //单个字符宽度(与你设置的字体大小一致)
#define K_MAIN_VIEW_SCROLLER_LABLE_MARGIN  20.0f  //前后间隔距离
#define K_MAIN_VIEW_SCROLLER_SLEEP_INTERVAL 1    //停留时间
 
 
@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" :@"http://bg.fx678.com/HTMgr/upload/UpFiles/20150707/sy_2015070709395519.jpg",
               @"newsTitle":@"三大理由欧元任性抗跌,欧元区峰会将为希腊定调"
             },
             @{
               @"newsId"  :@"201507070929021220",
               @"newsImg"  :@"http://bg.fx678.com/HTMgr/upload/UpFiles/20150707/sy_2015070709273545.jpg",
               @"newsTitle" :@"欧盟峰会或现希腊转机,黄金打响1162保卫战"
             },
             @{
               @"newsId"  :@"201507070656471857",
               @"newsImg"  :@"http://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, 80, self.view.frame.size.width, K_MAIN_VIEW_SCROLL_HEIGHT)];
    scrollViewText.showsHorizontalScrollIndicator = NO;  //隐藏水平滚动条
    scrollViewText.showsVerticalScrollIndicator = NO;   //隐藏垂直滚动条
    scrollViewText.scrollEnabled = NO;          //禁用手动滑动
 
    //横竖屏自适应
    scrollViewText.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    scrollViewText.tag = K_MAIN_VIEW_SCROLL_TEXT_TAG;
    [scrollViewText setBackgroundColor:[UIColor grayColor]];
 
    //给滚动视图添加事件
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollerViewClick:)];
    [scrollViewText addGestureRecognizer:tapGesture];
 
    //添加到当前视图
    [self.view addSubview:scrollViewText];
  }else{
    //清除子控件
    for (UIView *view in [scrollViewText subviews]) {
      [view removeFromSuperview];
    }
  }
 
  if (self.arrData) {
 
    CGFloat offsetX = 0 ,i = 0, h = 30;
 
    //设置滚动文字
    UIButton *btnText = nil;
    NSString *strTitle = [[NSString alloc] init];
 
    for (NSDictionary *dicTemp in self.arrData) {
 
      strTitle = dicTemp[@"newsTitle"];
 
      btnText = [UIButton buttonWithType:UIButtonTypeCustom];
      [btnText setFrame:CGRectMake([self getTitleLeft:i],
                     (K_MAIN_VIEW_SCROLL_HEIGHT - h) / 2,
                     strTitle.length * K_MAIN_VIEW_SCROLLER_LABLE_WIDTH,
                     h)];
 
      [btnText setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
      [btnText setTitle:strTitle forState:UIControlStateNormal];
 
      //横竖屏自适应
      btnText.autoresizingMask = UIViewAutoresizingFlexibleWidth;
      offsetX += btnText.frame.origin.x;
 
      //设置为 NO,否则无法响应点击事件
      btnText.userInteractionEnabled = NO;
 
      //添加到滚动视图
      [scrollViewText addSubview:btnText];
 
      i++;
    }
 
    //设置滚动区域大小
    [scrollViewText setContentSize:CGSizeMake(offsetX, 0)];
  }
}
 
 
#pragma mark - 滚动处理
//开始滚动
-(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{
 
  [UIView animateWithDuration:K_MAIN_VIEW_TEME_INTERVAL * 2 animations:^{
    CGRect rect;
    CGFloat offsetX = 0.0,width = 0.0;
 
    for (UIButton *btnText in scrollViewText.subviews) {
 
      rect = btnText.frame;
      offsetX = rect.origin.x - K_MAIN_VIEW_SCROLLER_SPACE;
      width = [btnText.titleLabel.text length] * K_MAIN_VIEW_SCROLLER_LABLE_WIDTH;
 
      btnText.frame = CGRectMake(offsetX, rect.origin.y, rect.size.width, rect.size.height);
 
      NSLog(@"offsetX:%f",offsetX);
    }
 
    if (offsetX < -width){
      [UIView setAnimationsEnabled:NO];
      [self initScrollText];
    }else
      [UIView setAnimationsEnabled:YES];
  }];
 
}
 
 
#pragma mark - 动态获取左边位置
-(float) getTitleLeft:(CGFloat) i {
  float left = i * K_MAIN_VIEW_SCROLLER_LABLE_MARGIN;
 
  if (i > 0) {
    for (int j = 0; j < i; j ++) {
      left += [[self.arrData objectAtIndex:j][@"newsTitle"] length] * K_MAIN_VIEW_SCROLLER_LABLE_WIDTH;
    }
  }
 
  return left;
}
 
 
#pragma mark - 新闻点击事件
-(void)btnNewsClick:(UIButton *) sender{
 
  NSString *strNewsTitle = sender.titleLabel.text;
 
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"系统提示"
                          message:strNewsTitle
                          delegate:sender
                     cancelButtonTitle:@"确定"
                     otherButtonTitles:@"其他", nil];
  [alert show];
 
}
 
-(void)scrollerViewClick:(UITapGestureRecognizer*)gesture{
 
  CGPoint touchPoint = [gesture locationInView:scrollViewText];
 
  for (UIButton *btn in scrollViewText.subviews) {
 
    if ([btn.layer.presentationLayer hitTest:touchPoint]) {
      [self btnNewsClick:btn];
      break;
    }
 
  }
 
}
 
@end 

示例源码下载:文字水平无间断滚动效果

备注:该开发工具XCode 版本为 6.4,如无法直接运行,可新建项目将以上文件复制替换即可

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

iOS画出精美的图表方法示例

这篇文章主要给大家介绍了关于iOS如何画出精美的图表的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

ios 服务器端推送证书生成的方法

这篇文章主要介绍了ios 服务器端推送证书生成的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

浅谈iOS推送证书生成pem文件(详细生成过程)

这篇文章主要介绍了浅谈iOS推送证书生成pem文件(详细生成过程),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

iOS高仿微信文章悬浮球功能

这篇文章主要介绍了iOS高仿微信文章悬浮球功能,本文给大家介绍的非常详细,具有一定的参考解决价值,需要的朋友可以参考下
收藏 0 赞 0 分享

通过一行代码搞定UITextField的输入格式限制

这篇文章主要给大家介绍了如何通过一行代码搞定UITextField的输入格式限制的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

iOS判断是否越狱设备方法示例

这篇文章主要给大家介绍了关于iOS判断是否越狱设备的相关资料,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

iOS开发教程之Status Bar状态栏设置的方法汇总

iOS 的 Status Bar 状态栏是一个比较坑的地方,所以下面这篇文章主要给大家介绍了关于iOS开发教程之Status Bar状态栏设置的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS开发之导航栏各种右滑返回失效的解决方法汇总

这篇文章主要给大家总结介绍了关于iOS开发教程之导航栏各种右滑返回失效的解决方法,文中通过示例代码介绍的非常详细,对各位iOS具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

iOS实现简单的头部缩放功能

这篇文章主要介绍了iOS 简单的头部缩放效果,头部伴随模糊效果放大缩小,并在一定位置时悬停充当导航栏,本文给大家提供实现思路,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS中震动反馈(UIFeedbackGenerator)与系统震动详解

最近要做一个项目,需要持续响铃并振动,所以就有了这篇文章,下面这篇文章主要给大家介绍了关于iOS中震动反馈(UIFeedbackGenerator)与系统震动的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多