Automatic scrolling picture of iOS Development UI Chapter

Source: Internet
Author: User

We do project sometimes encounter set auto scrolling picture, so I have encountered two times myself. Think he's a pretty interesting thing so did the next summary

DEMO:

. h

#import <UIKit/UIKit.h>@interface imageplayview:uiview<uiscrollviewdelegate>  * * * timer;  @end

. m

#import "ImagePlayView.h"@implementationImageplayview- (ID) initWithFrame: (cgrect) frame{if(self =[Super Initwithframe:frame]) {        //Create a Uiscrollview component as the head view of the scrolling componentCGRect Headscroll = CGRectMake (0,0, Self.frame.size.width, self.frame.size.height); _scroll=[[Uiscrollview alloc] initwithframe:headscroll]; //Cancel horizontal scroll bar_scroll.showshorizontalscrollindicator =NO; //Cancel Flex Settings_scroll.bounces =NO; //Set Page Paging_scroll.pagingenabled =YES;        [self addsubview: _scroll];  for(inti =0; I <3; i + +) {Uiimageview* Imagev = [[Uiimageview alloc] Initwithframe:cgrectmake (i * self.frame.size.width,0, Self.frame.size.width, Self.frame.size.height)]; Imagev.image= [UIImage imagenamed:[nsstring stringWithFormat:@"banner-%d", i +1]];        [_scroll Addsubview:imagev]; } _scroll.contentsize= Cgsizemake (3* Self.frame.size.width,0); _scroll.Delegate=Self ; //set up a paging indicator_play = [[Uipagecontrol alloc] Initwithframe:cgrectmake (0,0, self.frame.size.width/3, -)]; //set the location of the page splitter_play.center = Cgpointmake (self.frame.size.width/2, self.frame.size.height/1.2); //set the color of other page indicator dots_play.pageindicatortintcolor =[Uicolor Graycolor]; //sets the color of the current page indicator dot_play.currentpageindicatortintcolor =[Uicolor Graycolor]; //set the number of pages for a page splitter_play.numberofpages =3; //set the current page_play.currentpageindicatortintcolor =0;                [Self addsubview:_play]; //Setting Timers_timer = [Nstimer scheduledtimerwithtimeinterval:1.0target:self selector: @selector (time) Userinfo:nil Repeats:yes]; //gets the current message loop first, using the message loop to set the priority of the timer object and the control's priority levelNsrunloop *runloop =[Nsrunloop Currentrunloop];    [Runloop AddTimer:self.timer formode:nsrunloopcommonmodes]; }    returnSelf ;}-(void) Scrollviewwillbegindragging: (Uiscrollview *) scrollview{//solve the first bug: Press and hold the scrolling content to drag or drop, and once you let go, you will scroll many times. Workaround when you start dragging the seasonal timer expires. Create a new timer when dragging ends[Self.timer invalidate]; //Once the timer expires, it can no longer be used. So assign it a null pointerSelf.timer =Nil;}//This method is executed during the drag process-(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{//gets the current number of pages based on the offset position of the content    intpage = (scrollview.contentoffset.x+scrollview.frame.size.width*0.5)/ScrollView.frame.size.width; //informs the paging controller component of the current page number during dragSelf.play.currentPage =page;}//This method is executed after the drag is completed-(void) Scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate{//when the drag is finished and a new timer is created, the first bug is removedSelf.timer = [Nstimer scheduledtimerwithtimeinterval:1.0target:self selector: @selector (time) Userinfo:nil Repeats:yes]; //Fix the second bug: When you drag a button on a different control. Scrolling is stopped because of single-threaded execution, and the priority of the timer object and the network object is less than the priority of the control, so when you drag the control, the action of the Nstimer timer object is no longer executed. The workaround is as follows: Whenever a new Nstimer timer is created, the current message loop is first fetched, and the priority of the control is set in the message loop, and the program takes the time slice to the object of the same priority;Nsrunloop *runroop = [Nsrunloop currentrunloop];//gets the current message loop//use message loops to set the priority for the Timer object and control the same priority[Runroop AddTimer:self.timer formode:nsrunloopcommonmodes]; }- (void) time{//gets the current number of pagesNsinteger page =Self.play.currentPage; if(page = = self.play.numberofpages-1) {page=0; }Else{page++; }        //calculates the size of the content offset contentoffset based on the current number of pages//set Contentoffset offset via code to enable automatic scrolling//non-animated mode//Self.scrollView.contentOffset = cgpointmake (page*self.scrollview.frame.size.width, 0); //by animating the way[Self.scroll Setcontentoffset:cgpointmake (Page*self.scroll.frame.size.width,0) Animated:yes];}@end

Automatic scrolling picture of iOS Development UI Chapter

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.