怎么在iOS中實現(xiàn)一個文字水平無間斷滾動效果-創(chuàng)新互聯(lián)

本篇文章為大家展示了怎么在iOS中實現(xiàn)一個文字水平無間斷滾動效果,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)公司主要從事網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)宿遷,十載網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220

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  //單個字符寬度(與你設(shè)置的字體大小一致)
#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" :@"https://cache.yisu.com/upload/information/20200623/126/118344.jpg",
               @"newsTitle":@"三大理由歐元任性抗跌,歐元區(qū)峰會將為希臘定調(diào)"
             },
             @{
               @"newsId"  :@"201507070929021220",
               @"newsImg"  :@"https://cache.yisu.com/upload/information/20200623/126/118345.jpg",
               @"newsTitle" :@"歐盟峰會或現(xiàn)希臘轉(zhuǎn)機,黃金打響1162保衛(wèi)戰(zhàn)"
             },
             @{
               @"newsId"  :@"201507070656471857",
               @"newsImg"  :@"https://cache.yisu.com/upload/information/20200623/126/118347.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;          //禁用手動滑動
 
    //橫豎屏自適應(yīng)
    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];
 
    //添加到當(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è)置滾動文字
    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)點擊事件
      btnText.userInteractionEnabled = NO;
 
      //添加到滾動視圖
      [scrollViewText addSubview:btnText];
 
      i++;
    }
 
    //設(shè)置滾動區(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

上述內(nèi)容就是怎么在iOS中實現(xiàn)一個文字水平無間斷滾動效果,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)頁題目:怎么在iOS中實現(xiàn)一個文字水平無間斷滾動效果-創(chuàng)新互聯(lián)
URL分享:http://muchs.cn/article28/cocdcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、Google、標(biāo)簽優(yōu)化、網(wǎng)頁設(shè)計公司、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)