你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS可旋轉的餅圖

iOS可旋轉的餅圖

編輯:IOS開發綜合

效果圖:

\


代碼下載:http://pan.baidu.com喎?/kf/ware/vc/" target="_blank" class="keylink">vcy8xbzZCR3owbSAgIGh0dHA6Ly9kb3dubG9hZC5jc2RuLm5ldC9kZXRhaWwvcXFtY3kvNzA5MDg2NTwvcD4KPHA+sf3NvLXEtdrI/be9wODU2sD919PW0MrH09C1xKOsyrnTw8bwwLTSsrrcvPK1paOsz8LD5srHyrnTw7XEtPrC66GjPC9wPgo8cD48YnI+CjwvcD4KPHA+Vmlld0NvbnRyb2xsZXIuaDxicj4KPC9wPgo8cD48L3A+CjxwcmUgY2xhc3M9"brush:java;">#import #import "PieChartView.h" @interface ViewController : UIViewController @property (nonatomic,strong) NSMutableArray *m_valueArray; //第一組數據的容器 @property (nonatomic,strong) NSMutableArray *m_colorArray; //第一組顏色的容器 @property (nonatomic,strong) NSMutableArray *m_valueArray2;//第二組數據的容器 @property (nonatomic,strong) NSMutableArray *m_colorArray2;//第二組顏色的容器 @property (nonatomic,strong) PieChartView *m_pieChartView; //餅圖類 @property (nonatomic,strong) UIView *m_pieContainer; //餅圖的容器 @property (nonatomic)BOOL inOut; //點擊中心的控制開關 @property (nonatomic,strong) UILabel *m_selLabel; //顯示數據的標簽 @end
ViewController.m

//
//  ViewController.m
//  可旋轉的餅圖
//
//  Created by 杜甲 on 14-3-24.
//  Copyright (c) 2014年 杜甲. All rights reserved.
//

#import "ViewController.h"

#define PIE_HEIGHT 280
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    
    self.inOut = YES;
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.m_valueArray = [[NSMutableArray alloc] initWithObjects:
                       [NSNumber numberWithInt:12],
                       [NSNumber numberWithInt:3],
                       [NSNumber numberWithInt:2],
                       [NSNumber numberWithInt:3],
                       [NSNumber numberWithInt:3],
                       [NSNumber numberWithInt:4],
                       nil];
    self.m_valueArray2 = [[NSMutableArray alloc] initWithObjects:
                        [NSNumber numberWithInt:3],
                        [NSNumber numberWithInt:2],
                        [NSNumber numberWithInt:2],
                        nil];
    
    
    self.m_colorArray = [NSMutableArray arrayWithObjects:
                       [UIColor colorWithHue:((0/8)%20)/20.0+0.08 saturation:(0%8+3)/10.0 brightness:91/100.0 alpha:1],
                       [UIColor colorWithHue:((1/8)%20)/20.0+0.03 saturation:(1%8+3)/10.0 brightness:91/100.0 alpha:1],
                       [UIColor colorWithHue:((2/8)%20)/20.0+0.12 saturation:(2%8+3)/10.0 brightness:91/100.0 alpha:1],
                       [UIColor colorWithHue:((3/8)%20)/20.0+0.32 saturation:(3%8+3)/10.0 brightness:91/100.0 alpha:1],
                       [UIColor colorWithHue:((4/8)%20)/20.0+0.01 saturation:(4%8+3)/10.0 brightness:91/100.0 alpha:1],
                       [UIColor colorWithHue:((5/8)%20)/20.0+0.05 saturation:(5%8+3)/10.0 brightness:91/100.0 alpha:1],
                       nil];
    self.m_colorArray2 = [[NSMutableArray alloc] initWithObjects:
                        [UIColor purpleColor],
                        [UIColor orangeColor],
                        [UIColor magentaColor],
                        nil];
    
    
    //add shadow img
    CGRect pieFrame = CGRectMake((self.view.frame.size.width - PIE_HEIGHT) / 2, 0, PIE_HEIGHT, PIE_HEIGHT);
    
    UIImage *shadowImg = [UIImage imageNamed:@"shadow.png"];
    UIImageView *shadowImgView = [[UIImageView alloc]initWithImage:shadowImg];
    shadowImgView.frame = CGRectMake(0, pieFrame.origin.y + PIE_HEIGHT*0.92, shadowImg.size.width/2, shadowImg.size.height/2);
    [self.view addSubview:shadowImgView];
    
    
    self.m_pieContainer = [[UIView alloc]initWithFrame:pieFrame];
    self.m_pieChartView = [[PieChartView alloc]initWithFrame:self.m_pieContainer.bounds withValue:self.m_valueArray withColor:self.m_colorArray];
    self.m_pieChartView.delegate = self;
    [self.m_pieContainer addSubview:self.m_pieChartView];
    [self.m_pieChartView setAmountText:@"-2456.0"];
    [self.view addSubview:self.m_pieContainer];
    
    
    
    //add selected view
    UIImageView *selView = [[UIImageView alloc]init];
    selView.image = [UIImage imageNamed:@"select.png"];
    selView.frame = CGRectMake((self.view.frame.size.width - selView.image.size.width/2)/2, self.m_pieContainer.frame.origin.y + self.m_pieContainer.frame.size.height, selView.image.size.width/2, selView.image.size.height/2);
    [self.view addSubview:selView];
    
    self.m_selLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 24, selView.image.size.width/2, 21)];
    self.m_selLabel.backgroundColor = [UIColor clearColor];
    self.m_selLabel.textAlignment = NSTextAlignmentCenter;
    self.m_selLabel.font = [UIFont systemFontOfSize:17];
    self.m_selLabel.textColor = [UIColor whiteColor];
    [selView addSubview:self.m_selLabel];
    [self.m_pieChartView setTitleText:@"支出總計"];
    self.title = @"對賬單";

    
    
}

- (void)selectedFinish:(PieChartView *)pieChartView index:(NSInteger)index percent:(float)per
{
     self.m_selLabel.text = [NSString stringWithFormat:@"%2.2f%@",per*100,@"%"];
}

- (void)onCenterClick:(PieChartView *)pieChartView
{
    self.inOut = !self.inOut;
    self.m_pieChartView.delegate = nil;
    [self.m_pieChartView removeFromSuperview];
    self.m_pieChartView = [[PieChartView alloc]initWithFrame:self.m_pieContainer.bounds withValue:self.inOut?self.m_valueArray:self.m_valueArray2 withColor:self.inOut?self.m_colorArray:self.m_colorArray2];
    self.m_pieChartView.delegate = self;
    [self.m_pieContainer addSubview:self.m_pieChartView];
    [self.m_pieChartView reloadChart];
    
    if (self.inOut) {
        [self.m_pieChartView setTitleText:@"支出總計"];
        [self.m_pieChartView setAmountText:@"-2456.0"];
        
    }else{
        [self.m_pieChartView setTitleText:@"收入總計"];
        [self.m_pieChartView setAmountText:@"+567.23"];
    }

}

- (void)viewDidAppear:(BOOL)animated
{
    [self.m_pieChartView reloadChart];
}


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

@end


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved