Skip to content

Commit

Permalink
Updated custom colored accessory
Browse files Browse the repository at this point in the history
  • Loading branch information
myell0w committed Apr 12, 2011
1 parent 0b005fb commit 3aff8db
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 14 deletions.
16 changes: 15 additions & 1 deletion TableView/DTCustomColoredAccessory.h
Expand Up @@ -6,18 +6,32 @@
// Copyright 2011 @myell0w. All rights reserved.
//
// Found on cocoanetics.com
// http://www.cocoanetics.com/2011/03/expandingcollapsing-tableview-sections/

#import <Foundation/Foundation.h>


@interface DTCustomColoredAccessory : UIControl {
typedef enum
{
DTCustomColoredAccessoryTypeRight = 0,
DTCustomColoredAccessoryTypeUp,
DTCustomColoredAccessoryTypeDown
} DTCustomColoredAccessoryType;

@interface DTCustomColoredAccessory : UIControl
{
UIColor *_accessoryColor;
UIColor *_highlightedColor;

DTCustomColoredAccessoryType _type;
}

@property (nonatomic, retain) UIColor *accessoryColor;
@property (nonatomic, retain) UIColor *highlightedColor;

@property (nonatomic, assign) DTCustomColoredAccessoryType type;

+ (DTCustomColoredAccessory *)accessoryWithColor:(UIColor *)color;
+ (DTCustomColoredAccessory *)accessoryWithColor:(UIColor *)color type:(DTCustomColoredAccessoryType)type;

@end
74 changes: 61 additions & 13 deletions TableView/DTCustomColoredAccessory.m
Expand Up @@ -26,27 +26,74 @@ - (void)dealloc
[super dealloc];
}

+ (DTCustomColoredAccessory *)accessoryWithColor:(UIColor *)color
+ (DTCustomColoredAccessory *)accessoryWithColor:(UIColor *)color {
return [self accessoryWithColor:color type:DTCustomColoredAccessoryTypeRight];
}

+ (DTCustomColoredAccessory *)accessoryWithColor:(UIColor *)color type:(DTCustomColoredAccessoryType)type
{
DTCustomColoredAccessory *ret = [[[DTCustomColoredAccessory alloc] initWithFrame:CGRectMake(0, 0, 11.0, 15.0)] autorelease];
DTCustomColoredAccessory *ret = [[[DTCustomColoredAccessory alloc] initWithFrame:CGRectMake(0, 0, 15.0, 15.0)] autorelease];
ret.accessoryColor = color;
ret.type = type;

return ret;
}

- (void)drawRect:(CGRect)rect
{
// (x,y) is the tip of the arrow
CGFloat x = CGRectGetMaxX(self.bounds)-3.0;;
CGFloat y = CGRectGetMidY(self.bounds);
const CGFloat R = 4.5;
CGContextRef ctxt = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctxt, x-R, y-R);
CGContextAddLineToPoint(ctxt, x, y);
CGContextAddLineToPoint(ctxt, x-R, y+R);
CGContextSetLineCap(ctxt, kCGLineCapSquare);
CGContextSetLineJoin(ctxt, kCGLineJoinMiter);
CGContextSetLineWidth(ctxt, 3);
CGContextRef ctxt = UIGraphicsGetCurrentContext();

const CGFloat R = 4.5;

switch (_type)
{
case DTCustomColoredAccessoryTypeRight:
{
// (x,y) is the tip of the arrow
CGFloat x = CGRectGetMaxX(self.bounds)-3.0;;
CGFloat y = CGRectGetMidY(self.bounds);

CGContextMoveToPoint(ctxt, x-R, y-R);
CGContextAddLineToPoint(ctxt, x, y);
CGContextAddLineToPoint(ctxt, x-R, y+R);

break;
}

case DTCustomColoredAccessoryTypeUp:
{
// (x,y) is the tip of the arrow
CGFloat x = CGRectGetMaxX(self.bounds)-7.0;;
CGFloat y = CGRectGetMinY(self.bounds)+5.0;

CGContextMoveToPoint(ctxt, x-R, y+R);
CGContextAddLineToPoint(ctxt, x, y);
CGContextAddLineToPoint(ctxt, x+R, y+R);

break;
}

case DTCustomColoredAccessoryTypeDown:
{
// (x,y) is the tip of the arrow
CGFloat x = CGRectGetMaxX(self.bounds)-7.0;;
CGFloat y = CGRectGetMaxY(self.bounds)-5.0;

CGContextMoveToPoint(ctxt, x-R, y-R);
CGContextAddLineToPoint(ctxt, x, y);
CGContextAddLineToPoint(ctxt, x+R, y-R);

break;
}


default:
break;
}

CGContextSetLineCap(ctxt, kCGLineCapSquare);
CGContextSetLineJoin(ctxt, kCGLineJoinMiter);
CGContextSetLineWidth(ctxt, 3);

if (self.highlighted)
{
Expand Down Expand Up @@ -89,5 +136,6 @@ - (UIColor *)highlightedColor

@synthesize accessoryColor = _accessoryColor;
@synthesize highlightedColor = _highlightedColor;
@synthesize type = _type;

@end

0 comments on commit 3aff8db

Please sign in to comment.