IOS-----Uiscrollview

Source: Internet
Author: User

In doing the project is sometimes encountered setting automatic scrolling picture, Uiscrollview is a very interesting thing, so do the following summary

DEMO:

. h

#import <UIKit/UIKit.h> @interface imageplayview:uiview<uiscrollviewdelegate> @property (nonatomic, Strong) Uiscrollview * scroll; @property (nonatomic, Strong) Uipagecontrol * play; @property (Nonatomic, strong) Nstimer * t Imer; @end

. m

#import "ImagePlayView.h"
//Define the total number of pictures
#define IMAGECOUNT 3; @implementation imageplayview-(ID) initWithFrame: (CGRect) frame{if (self = [Super Initwithframe:f Rame]) {//Create a Uiscrollview component as the head view of the scroll 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 Paging_scroll.pagingenabled = YES; [self addsubview: _scroll]; for (int i = 0; i < imagecount; i + +) {Uiimageview * Imagev = [[Uiimageview alloc] Initwithframe:cg Rectmake (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 Paging indicator_play = [[Uipagecontrol alloc] Initwithframe:cgrectmake (0, 0, SELF.FRAME.SIZE.WIDTH/3, 20)];//Set the location of the paging device_play.center = Cgpointmake (SELF.FRAME.SIZE.WIDTH/2, self.frame.size.height/1.2);//Set the color of the other page indicator dots_play.pageindicatortintcolor = [Uicolor Graycolor];//Set the color of the current page indicator dot_play.currentpageindicatortintcolor = [Uicolor Graycolor];//Set the number of pages for the page splitter_play.numberofpages = Imagecount;//Set Current page_play.currentpageindicatortintcolor = 0; [Self addsubview:_play];//Set Timer_timer = [Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (time) Userinfo:nil Repeats:yes];//Gets the current message loop, sets the priority of the timer object with the message loop and controls the same priorityNsrunloop *runloop = [Nsrunloop currentrunloop]; [Runloop AddTimer:self.timer formode:nsrunloopcommonmodes]; } return self;} -(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];//When 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{//Get the current number of pages based on the offset position of the contentint page = (scrollview.contentoffset.x+scrollview.frame.size.width*0.5)/scrollView.frame.size.width;//To tell 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.0 target:self selector: @selector (Time) Userinfo:nil repeats: YES];//Solve the second bug: When you drag a button on another 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{//Get the current number of pages
0,1,2
Nsinteger 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 by code to enable auto-scrolling//non-animated modeSelf.scrollView.contentOffset = Cgpointmake (page*self.scrollview.frame.size.width, 0);//By animated mode[Self.scroll setcontentoffset:cgpointmake (page*self.scroll.frame.size.width, 0) animated:yes];} @end

IOS-----Uiscrollview

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.