iOS實現(xiàn)文字水平無間斷滾動效果
IOS跑馬燈效果,實現(xiàn)文字水平無間斷滾動,示例代碼如下:
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 //初始化數(shù)據(jù) -(void) initView{ if (!self.arrData) { self.arrData = @[ @{ @"newsId" :@"201507070942261935", @"newsImg" :@"http://bg.fx678.com/HTMgr/upload/UpFiles/20150707/sy_2015070709395519.jpg", @"newsTitle":@"三大理由歐元任性抗跌,歐元區(qū)峰會將為希臘定調" }, @{ @"newsId" :@"201507070929021220", @"newsImg" :@"http://bg.fx678.com/HTMgr/upload/UpFiles/20150707/sy_2015070709273545.jpg", @"newsTitle" :@"歐盟峰會或現(xiàn)希臘轉機,黃金打響1162保衛(wèi)戰(zhàn)" }, @{ @"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++; } //設置滾動區(qū)域大小 [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 - 動態(tài)獲取左邊位置 -(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:@"系統(tǒng)提示" 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
示例源碼下載:文字水平無間斷滾動效果
備注:該開發(fā)工具XCode 版本為 6.4,如無法直接運行,可新建項目將以上文件復制替換即可
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。
上一篇:淺談iOS開發(fā)如何適配暗黑模式(Dark Mode)
欄 目:IOS
下一篇:iOS導航欄控制的一些總結
本文地址:http://mengdiqiu.com.cn/a1/IOS/11877.html
您可能感興趣的文章
- 01-11iOS常用算法之兩個有序數(shù)組合并(要求時間復雜度為0(n))
- 01-11iOS 彈幕功能的實現(xiàn)思路圖解
- 01-11iOS調試Block引用對象無法被釋放的小技巧分享
- 01-11iOS動態(tài)更換Icon的全過程記錄
- 01-11iOS實現(xiàn)文本分頁的方法示例
- 01-11iOS常見宏理解及使用方法
- 01-11iOs遷至WKWebView跨過的一些坑
- 01-11iOS模擬中獎名單循環(huán)滾動效果
- 01-11Python一鍵查找iOS項目中未使用的圖片、音頻、視頻資源
- 01-11iOS中如何獲取某個視圖的截圖詳析


閱讀排行
本欄相關
- 01-11UILabel顯示定時器文本跳動問題的解決
- 01-11iOS常用算法之兩個有序數(shù)組合并(要
- 01-11iOS 彈幕功能的實現(xiàn)思路圖解
- 01-11詳解MacOs免密登錄CentOs操作步驟
- 01-11iOS動態(tài)更換Icon的全過程記錄
- 01-11iOS調試Block引用對象無法被釋放的小技
- 01-11iOS常見宏理解及使用方法
- 01-11iOS實現(xiàn)文本分頁的方法示例
- 01-11iOs遷至WKWebView跨過的一些坑
- 01-11iOS模擬中獎名單循環(huán)滾動效果
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery