forked from boctor/idev-recipes
/
RootViewController.m
165 lines (131 loc) · 6.75 KB
/
RootViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// RootViewController.m
// SideSwipeTableView
//
// Created by Peter Boctor on 4/13/11.
// Copyright 2011 Peter Boctor. All rights reserved.
//
#import "RootViewController.h"
#import "SideSwipeTableViewCell.h"
#import <QuartzCore/QuartzCore.h>
#define BUTTON_LEFT_MARGIN 10.0
#define BUTTON_SPACING 25.0
@interface RootViewController (PrivateStuff)
-(void) setupSideSwipeView;
-(UIImage*) imageFilledWith:(UIColor*)color using:(UIImage*)startImage;
@end
@implementation RootViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Setup the title and image for each button within the side swipe view
buttonData = [[NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"Reply", @"title", @"reply.png", @"image", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Retweet", @"title", @"retweet-outline-button-item.png", @"image", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Favorite", @"title", @"star-hollow.png", @"image", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Profile", @"title", @"person.png", @"image", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Links", @"title", @"paperclip.png", @"image", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Action", @"title", @"action.png", @"image", nil],
nil] retain];
buttons = [[NSMutableArray alloc] initWithCapacity:buttonData.count];
self.sideSwipeView = [[[UIView alloc] initWithFrame:CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.rowHeight)] autorelease];
[self setupSideSwipeView];
}
- (void) setupSideSwipeView
{
// Add the background pattern
self.sideSwipeView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"dotted-pattern.png"]];
// Overlay a shadow image that adds a subtle darker drop shadow around the edges
UIImage* shadow = [[UIImage imageNamed:@"inner-shadow.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0];
UIImageView* shadowImageView = [[[UIImageView alloc] initWithFrame:sideSwipeView.frame] autorelease];
shadowImageView.alpha = 0.6;
shadowImageView.image = shadow;
shadowImageView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.sideSwipeView addSubview:shadowImageView];
// Iterate through the button data and create a button for each entry
CGFloat leftEdge = BUTTON_LEFT_MARGIN;
for (NSDictionary* buttonInfo in buttonData)
{
// Create the button
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
// Make sure the button ends up in the right place when the cell is resized
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
// Get the button image
UIImage* buttonImage = [UIImage imageNamed:[buttonInfo objectForKey:@"image"]];
// Set the button's frame
button.frame = CGRectMake(leftEdge, sideSwipeView.center.y - buttonImage.size.height/2.0, buttonImage.size.width, buttonImage.size.height);
// Add the image as the button's background image
// [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
UIImage* grayImage = [self imageFilledWith:[UIColor colorWithWhite:0.9 alpha:1.0] using:buttonImage];
[button setImage:grayImage forState:UIControlStateNormal];
// Add a touch up inside action
[button addTarget:self action:@selector(touchUpInsideAction:) forControlEvents:UIControlEventTouchUpInside];
// Keep track of the buttons so we know the proper text to display in the touch up inside action
[buttons addObject:button];
// Add the button to the side swipe view
[self.sideSwipeView addSubview:button];
// Move the left edge in prepartion for the next button
leftEdge = leftEdge + buttonImage.size.width + BUTTON_SPACING;
}
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
SideSwipeTableViewCell *cell = (SideSwipeTableViewCell*)[theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[SideSwipeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
cell.supressDeleteButton = ![self gestureRecognizersSupported];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44 + indexPath.row * 5;
}
#pragma mark Button touch up inside action
- (IBAction) touchUpInsideAction:(UIButton*)button
{
NSIndexPath* indexPath = [tableView indexPathForCell:sideSwipeCell];
NSUInteger index = [buttons indexOfObject:button];
NSDictionary* buttonInfo = [buttonData objectAtIndex:index];
[[[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat: @"%@ on cell %d", [buttonInfo objectForKey:@"title"], indexPath.row]
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil] autorelease] show];
[self removeSideSwipeView:YES];
}
#pragma mark Generate images with given fill color
// Convert the image's fill color to the passed in color
-(UIImage*) imageFilledWith:(UIColor*)color using:(UIImage*)startImage
{
// Create the proper sized rect
CGRect imageRect = CGRectMake(0, 0, CGImageGetWidth(startImage.CGImage), CGImageGetHeight(startImage.CGImage));
// Create a new bitmap context
CGContextRef context = CGBitmapContextCreate(NULL, imageRect.size.width, imageRect.size.height, 8, 0, CGImageGetColorSpace(startImage.CGImage), kCGImageAlphaPremultipliedLast);
// Use the passed in image as a clipping mask
CGContextClipToMask(context, imageRect, startImage.CGImage);
// Set the fill color
CGContextSetFillColorWithColor(context, color.CGColor);
// Fill with color
CGContextFillRect(context, imageRect);
// Generate a new image
CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage* newImage = [UIImage imageWithCGImage:newCGImage scale:startImage.scale orientation:startImage.imageOrientation];
// Cleanup
CGContextRelease(context);
CGImageRelease(newCGImage);
return newImage;
}
- (void)dealloc
{
[buttons release];
[buttonData release];
[super dealloc];
}
@end