`
re_reference
  • 浏览: 236683 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

custom progress bar for iphone&ipad(转)

阅读更多
Posted by NaveenShan on Wednesday, November 3, 2010 Under: iPhone/iPad
/**************************************************************************************
/*  File Name      : ProgressBar.h
/*  Project Name   : <nil> Generic
/*  Description    : A Custom Progress Bar View
/*  Version        : 1.0
/*  Created by     : Naveen Shan
/*  Created on     : 13/10/10
/*  Copyright (C) 2010 RapidValue IT Services Pvt. Ltd. All Rights Reserved.
/**************************************************************************************/

#import <UIKit/UIKit.h>

@interface ProgressBar : UIView {
    float minValue, maxValue;
    float currentValue;
    UIColor *lineColor, *progressRemainingColor, *progressColor, *textColor;
}

@property (readwrite) float minValue, maxValue, currentValue;
@property (nonatomic, retain) UIColor *lineColor, *progressRemainingColor, *progressColor, *textColor;

-(void)setNewRect:(CGRect)newFrame;

@end


/**************************************************************************************
/*  File Name      : ProgressBar.m
/*  Project Name   : <nil> Generic
/*  Description    : N/A
/*  Version        : 1.0
/*  Created by     : Naveen Shan
/*  Created on     : 13/10/10
/*  Copyright (C) 2010 RapidValue IT Services Pvt. Ltd. All Rights Reserved.
/**************************************************************************************/

#import "ProgressBar.h"

@implementation ProgressBar

#pragma mark -

@synthesize  minValue, maxValue, currentValue;
@synthesize lineColor, progressRemainingColor, progressColor, textColor;

#pragma mark -

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        minValue = 0;
        maxValue = 1;
        currentValue = 0;
        self.backgroundColor = [UIColor clearColor];
        lineColor = [[UIColor whiteColor] retain];
        textColor = [[UIColor magentaColor] retain];
        progressColor = [[UIColor darkGrayColor] retain];
        progressRemainingColor = [[UIColor lightGrayColor] retain];
    }
    return self;
}

#pragma mark -

- (void)drawRect:(CGRect)rect {
   
    CGContextRef context = UIGraphicsGetCurrentContext();
   
    CGContextSetLineWidth(context, 3);
    CGContextSetStrokeColorWithColor(context,[lineColor CGColor]);
   
    //upper half
    CGContextSetFillColorWithColor(context, [[progressRemainingColor colorWithAlphaComponent:.7] CGColor]);
    CGContextAddRect(context, CGRectMake(2, 2, rect.size.width-4, ((rect.size.height/2)-2)));
    CGContextFillPath(context);
   
    //lower half
    CGContextSetFillColorWithColor(context, [progressRemainingColor CGColor]);
    CGContextAddRect(context, CGRectMake(2, rect.size.height/2-2, rect.size.width-4, ((rect.size.height/2)-2)));
    CGContextFillPath(context);
   
    //border
    CGContextAddRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height-2));
    CGContextStrokePath(context);
   
    //to plot progress
    float amount = (currentValue/(maxValue - minValue)) * (rect.size.width-5);
    CGContextSetFillColorWithColor(context, [progressColor CGColor]);
    CGContextAddRect(context, CGRectMake(2,2, amount, rect.size.height-5));
    CGContextFillPath(context);
   
    //to draw percentage text
    CGContextSetFillColorWithColor(context, [textColor CGColor]);
    CGContextSelectFont(context, "Helvetica", rect.size.height/2, kCGEncodingMacRoman);
    CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));
    CGContextSetTextDrawingMode(context, kCGTextFill);
    char str[20];
    sprintf(str, "%d %s" ,(int)currentValue,"%");
    CGContextShowTextAtPoint(context,((rect.size.width/2)-10),rect.size.height/2,str,strlen(str));
}

-(void)setNewRect:(CGRect)newFrame {
    self.frame = newFrame;
    [self setNeedsDisplay];
}

#pragma mark -

-(void)setMinValue:(float)newMin    {
    minValue = newMin;
    [self setNeedsDisplay];
}

-(void)setMaxValue:(float)newMax    {
    maxValue = newMax;
    [self setNeedsDisplay];
}

-(void)setCurrentValue:(float)newValue    {
   
    if(newValue < minValue)
        currentValue = minValue;
    else if(newValue > maxValue)
        currentValue = maxValue;
    else
        currentValue = newValue;
    [self setNeedsDisplay];
}

#pragma mark -

-(void)setLineColor:(UIColor *)newColor    {
    [newColor retain];
    [lineColor release];
    lineColor = newColor;
    [self setNeedsDisplay];
}

-(void)setTextColor:(UIColor *)newColor    {
    [newColor retain];
    [textColor release];
    textColor = newColor;
    [self setNeedsDisplay];
}

-(void)setProgressColor:(UIColor *)newColor    {
    [newColor retain];
    [progressColor release];
    progressColor = newColor;
    [self setNeedsDisplay];
}

-(void)setProgressRemainingColor:(UIColor *)newColor    {
    [newColor retain];
    [progressRemainingColor release];
    progressRemainingColor = newColor;
    [self setNeedsDisplay];
}

#pragma mark -

- (void)dealloc {
    [lineColor release];
    [textColor release];
    [progressColor release];
    [progressRemainingColor release];
    [super dealloc];
}

Call for Custom Progress Bar.
First, Implement the above code as new File
And Call like following,
    ProgressBar *objProgressBar= [[ProgressBar alloc] initWithFrame:CGRectMake(100,100,600,75)];
    objProgressBar.maxValue=100.0;
    objProgressBar.minValue=0.0;
    objProgressBar.lineColor=[UIColor redColor];
    objProgressBar.textColor=[UIColor magentaColor];
    objProgressBar.progressColor=[UIColor greenColor];
    objProgressBar.progressRemainingColor=[UIColor blueColor];
    objProgressBar.currentValue=10.0;
    [self.view addSubview:objProgressBar];
   
    [objProgressBar release];

From:http://www.naveenshan.yolasite.com/blog/custom-slider-for-iphone-ipad
分享到:
评论

相关推荐

    (0102)-iOS/iPhone/iPAD/iPod源代码-选项卡(Tab Bar)-Custom TabBarController

    本资源"(0102)-iOS/iPhone/iPAD/iPod源代码-选项卡(Tab Bar)-Custom TabBarController"主要探讨了如何自定义Tab Bar,使其符合应用程序的个性化需求。下面将详细介绍自定义Tab Bar的相关知识点。 1. **...

    Trello custom progress bar-crx插件

    【Trello custom progress bar-crx插件】是一个针对Trello平台的第三方扩展程序,它允许用户自定义Trello卡片上的进度条样式,从而提供更加个性化和视觉吸引力的项目管理体验。这款插件专为那些寻求超越Trello默认...

    Custom Progress Bar for YouTube:trade_mark:-crx插件

    将Youtube:trade_mark:进度条更改为来自有趣收藏的自定义。让你的Youtube:trade_mark:播放器真棒! 让Youtube播放器的进度条搞笑又酷! 从大型集合中为进度条选择一个主题,它将适用于YouTube上的所有视频。...

    (0138)-iOS/iPhone/iPAD/iPod源代码-按钮(Button)-Round Progress Button

    本文将深入探讨如何在iPhone、iPad或iPod Touch应用中创建一个具有圆形设计并显示动态进度条的按钮,即"Round Progress Button"。这个特性常见于音乐播放应用,如iTunes,它在用户预览歌曲时展示当前的播放进度。 ...

    IOS应用源码——效果很炫的progress bar.rar

    在iOS开发中,Progress Bar是一种常见的UI元素,用于显示任务的进度或加载状态。这个"效果很炫的progress bar"源码提供了一种创新且吸引用户的进度条实现方式,能够提升应用的用户体验。下面我们将深入探讨这个源码...

    ComponentOne Studio for iPhone 2013 v1

    CoverFlow for iPhone now supports devices with a custom screen size (iPad / PC version of the Safari browser). What's New in Prior Releases? Take a look at Studio for iPhone's release history to ...

    安卓Android多阶段进度条progress bar附带动画效果

    在`res/layout`目录下,创建一个名为`custom_progress_bar.xml`的文件,并添加以下代码: ```xml xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/custom_progress_bar" style=...

    Android--Progress超牛的使用....zip

    ProgressBar progressBar = findViewById(R.id.custom_progress_bar); progressBar.setProgress(50); // 设置当前进度为50% ``` 此外,`android-square-progressbar`还支持多种动画效果,比如平滑的渐进动画,可以...

    Android代码-Custom Navigation Bar

    This repo contains translations of the app Custom Navigation Bar on Google Play. Feel free contact me at paphonb@gmail.com if you want to help translating the app to your language.

    OfficeCustomUIEditorFiles_CustomUI_MicrosoftOffice_

    【标题】"OfficeCustomUIEditorFiles_CustomUI_MicrosoftOffice_" 提到的是一个与 Microsoft Office 自定义用户界面(Custom UI)编辑器相关的文件集合。这个工具主要用于帮助用户自定义 Office 应用程序,如 Word、...

    dataGrid 创建带有进度条的列

    After hours of “googling” I found some solutions, how to show progress bar in DataGridView object so I decided to create my own custom progress bar column. Articles I found, served as a guide for ...

    react-progress-bar:简单的React进度栏组件

    @ ramonak / react-progress-bar 进度栏React组件(使用Typescript构建) 安装 npm install --save @ramonak/react-progress-bar 用法 import React from "react" ; import ProgressBar from "@ramonak/react-...

    office custom UI editor(office2007自定义选项卡制作工具)

    Office Custom UI Editor是一款专为Microsoft Office 2007设计的工具,旨在帮助用户自定义其界面,特别是针对2007版本中的 Ribbon 用户界面。Ribbon 是Office 2007引入的一项重大改变,它将传统的菜单和工具栏转换为...

    LabVIEW Tools for iphone, Andriod

    LabVIEW Tools is new for iphone and andriod. The Data Dashboard for LabVIEW app lets you create a custom dashboard that can remotely control and monitor running NI LabVIEW applications. This is done ...

    Custom UI Editor

    CustomUIEditor 是一种工具,用于创建和编辑 Microsoft Office 的自定义用户界面。通过 CustomUIEditor,用户可以自定义 Office 应用程序的功能区、菜单和工具栏,以满足特定的需求。这样可以提高用户的工作效率,使...

    Stylish-Custom-themes-for-any-website_v1.8.3.rar

    标题 "Stylish-Custom-themes-for-any-website_v1.8.3.rar" 提供了一个关于自定义网站主题的扩展程序,版本为1.8.3。这个扩展程序允许用户为他们访问的任何网站定制样式,从而改变浏览器的视觉体验。其中,“Stylish...

    几种常用Android Progress效果

    4. **自定义进度条(Custom Progress Bar)** 自定义进度条允许开发者创建独特的加载效果。这通常涉及创建自定义的布局和动画。例如,`customloading`这个文件可能包含了一个自定义加载效果的实现。开发者可以使用`...

Global site tag (gtag.js) - Google Analytics