欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-02-21 21:15:40
ios跑马灯效果,实现文字水平无间断滚动,示例代码如下: viewcontroller.h #import ...

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,如无法直接运行,可新建项目将以上文件复制替换即可

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