你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS實現文字水平無間斷滾動

IOS實現文字水平無間斷滾動

編輯:IOS開發綜合

IOS實現文字水平無間斷滾動

IOS跑馬燈效果,實現文字水平無間斷滾動,示例代碼如下:

 

ViewController.h

#import 

@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

 

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved