欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS實(shí)現(xiàn)文字水平無間斷滾動(dòng)效果

 更新時(shí)間:2019年08月26日 16:32:12   作者:追夢秋陽  
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)文字水平無間斷滾動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

IOS跑馬燈效果,實(shí)現(xiàn)文字水平無間斷滾動(dòng),示例代碼如下:

ViewController.h

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

ViewController.m

//
// ViewController.m
// 滾動(dòng)
//
#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        //計(jì)時(shí)器間隔時(shí)間(單位秒)
#define K_MAIN_VIEW_SCROLLER_SPACE 20.0f       //每次移動(dòng)的距離
#define K_MAIN_VIEW_SCROLLER_LABLE_WIDTH   18.0f  //單個(gè)字符寬度(與你設(shè)置的字體大小一致)
#define K_MAIN_VIEW_SCROLLER_LABLE_MARGIN  20.0f  //前后間隔距離
#define K_MAIN_VIEW_SCROLLER_SLEEP_INTERVAL 1    //停留時(shí)間
 
 
@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ū)峰會(huì)將為希臘定調(diào)"
             },
             @{
               @"newsId"  :@"201507070929021220",
               @"newsImg"  :@"http://bg.fx678.com/HTMgr/upload/UpFiles/20150707/sy_2015070709273545.jpg",
               @"newsTitle" :@"歐盟峰會(huì)或現(xiàn)希臘轉(zhuǎn)機(jī),黃金打響1162保衛(wèi)戰(zhàn)"
             },
             @{
               @"newsId"  :@"201507070656471857",
               @"newsImg"  :@"http://bg.fx678.com/HTMgr/upload/UpFiles/20150707/2015070706533134.jpg",
               @"newsTitle" :@"希臘困局歐元不怕,油價(jià)服軟暴跌8%"
             }
           ];
  }
 
  //文字滾動(dòng)
  [self initScrollText];
 
  //開啟滾動(dòng)
  [self startScroll];
 
}
 
 
//文字滾動(dòng)初始化
-(void) initScrollText{
 
  //獲取滾動(dòng)條
  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;  //隱藏水平滾動(dòng)條
    scrollViewText.showsVerticalScrollIndicator = NO;   //隱藏垂直滾動(dòng)條
    scrollViewText.scrollEnabled = NO;          //禁用手動(dòng)滑動(dòng)
 
    //橫豎屏自適應(yīng)
    scrollViewText.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    scrollViewText.tag = K_MAIN_VIEW_SCROLL_TEXT_TAG;
    [scrollViewText setBackgroundColor:[UIColor grayColor]];
 
    //給滾動(dòng)視圖添加事件
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollerViewClick:)];
    [scrollViewText addGestureRecognizer:tapGesture];
 
    //添加到當(dāng)前視圖
    [self.view addSubview:scrollViewText];
  }else{
    //清除子控件
    for (UIView *view in [scrollViewText subviews]) {
      [view removeFromSuperview];
    }
  }
 
  if (self.arrData) {
 
    CGFloat offsetX = 0 ,i = 0, h = 30;
 
    //設(shè)置滾動(dòng)文字
    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];
 
      //橫豎屏自適應(yīng)
      btnText.autoresizingMask = UIViewAutoresizingFlexibleWidth;
      offsetX += btnText.frame.origin.x;
 
      //設(shè)置為 NO,否則無法響應(yīng)點(diǎn)擊事件
      btnText.userInteractionEnabled = NO;
 
      //添加到滾動(dòng)視圖
      [scrollViewText addSubview:btnText];
 
      i++;
    }
 
    //設(shè)置滾動(dòng)區(qū)域大小
    [scrollViewText setContentSize:CGSizeMake(offsetX, 0)];
  }
}
 
 
#pragma mark - 滾動(dòng)處理
//開始滾動(dòng)
-(void) startScroll{
 
  if (!timer)
    timer = [NSTimer scheduledTimerWithTimeInterval:K_MAIN_VIEW_TEME_INTERVAL target:self selector:@selector(setScrollText) userInfo:nil repeats:YES];
 
  [timer fire];
 
}
 
 
//滾動(dòng)處理
-(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 - 動(dòng)態(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 - 新聞點(diǎn)擊事件
-(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 

示例源碼下載:文字水平無間斷滾動(dòng)效果

備注:該開發(fā)工具XCode 版本為 6.4,如無法直接運(yùn)行,可新建項(xiàng)目將以上文件復(fù)制替換即可

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS App設(shè)計(jì)模式開發(fā)中對(duì)interpreter解釋器模式的運(yùn)用

    iOS App設(shè)計(jì)模式開發(fā)中對(duì)interpreter解釋器模式的運(yùn)用

    這篇文章主要介紹了iOS App設(shè)計(jì)模式開發(fā)中對(duì)interpreter解釋器模式的運(yùn)用,示例為傳統(tǒng)的Objective-C寫成,需要的朋友可以參考下
    2016-04-04
  • iOS 實(shí)現(xiàn)簡單的加載等待動(dòng)畫示例(思路與實(shí)現(xiàn))

    iOS 實(shí)現(xiàn)簡單的加載等待動(dòng)畫示例(思路與實(shí)現(xiàn))

    本篇文章主要介紹了iOS 實(shí)現(xiàn)簡單的加載等待動(dòng)畫示例(思路與實(shí)現(xiàn)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • iOS正確監(jiān)聽手機(jī)靜音鍵和側(cè)邊音量鍵的方法示例

    iOS正確監(jiān)聽手機(jī)靜音鍵和側(cè)邊音量鍵的方法示例

    這篇文章主要給大家介紹了關(guān)于iOS正確監(jiān)聽手機(jī)側(cè)邊音量鍵的相關(guān)資料,并且給大家分享了ios監(jiān)聽靜音鍵的示例代碼,文中介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-11-11
  • iOS13原生端適配攻略(推薦)

    iOS13原生端適配攻略(推薦)

    這篇文章主要介紹了iOS13原生端適配攻略(推薦),現(xiàn)匯總一下iOS 13的各種坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-10-10
  • iOS開發(fā)中實(shí)現(xiàn)hook消息機(jī)制的方法探究

    iOS開發(fā)中實(shí)現(xiàn)hook消息機(jī)制的方法探究

    這篇文章主要介紹了iOS開發(fā)中實(shí)現(xiàn)hook消息機(jī)制的方法探究,這里用到了一個(gè)Method Swizzling原理,需要的朋友可以參考下
    2015-10-10
  • 微信小程序 實(shí)現(xiàn)listview帶字母滑動(dòng)

    微信小程序 實(shí)現(xiàn)listview帶字母滑動(dòng)

    這篇文章主要介紹了微信小程序 實(shí)現(xiàn)listview帶字母滑動(dòng)的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 一篇文章讓你看懂IOS中的block為何再也不需要WeakSelf弱引用

    一篇文章讓你看懂IOS中的block為何再也不需要WeakSelf弱引用

    這篇文章主要給大家介紹了關(guān)于IOS中block為何再也不需要WeakSelf弱引用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • iOS 通用鏈接(Universal Link)配置詳解

    iOS 通用鏈接(Universal Link)配置詳解

    這篇文章主要介紹了iOS 通用鏈接(Universal Link)配置詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • iOS查找私有API的方法示例

    iOS查找私有API的方法示例

    這篇文章主要介紹了iOS查找私有API的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • IOS self和super詳解實(shí)現(xiàn)原理及區(qū)別

    IOS self和super詳解實(shí)現(xiàn)原理及區(qū)別

    這篇文章主要介紹了iOS self和super詳解實(shí)現(xiàn)原理及區(qū)別的相關(guān)資料,這里不僅說明區(qū)別并介紹實(shí)現(xiàn)原理,具有參考價(jià)值,需要的朋友可以參考下
    2016-12-12

最新評(píng)論