標籤:objective-c ios 跑馬燈 文字滾動 無間斷
IOS實現文字水平無間斷滾動
IOS跑馬燈效果,實現文字水平無間斷滾動,範例程式碼如下:
ViewController.h
#import <UIKit/UIKit.h>@interface ViewController : UIViewController{ NSTimer *timer; UIScrollView *scrollViewText;}@property (nonatomic ,strong) NSArray *arrData;@endViewController.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實現文字水平無間斷滾動