Saturday, April 6, 2013

Create ellipse shape and set color by delegate

Here's a way to create ellipse shape and use delegate to set what color the ellipse shape is.

In my app "Money42",it's has a switch button to set who pay the money.

When the switch button init,it will use delegate to set color itself.

The delegate method is only do one thing,that's return a color NSArray from NSUserDefaults which we set before.

Step 1:Add New Class

File --> New --> File

Choose Objective-C Class

Set a class name "EllipseShape"(subclass of UIView)

Step 2:Edit EllipseShape.h
#import <UIKit/UIKit.h>

@protocol EllipseColorDelegate <NSObject>

@interface EllipseShape : UIView

Step 3:Edit EllipseShape.m
#import "EllipseShape.h"

@implementation EllipseShape

- (id)initWithFrame:(CGRect)frame
    self = [super initWithFrame:frame];
    if (self) {
        self.opaque = NO;
    return self;

- (void)drawRect:(CGRect)rect
    NSArray *arrayOfColors = [self.delegate giveEllipseColors];
    int red = [[arrayOfColors objectAtIndex:0] intValue];
    int green = [[arrayOfColors objectAtIndex:1] intValue];
    int blue = [[arrayOfColors objectAtIndex:2] intValue];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect);
    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0].CGColor);
    CGRect rectangle = CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
    CGContextAddEllipseInRect(context, rectangle);


Step 4:Set Default Color in AppDelegate.m
Add in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    NSString *dateKey    = @"dateKey";
    NSDate *lastRead    = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:dateKey];
    if (lastRead == nil)
    NSDictionary *appDefaults  = [NSDictionary dictionaryWithObjectsAndKeys:[NSDate date], dateKey, nil];
    [[NSUserDefaults standardUserDefaults] setObject:@[[NSNumber numberWithDouble:0], [NSNumber numberWithDouble:210], [NSNumber numberWithDouble:223]] forKey:@"currentColor"];
    [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:dateKey];

Step 5:Set delegate in ViewController.h
#import <UIKit/UIKit.h>
#import "EllipseShape.h"
@interface ViewController : UIViewController<EllipseColorDelegate>

Step 5:implement in ViewController.m
#import "ViewController.h"

@interface ViewController ()
@property (nonatomic)double redQuantity;
@property (nonatomic)double greenQuantity;
@property (nonatomic)double blueQuantity;

@implementation ViewController
@synthesize redQuantity;
@synthesize blueQuantity;
@synthesize greenQuantity;

- (void)viewDidLoad
    [super viewDidLoad];
    //Get color from NSUserDefaults
    NSArray *selfColorArray=[[NSUserDefaults standardUserDefaults] objectForKey:@"currentColor"];
    self.redQuantity=[[selfColorArray objectAtIndex:0]doubleValue];
    self.greenQuantity=[[selfColorArray objectAtIndex:1]doubleValue];
    self.blueQuantity=[[selfColorArray objectAtIndex:2]doubleValue];
    //Create a ellipse
    EllipseShape *ellipse=[[EllipseShape alloc]initWithFrame:CGRectMake(160,100, 18, 18)];
    [ellipse setDelegate:self];
    [self.view addSubview:ellipse];

- (void)didReceiveMemoryWarning
    [super didReceiveMemoryWarning];

//  EllipseColorDelegate
-(NSArray *)giveEllipseColors
    NSNumber *redNumber = [NSNumber numberWithDouble:self.redQuantity];
    NSNumber *greenNumber = [NSNumber numberWithDouble:self.greenQuantity];
    NSNumber *blueNumber = [NSNumber numberWithDouble:self.blueQuantity];
    NSArray *array = [[NSArray alloc] initWithObjects:redNumber, greenNumber,blueNumber, nil];
    return array;


Step 6:Run it
You will see the blue ellipse and you also can create a setting ViewController to change NSUserDefaults color.

1 comment:

  1. This comment has been removed by a blog administrator.