Text horizontal scrolling in IOS

Source: Internet
Author: User

Text horizontal scrolling in IOS
Text horizontal scrolling in IOS

The IOS marquee achieves uninterrupted horizontal text scrolling. The sample code is as follows:

 

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 80366f # define K_MAIN_VIEW_SCROLL_TEXT_TAG 300 # define K_MAIN_VIEW_TEME_INTERVAL 0.35 // timer interval (in seconds) # define K_MAIN_VIEW_SCROLLER_SPACE 20 // the distance between each movement # define K_MAIN_VIEW_SCROLLER_LABLE_WIDTH 280 // The font width # define Limit 20 // The distance between the frontend and backend @ interface ViewController () @ end @ implementation Ler # pragma mark-Class property @ synthesize arrData;-(void) viewDidLoad {[super viewDidLoad]; [self initView];}-(void) didreceivemorywarning {[super ready]; // Dispose of any resources that can be recreated .} # pragma mark-Custom method // initialize data-(void) initView {if (! Self. arrData) {self. arrData = @ [@ {@ newsId: @ 201507070942261935, @ newsImg: @ http://bg.fx678.com/HTMgr/upload/UpFiles/20150707/sy_2015070709395519.jpg, @ newsTitle: @ three reasons for euro resistance, Euro Zone summit will be set for Greece }, @ {@ newsId: @ 201507070929021220, @ newsImg: @ http://bg.fx678.com/HTMgr/upload/UpFiles/20150707/sy_2015070709273545.jpg, @ newsTitle: @ the EU Summit or the current turning point in Greece, gold 1162 Guard war}, @ {@ newsId: @ 201507070656471857, @ newsImg: @ http: // B Warning, @ newsTitle: @ the euro is not afraid of the Greek dilemma, and the oil price has plummeted by 8%}];} // text rolling [self initScrollText]; // enable rolling [self startScroll];} // initialize text scrolling-(void) initScrollText {// obtain the 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; // hide the horizontal scroll bar scrollViewText. showsVerticalScrollIndicator = NO; // hide the vertical scroll bar scrollViewText. tag = K_MAIN_VIEW_SCROLL_TEXT_TAG; [scrollViewText setBackgroundColor: [UIColor grayColor]; // clear the child Control for (UIView * view in [ScrollViewText subviews]) {[view removeFromSuperview];} // Add to current view [self. view addSubview: scrollViewText];} if (self. arrData) {CGFloat offsetX = 0, I = 0, h = 30; // set the scroll text UILabel * labText = nil; for (NSDictionary * dicTemp in self. arrData) {labText = [[UILabel alloc] initWithFrame: CGRectMake (I * (K_MAIN_VIEW_SCROLLER_LABLE_WIDTH + width), (K_MAIN_VIEW_SCROLL_HEIGH T-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; // Add to the scroll view [scrollViewText addSubview: labText]; I ++;} // set the scroll area size [scrollViewText setContentSize: CGSizeMake (offsetX, 0)];} // start rolling-(void) startScroll {if (! Timer) timer = [NSTimer scheduledTimerWithTimeInterval: K_MAIN_VIEW_TEME_INTERVAL target: self selector: @ selector (setScrollText) userInfo: nil repeats: YES]; [timer fire];} // scroll-(void) setScrollText {CGFloat startX = scrollViewText. contentSize. width-encryption-interval; [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

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.