Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

iOS/iPhone Page Control (UIPageControl) and ScrollView (UIScrollView) (iPhone, iPad)

iOS/iPhone Page Control (UIPageControl) and ScrollView (UIScrollView) (iPhone, iPad) ถ้าเราได้ใช้ iPhone บ่อย ๆ จะมี Interface บางตัว เช่นหน้าแรกบนหน้าจอของ iPhone ถ้ามี App จำนวนมากกว่า 1 หน้า App Icon Launcher ต่าง ๆ ก็จะถูกกระจ่ายไปยังหน้า 2 หรือ หน้า 3 และเราสามารถเลื่อน Slide ระหว่างหน้าต่าง ๆ ผ่าน Gesture และในแต่ละหน้าต่าง ๆ เราจะเห็นมี Page Control(UIPageControl) ซึ่งเป็นจุดเล็ก ๆ แจ้งว่าตอนนี้อยู่หน้าที่เท่าไหร่แล้ว ลองเปิดดูในหน้าแรกของ iPhone หรือ iPad ก็จะเห็น ซึ่ง Page Control(UIPageControl) อาจจะพบเจอได้จากหลาย ๆ ส่วนของโปรแกรม

iOS/iPhone Page Control (UIPageControl)

iOS/iPhone Page Control (UIPageControl) and ScrollView (UIScrollView)


ใน Page Control (UIPageControl) จะใช้ร่วมกับ ScrollView (UIScrollView) ซึ่ง ScrollView จะเป็นตัวกำหนดจำนวนแสดงผลในแต่ล่ะหน้า และ ScrollView สามารถ delegate ทำงานรวมกับ Page Control ที่จะแสดงสถานะว่าตอนนี้อยู่ Page ที่เท่าไหร่แล้ว

iOS/iPhone Page Control (UIPageControl)

เริ่มต้นด้วยการสร้าง Project ด้วย Xcode แบบ Single View Application

iOS/iPhone Page Control (UIPageControl)

เลือกและไม่เลือกดังรูป แล้วเลือก Create

iOS/iPhone Page Control (UIPageControl)

หน้าแรกของ View ตอนนี้ยังเป็นหน้าจอเปล่า ๆ

iOS/iPhone Page Control (UIPageControl)

ลาก Object ที่มีชื่อว่า ScrollView มาไว้ในหน้า View โดยขนาดลอง ScrollView ให้เว้นไสำหรับ Page Control ด้วย








iOS/iPhone Page Control (UIPageControl)

ลาก Page Control มาไว้ในตำแหน่งล่างของ ScrollView ดังภาพ

iOS/iPhone Page Control (UIPageControl)

เปลี่ยนสีให้ Page Control เพื่อจะได้ดูง่าย ๆ

iOS/iPhone Page Control (UIPageControl)

ปรับแต่ง ScrollView โดยการเลือก Scrolling Enabled และ Paging Enabled

iOS/iPhone Page Control (UIPageControl)

สร้าง IBOutlet ให้กับ Scroll View และ Page Control

iOS/iPhone Page Control (UIPageControl)

คลิกขวาที่ Scroll View แล้วเชื่อม delegate กับ File's Owner

iOS/iPhone Page Control (UIPageControl)

คลิกที่ File's Owner และ Inspector ในส่วนของ Outlet

iOS/iPhone Page Control (UIPageControl)

Received Actions จะมี Event ชื่อ changePage ให้ลากเพื่อเชื่อมกับ Page Control (ดูภาพประกอบ)

iOS/iPhone Page Control (UIPageControl)

เลือก Event เป็นแบบ Value Changed

iOS/iPhone Page Control (UIPageControl)

ตอนนี้เราจะได้ Outlet ต่าง ๆ ที่เชื่อมกันดังนี้ จากนั้นจะเป็นส่วนของ Coding ของภาษา Objective-C








ViewController.h
//
//  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
//
//  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

คำอธิบาย
ในตัวอย่างนี้จะนำค่าข้อความที่อยู่ในรูปแบบของ Array เพื่อมาแสดงข้อมูลเป็นหน้า ๆ ด้วย Page Control และ Scroll View

Screenshot

iOS/iPhone Page Control (UIPageControl)

แสดงข้อมูลและ Page Control

iOS/iPhone Page Control (UIPageControl)

เมื่อ Slide ไปยังหน้าอื่น ๆ ด้วย Gesture ตำแหน่งของ Page Control ก็จะเลื่อนไปด้วย



.

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-11-07 10:37:20 / 2017-03-25 23:50:10
  Download : Download  iOS/iPhone Page Control (UIPageControl) and ScrollView (UIScrollView) (iPhone, iPad)
 Sponsored Links / Related

 
iOS/iPhone Collection View and Master Detail (Objective-C, iPhone, iPad)
Rating :

 
iOS/iPhone Hide Input Keyboard and Validate Text Field (Password, Number, URL, E-Mail, Phone Number)
Rating :

 
iOS/iPhone AD BannerView (iAd Framework)
Rating :

 
iOS/iPhone Table View Show Enable Edit / Delete Cell (Swipe To Delete) (UITableView)
Rating :

 
iOS/iPhone Search Data from Web Server (URL,Website)
Rating :

 
iOS/iPhone Edit Update Data on Web Server (URL,Website)
Rating :

 
iOS/iPhone Delete Remove Data on Web Server (URL,Website)
Rating :

 
iOS/iPhone Register Form and Send Data to Web Server (PHP & MySQL)
Rating :

 
iOS/iPhone Login Username and Password from Web Server (PHP & MySQL)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่