// // ViewController.h // // Created by Weerachai on 11/4/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController { BOOL pageControlBeingUsed; } @property (retain, nonatomic) IBOutlet UIScrollView *scrollView; @property (retain, nonatomic) IBOutlet UIPageControl *pageControl; - (IBAction)changePage; @end
// // ViewController.m // // Created by Weerachai on 11/4/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import "ViewController.h" @interface ViewController () { NSMutableArray *myObject; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. myObject = [[NSMutableArray alloc] init]; [myObject addObject:@"ThaiCreate.Com 1"]; [myObject addObject:@"ThaiCreate.Com 2"]; [myObject addObject:@"ThaiCreate.Com 3"]; [myObject addObject:@"ThaiCreate.Com 4"]; [myObject addObject:@"ThaiCreate.Com 5"]; [myObject addObject:@"ThaiCreate.Com 6"]; [myObject addObject:@"ThaiCreate.Com 7"]; [myObject addObject:@"ThaiCreate.Com 8"]; [myObject addObject:@"ThaiCreate.Com 9"]; pageControlBeingUsed = NO; CGRect screenRect = [[UIScreen mainScreen] bounds]; CGFloat screenWidth = screenRect.size.width; CGFloat screenHight = screenRect.size.height; for (int i = 0; i < [myObject count]; i++) { CGRect frame; frame.origin.x = self.scrollView.frame.size.width * i; frame.origin.y = 0; frame.size = self.scrollView.frame.size; NSString *val = [myObject objectAtIndex:i]; UILabel *txt=[[UILabel alloc] initWithFrame:CGRectMake(screenWidth*i,0,screenWidth ,screenHight)]; txt.text = val; txt.textColor = [UIColor blackColor]; txt.textAlignment = NSTextAlignmentCenter; [self.scrollView addSubview:txt]; [txt release]; } self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * [myObject count], self.scrollView.frame.size.height-100); self.pageControl.currentPage = 0; self.pageControl.numberOfPages = [myObject count]; } - (void)scrollViewDidScroll:(UIScrollView *)sender { if (!pageControlBeingUsed) { // Switch the indicator when more than 50% of the previous/next page is visible CGFloat pageWidth = self.scrollView.frame.size.width; int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1; self.pageControl.currentPage = page; } } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { pageControlBeingUsed = NO; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { pageControlBeingUsed = NO; } - (IBAction)changePage { // Update the scroll view to the appropriate page CGRect frame; frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage; frame.origin.y = 0; frame.size = self.scrollView.frame.size; [self.scrollView scrollRectToVisible:frame animated:YES]; pageControlBeingUsed = YES; } - (void)viewDidUnload { self.scrollView = nil; self.pageControl = nil; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [_scrollView release]; [_pageControl release]; [super dealloc]; } @end
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท