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 Progress View (UIProgressView) Example (iPhone,iPad)

iOS/iPhone Progress View (UIProgressView) Example (iPhone,iPad) ในการเขียน iOS App เพื่อทำงานบน iPhone และ iPad สิ่งที่เราจะลืมไม่ได้เลยอย่างยิ่งก็คือ Progress View โดย Progress View จะนิยมใช้ในการแสดงสถานะการทำงาน เช่น การดาวน์โหลด (Download) ไฟล์ , การอัพโหลดไฟล์ (Upload) หรือการทำงานต่าง ๆ ที่สามารถแจ้งสถานะให้ผู้ใช้ทราบว่าตอนนี้ ทำถึงขั้นตอนไหนแล้ว โดยจะใช้แถบสีเป็นตัวแจ้งสถานะให้ทราบในแต่ระยะ สำหรับ Progress View จะใช้ร่วมกับ Class ของ UIProgressView ในภาษา Objective-C ในการเขียนเงื่อนไขการทำงานต่าง ๆ

iOS/iPhone Progress View (UIProgressView) Example

iOS/iPhone Progress View (UIProgressView) Example


Progress View (UIProgressView) เป็น Progress Bar ที่สามารถแจ้งสถานะการทำงานได้ตามสถานะการทำงานจริง เพราะฉะนั้นในการใช้งานจริงแล้ว Progress View จะต้องทำการทำงานสัมพันธ์กับสิ่งที่เกิดขึ้นจริง ซึ่งปกติแล้วการทำงานแบบ Progress View แสดงว่า Process นั้นทำงานนานซะพักหนึ่ง เพาะฉะนั้นจะต้องใช้ Thread ทำงานในรูปแบบของ Background Process มาเกี่ยวข้อง โดยขั้น Concept การทำงานนั้น เช่น เรามี ข้อมูลใน Loop ประมาณ 100 รายการที่จะต้องทำงาน เราอาจจะใช้ Thread มาครอบ Loop นั้น ๆ และเมื่อ Loop ทำงานแต่ล่ะครั้ง เราก็จะทำการแจ้งสถานะมายัง Progress View เพื่อให้ทำการ Update สถานะแจ้งให้ผู้ใช้ได้ทราบ สำหรับบทความนี้จะยังไม่ได้ไปถึงขั้นการใช้ Progress View กับ Thread ที่จะให้เห็นภาพและสถานะการทำงานจริง ๆ (สามารถอ่านได้จากหัวข้อ Thread และ Progress View) แต่จะเป็นพื้นฐานการรู้จักและการใช้งาน Progress View กับ UIProgressView แบบง่าย ๆ โดยใช้ Timer มาหน่วงเวลา และแจ้ง Update ตัว Status ไปยัง Progress View

การ Get ค่าของ Progress View (UIProgressView)
IBOutlet UIProgressView *progressview;

int float  = progressview.progress;


การ Set ค่าของ Progress View (UIProgressView)
IBOutlet UIProgressView *progressview;

[progressview setProgress:0.0];

โดย Value ของ Progress View จะเริ่มจาก 0.00 ถึง 1.00

ก่อนที่เราจะเขียน Progress View บน Xcode จะขออธิบายเกี่ยวกับ Progress View ว่าจะมีค่า Min อยู่ที่ 0.00 และ ค่า Max อยู่ที่ 1.00 เฉพาะฉะนั้นการ Update สถานะ จะใช้หน่วยตั้งแต่ 0.00 ถึง 1.00 (ถึงจะแสดงแถบสถานะเต็ม) เช่นเรา Loop ข้อมูล 1-100 เราสามารถใช้สูตร 1/100...2/100...100/100 เช่น

1/100 = 0.01
2/100 = 0.02
.
.
.
100/100 = 1.00


จะเห็นว่าเราสามารถแสดงสถานะจาก 0.00 ถึง 1.00 ได้พอดี








Example ตัวอย่างการใช้งาน Progress View (UIProgressView) ร่วมกับ Timer แบบง่าย ๆ

iOS/iPhone Progress View (UIProgressView) Example

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

iOS/iPhone Progress View (UIProgressView) Example

เลือกและไม่เลือกรายการดังรูป

iOS/iPhone Progress View (UIProgressView) Example

ตอนนี้หน้าจอ View ของเราจะยังว่าง ๆ

iOS/iPhone Progress View (UIProgressView) Example

