I've subclass UIView to have my own drawing method functionality in which I drawn an image (green circle) whenever the user touches on the screen
http://yfrog.com/mwscreenshot20100713at409p
My problem is, if I have another image as a background, the drawn image (green circle) is behind the background image. Any idea how I could fix it?
I've tried to set the background image in IB and even in code still not working..
Any help. Thanks
anyone have some idea?😕
Attaching the sample code here:
http://www.mediafire.com/?qyzqyqyot3o
http://yfrog.com/mwscreenshot20100713at409p
My problem is, if I have another image as a background, the drawn image (green circle) is behind the background image. Any idea how I could fix it?
I've tried to set the background image in IB and even in code still not working..
Code:
#import <UIKit/UIKit.h>
@interface DrawView : UIView {
CGPoint firstTouch;
CGPoint lastTouch;
UIImage *drawImage;
CGRect redrawRect;
}
@property CGPoint firstTouch;
@property CGPoint lastTouch;
@property (nonatomic, retain) UIImage *drawImage;
@property (readonly) CGRect currentRect;
@property CGRect redrawRect;
@end
------------------
@implementation DrawView
@synthesize firstTouch;
@synthesize lastTouch;
@synthesize drawImage;
@synthesize redrawRect;
@synthesize currentRect;
-(CGRect)currentRect
{
return CGRectMake((firstTouch.x > lastTouch.x) ? lastTouch.x : firstTouch.x,
(firstTouch.y > lastTouch.y) ? lastTouch.y : firstTouch.y,
fabsf(firstTouch.x - lastTouch.x),
fabsf(firstTouch.y - lastTouch.y));
}
- (id)initWithCoder:(NSCoder*)coder
{
if (( self = [super initWithCoder:coder] ))
{
if (drawImage == nil)
{
self.drawImage = [UIImage imageNamed:@"green_circle.png"];
//self.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"Background.png"]];
}
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code
CGFloat horizontalOffset = drawImage.size.width / 2;
CGFloat verticalOffset = drawImage.size.height / 2;
CGPoint drawPoint = CGPointMake(lastTouch.x - horizontalOffset,
lastTouch.y - verticalOffset);
[drawImage drawAtPoint:drawPoint];
}
- (void)dealloc {
[drawImage release];
[super dealloc];
}
#pragma mark -
#pragma mark UI Touches
#pragma mark -
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
...
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
...
[self setNeedsDisplayInRect:redrawRect];
}
@end
Any help. Thanks
anyone have some idea?😕
Attaching the sample code here:
http://www.mediafire.com/?qyzqyqyot3o