iOS enables text level scrolling without interruption
iOS marquee effect, achieve text level without intermittent scrolling, the sample code is as follows:
ViewController.h
#import <UIKit/UIKit.h> @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//Timer interval (in seconds) #define K_MAIN_VIEW_SCROLLER_SPACE 20 Distance per move # define K_main_view_scroller_lable_width 280//font width # k_main_view_scroller_lable_margin 20//ago After the interval distance @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 the can be recreated.} #pragma mark-custom method//initialization 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 the euro is wayward, the eurozone summit will set the tone for Greece"}, @{ @ "NewsId": @ "201507070929021220" @ "newsimg": @ "Http://bg.fx678.com/HTMgr/up Load/upfiles/20150707/sy_2015070709273545.jpg ", @" Newstitle ": @" EU summit or now Greece transfer, gold 1162 battle " }, @{@ "newsId": @ "201507070656471857", @ "newsimg": @ "Http://bg.fx678.com/HTMgr/upload/UpFiles/20150707/2015070706533134.jpg", @ "Newstitle": @ "Greek trap euro not afraid, oil price Furuan plunge 8%"}]; }//text scrolling [self initscrolltext]; Turn on scroll [self startscroll];} Text scrolling initialization-(void) initscrolltext{//Get scroll bar 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 horizontal scroll bar scrollviewtext.showsverticalscrollindicator = NO; Hide vertical scroll bar Scrollviewtext.tag = K_main_view_scroll_text_tag; [Scrollviewtext Setbackgroundcolor:[uicolor Graycolor]; Clear child controls 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 scrolling text 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_marg In), (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; Add to Scrolling view [Scrollviewtext Addsubview:labtext]; i++; }//Set the scroll area size [Scrollviewtext setcontentsize:cgsizemake (OffsetX, 0)]; }}//Start scrolling-(void) startscroll{if (!timer) timer = [Nstimer scheduledtimerwithtimeinterval:k_main_view_teme_interv AL target:self selector: @selector (setscrolltext) Userinfo:nil Repeats:yes]; [Timer fire];} Scrolling processing-(void) setscrolltext{cgfloat StartX = scrollviewtext.contentsize.width-k_main_view_scroller_lable_width-k_m Ain_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
Sample source Download:
http://yun.baidu.com/share/link?shareid=3134999&uk=3104876438
Note: The development tool Xcode version 6.4, if not directly run, you can create a new project to copy the above files to replace
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS enables text level scrolling without interruption