ทำการลาก Object ที่มีชื่อว่า Progress View มาวางไว้ในในหน้าจอ Interface

iOS/iPhone Progress View (UIProgressView) Example

กำหนดค่า Progress ที่เป็น Default ให้เริ่มต้นจาก 0

iOS/iPhone Progress View (UIProgressView) Example

สร้าง Label เพื่อแจ้งสถานะเป็นตัวเลข และ Button สำหรับปุ่ม Start

iOS/iPhone Progress View (UIProgressView) Example

ใน Class ของ .h ทำการเชื่อม IBOutlet และ IBAction ดังรูป

iOS/iPhone Progress View (UIProgressView) Example

ใน Class ของ .m ตอนนี้ Event ของ Button (Start) ยังไม่มีอะไร

iOS/iPhone Progress View (UIProgressView) Example

ใส่คำสั่งดังนี้

- (IBAction)btnStart:(id)sender {
    [progress setProgress:0.0];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:@selector(setCustomProgress) userInfo:nil repeats:YES];
}

- (void) setCustomProgress
{
    progress.progress = progress.progress + 0.01;
    
    NSString *newValue = [[NSString alloc] initWithFormat:@"%.2f", progress.progress];
    lblResult.text = newValue;
    
    if(progress.progress == 1.0)
    {
        lblResult.text = @"Load Finished!";
        [timer invalidate];
    }
}









คำอธิบาย ในตัวอย่างจะใช้ Timer ในการหน่วงเวลา โดยใช้หน่วง 0.10 วินาที หลังจากนั้นจะ Update สถานะใน Method ของ setCustomProgress และใน Method ของ setCustomProgress จะทำการ Update สถานะของ Progress View โดยให้เพิ่มสถานะครั้งล่ะ 0.01 จนถึง 1.00 และเมื่อสถานะ ถึง 1.00 ก็จะหยุดการทำงาน

Screenshot

iOS/iPhone Progress View (UIProgressView) Example

กดปุ่ม Start เพื่อเริ่มการทำงาน โดยจะแสดงสถานะทั้ง Progress View และบน Label

iOS/iPhone Progress View (UIProgressView) Example

ถึง 1.00 ก็จะหยุดการทำงาน

เพิ่มเติม จากตัวอย่างจะเห็นว่าใน Label จะมีค่าเป็น 0.00 ถึง 1.00 แต่ถ้าเราต้องการเปลี่ยนเป็นตัวเลข 1 ถึง 100 เราสามารถเอา 100 ไปหารค่าเหล่านั้น ก็จะได้เป็นตัวเลข 1-100 ตามต้องการ

Code ทั้งหมดของ .h และ .m ในภาษา Objective-C

ViewController.h
//
//  ViewController.h
//  progressViewApp
//
//  Created by Weerachai on 11/10/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UIProgressView *progress;
    IBOutlet UILabel *lblResult;
    
     NSTimer *timer;
}

- (IBAction)btnStart:(id)sender;

- (void) setCustomProgress;

@end


ViewController.m
//
//  ViewController.m
//  progressViewApp
//
//  Created by Weerachai on 11/10/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnStart:(id)sender {
    [progress setProgress:0.0];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:@selector(setCustomProgress) userInfo:nil repeats:YES];
}

- (void) setCustomProgress
{
    progress.progress = progress.progress + 0.01;
    
    NSString *newValue = [[NSString alloc] initWithFormat:@"%.2f", progress.progress];
    lblResult.text = newValue;
    
    if(progress.progress == 1.0)
    {
        lblResult.text = @"Load Finished!";
        [timer invalidate];
    }
}

- (void)dealloc {
    [progress release];
    [lblResult release];
    [super dealloc];
}

@end



.

   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-11-13 16:44:24 / 2017-03-26 00:03:27
  Download : Download  iOS/iPhone Progress View (UIProgressView) Example (iPhone,iPad)
 Sponsored Links / Related

 
iOS/iPhone Activity Indicator View and AlertView (UIAlertView / UIActivityIndicatorView)
Rating :

 
iOS/iPhone Activity Indicator View (ActivityIndicator,UIActivityIndicatorView) Example (iPhone,iPad)
Rating :

 
iOS/iPhone Image View and NSURLConnection ActivityIndicator Progress
Rating :

 
iOS/iPhone NSURLConnection Show Progress and Activity Indicator View (UIActivityIndicatorView)
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 อัตราราคา คลิกที่นี่