5
// Created by döme on 11.08.2009.
6
// Copyright 2009 __MyCompanyName__. All rights reserved.
10
#import "ElAnimation.h"
12
#import <QuartzCore/QuartzCore.h>
14
void createPathInRoundedRect(CGContextRef c, CGRect rect, float radius)
16
CGContextBeginPath(c);
17
CGContextMoveToPoint(c, CGRectGetMinX(rect) + radius, CGRectGetMinY(rect) );
18
CGContextAddLineToPoint(c, CGRectGetMaxX(rect) - radius, CGRectGetMinY(rect));
19
CGContextAddArcToPoint(c, CGRectGetMaxX(rect), CGRectGetMinY(rect), CGRectGetMaxX(rect), CGRectGetMinY(rect) + radius, radius);
20
CGContextAddLineToPoint(c, CGRectGetMaxX(rect), CGRectGetMaxY(rect) - radius);
21
CGContextAddArcToPoint(c, CGRectGetMaxX(rect), CGRectGetMaxY(rect), CGRectGetMaxX(rect) - radius, CGRectGetMaxY(rect), radius);
22
CGContextAddLineToPoint(c, CGRectGetMinX(rect) + radius, CGRectGetMaxY(rect));
23
CGContextAddArcToPoint(c, CGRectGetMinX(rect), CGRectGetMaxY(rect), CGRectGetMinX(rect), CGRectGetMaxY(rect) - radius, radius);
24
CGContextAddLineToPoint(c, CGRectGetMinX(rect), CGRectGetMinY(rect) + radius );
25
CGContextAddArcToPoint(c, CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetMinX(rect) + radius, CGRectGetMinY(rect), radius);
26
CGContextClosePath(c);
33
@implementation ElImageView
35
- (id) initWithFrame: (CGRect) frame
37
if (!(self = [super initWithFrame: frame]))
40
borderColor = [[UIColor grayColor] retain];
45
[self setBackgroundColor: [UIColor redColor]];
51
- (id) initWithCoder: (NSCoder*) coder
53
if (!(self = [super initWithCoder: coder]))
56
borderColor = [[UIColor grayColor] retain];
63
[self setBackgroundColor: [UIColor clearColor]];
71
[borderColor release];
76
- (void) drawRect: (CGRect) rect
78
CGContextRef c = UIGraphicsGetCurrentContext();
79
//[[UIColor blueColor] set];
80
//CGContextFillRect(c, rect);
82
CGContextSaveGState(c);
91
img = [UIImage imageNamed: @"image-not-found-icon.png"];
93
CGRect bounds = CGRectInset([self bounds], 0.5*borderWidth,0.5*borderWidth);
94
CGSize imgSize = [img size];
97
float drawHeight = imgSize.height;
98
float drawWidth = imgSize.width;
100
float heightR = imgSize.height/bounds.size.height;
101
float widthR = imgSize.width/bounds.size.width;
105
drawHeight = bounds.size.height;
106
drawWidth *= 1.0f/heightR;
107
widthR = drawWidth/bounds.size.width;
112
drawWidth = bounds.size.width;
113
drawHeight *= 1.0f/widthR;
116
CGRect drawRect = CGRectMake(bounds.origin.x + 0.5f*(bounds.size.width - drawWidth), bounds.origin.y + 0.5f*(bounds.size.height - drawHeight), drawWidth, drawHeight);
121
createPathInRoundedRect(c, drawRect, cornerRadius);
125
[img drawInRect: drawRect blendMode: kCGBlendModeNormal alpha: imageAlpha];
127
CGContextRestoreGState(c);
130
[[borderColor colorWithAlphaComponent: imageAlpha] set];
131
createPathInRoundedRect(c, drawRect, cornerRadius);
132
CGContextSetLineWidth(c, borderWidth);
133
CGContextStrokePath(c);
137
- (void) setImageAlpha: (float) a
140
[self setNeedsDisplay];
143
- (void) setImage: (UIImage*) img
151
[self performSelectorOnMainThread: @selector(setNeedsDisplay) withObject: nil waitUntilDone: NO];
152
//[self setNeedsDisplay];
155
@synthesize image, cornerRadius, borderColor, borderWidth, fillView, imageAlpha;
161
@implementation ImageWell
164
- (id) initWithFrame: (CGRect) frame
166
if (!(self = [super initWithFrame: frame]))
172
- (id) initWithCoder: (NSCoder*) coder
174
if (!(self = [super initWithCoder: coder]))
177
instructionsView = [[UITextView alloc] initWithFrame: CGRectInset([self bounds], 0.0f, 0.0f)];
178
[instructionsView setText: @"\n\n\nTap and hold to paste image,\n\nor\n\nDouble-tap to select image from gallery."];
179
//[instructionsView setBackgroundColor: [UIColor yellowColor]];
180
[instructionsView setTextColor: [UIColor whiteColor]];
181
[instructionsView setBackgroundColor: [UIColor clearColor]];
182
[instructionsView setFont: [UIFont systemFontOfSize: [UIFont labelFontSize]]];
183
[instructionsView setEditable: NO];
184
[instructionsView setUserInteractionEnabled: NO];
185
[instructionsView setTextAlignment: UITextAlignmentCenter];
186
[self addSubview: instructionsView];
188
[instructionsView layer].opacity = 0.0f;
194
- (void) setFrame: (CGRect) frame
196
[super setFrame: frame];
197
[instructionsView setFrame: CGRectInset([self bounds], 0.0f, 0.0f)];
207
- (void) pasteAction: (id) info
209
NSArray* images = [[UIPasteboard generalPasteboard] images];
212
UIImage* img = [images objectAtIndex: 0];
214
[self setImage: img];
215
[delegate imageWasSelectedInWell: self];
220
- (void) showInstructions: (float) time
222
time = MAX(1.0f, time);
224
ElFloatAnimation* anim = nil;
226
anim = [[[ElFloatAnimation alloc] init] autorelease];
228
[anim setStartValue: [[instructionsView layer] opacity]];
229
[anim setEndValue: 0.667f];
230
[anim setDuration: 0.5f];
231
[anim setTarget: [instructionsView layer]];
232
[anim setProperty: @"opacity"];
233
[anim queueForKey: @"text-opacity" atBeginning: YES cancelPending: YES];
236
anim = [[[ElFloatAnimation alloc] init] autorelease];
238
[anim setStartValue: imageAlpha];
239
[anim setEndValue: 0.333f];
240
[anim setDuration: 0.5f];
241
[anim setTarget: self];
242
[anim setProperty: @"imageAlpha"];
243
[anim queueForKey: @"image-opacity" atBeginning: YES cancelPending: YES];
245
[[ElAnimation delayedAnimationWithDelay: time - 0.5f] queueForKey: @"text-opacity" atBeginning: NO cancelPending: NO];
246
[[ElAnimation delayedAnimationWithDelay: time - 0.5f] queueForKey: @"image-opacity" atBeginning: NO cancelPending: NO];
248
anim = [[[ElFloatAnimation alloc] init] autorelease];
250
[anim setStartValue: 0.667f];
251
[anim setEndValue: 0.0f];
252
[anim setDuration: 0.5f];
253
[anim setTarget: [instructionsView layer]];
254
[anim setProperty: @"opacity"];
255
[anim queueForKey: @"text-opacity" atBeginning: NO cancelPending: NO];
258
anim = [[[ElFloatAnimation alloc] init] autorelease];
260
[anim setStartValue: 0.333f];
261
[anim setEndValue: 1.0f];
262
[anim setDuration: 0.5f];
263
[anim setTarget: self];
264
[anim setProperty: @"imageAlpha"];
265
[anim queueForKey: @"image-opacity" atBeginning: NO cancelPending: NO];
268
- (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
270
UITouch* touch = [touches anyObject];
272
if ([touch tapCount] == 1)
274
[self showInstructions: 3.0f];
278
touchBeganTime = [event timestamp];
280
pasteTimer = [NSTimer scheduledTimerWithTimeInterval: 3.0 target: self selector:@selector(pasteAction:) userInfo: nil repeats: NO];
282
if ([touch tapCount] == 2)
284
[pasteTimer invalidate];
289
imagePicker = [[UIImagePickerController alloc] init];
291
//[imagePicker setSourceType: UIImagePickerControllerSourceTypeCamera];
292
[imagePicker setDelegate: self];
293
//[imagePicker setAllowsImageEditing: YES];
294
//[imagePicker setShowsCameraControls: NO];
298
[delegate presentModalViewController: imagePicker animated: YES];
302
- (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event
304
// UITouch* touch = [touches anyObject];
306
[pasteTimer invalidate];
311
- (void) touchesCancelled: (NSSet*) touches withEvent: (UIEvent*) event
313
// UITouch* touch = [touches anyObject];
315
[pasteTimer invalidate];
320
- (void)imagePickerController: (UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
323
UIImage* img = [info objectForKey: UIImagePickerControllerEditedImage];
326
img = [info objectForKey: UIImagePickerControllerOriginalImage];
330
[self setImage: img];
331
[delegate imageWasSelectedInWell: self];
334
[delegate dismissModalViewControllerAnimated: YES];
337
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
339
[delegate dismissModalViewControllerAnimated: YES];
343
@synthesize delegate;