码迷,mamicode.com
首页 > 移动开发 > 详细

IOS实现文字水平无间断滚动

时间:2015-07-08 14:44:14      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:objective-c   ios   跑马灯   文字滚动   无间断   

IOS实现文字水平无间断滚动

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

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
    NSTimer           *timer;
    UIScrollView      *scrollViewText;
}

@property (nonatomic ,strong) NSArray *arrData;

@end

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"  :@"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, 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 offsetX = 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 offsetX = 0.0;

        for (UILabel *lab in scrollViewText.subviews) {

            rect = lab.frame;
            offsetX = rect.origin.x - K_MAIN_VIEW_SCROLLER_SPACE;
            if (offsetX < -K_MAIN_VIEW_SCROLLER_LABLE_WIDTH)
                offsetX = startX;

            lab.frame = CGRectMake(offsetX, rect.origin.y, rect.size.width, rect.size.height);
        }

        NSLog(@"offsetX:%f",offsetX);
    }];

}

@end

示例源码下载:

http://yun.baidu.com/share/link?shareid=3134999&uk=3104876438

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


版权声明:本文为博主原创文章,未经博主允许不得转载。

IOS实现文字水平无间断滚动

标签:objective-c   ios   跑马灯   文字滚动   无间断   

原文地址:http://blog.csdn.net/yimiyuangguang/article/details/46801803

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!