/ Published in: iPhone
Expand |
Embed | Plain Text
MyDrawView file: #import <UIKit/UIKit.h> @interface MyDrawView : 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 ------------------------------------- #import "MyDrawView.h" @implementation MyDrawView @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)); } { if (( self = [super initWithCoder:coder] )) { if (drawImage == nil) { self.drawImage = [UIImage imageNamed:@"green_circle.png"]; } } return self; } - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { // Initialization code } 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 - { UITouch *touch = [touches anyObject]; firstTouch = [touch locationInView:self]; lastTouch = [touch locationInView:self]; [self setNeedsDisplay]; } { UITouch *touch = [touches anyObject]; lastTouch = [touch locationInView:self]; //[self setNeedsDisplay]; CGFloat horizontalOffset = drawImage.size.width / 2; CGFloat verticalOffset = drawImage.size.height / 2; redrawRect = CGRectUnion(redrawRect, CGRectMake(lastTouch.x - horizontalOffset, lastTouch.y - verticalOffset, drawImage.size.width, drawImage.size.height)); redrawRect = CGRectInset(redrawRect, -2.0, -2.0); [self setNeedsDisplayInRect:redrawRect]; } @end ---------------------------- MyDrawViewController @interface MyDrawViewController : UIViewController { UIImageView *m_myImage; } @property(nonatomic,retain) IBOutlet UIImageView *m_myImage; @end ------------ .m file: ... - (void)viewDidLoad { [super viewDidLoad]; CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f); m_myImage = [[UIImageView alloc] initWithFrame:myImageRect]; [m_myImage setImage:[UIImage imageNamed:@"Background.png"]]; m_myImage.opaque = YES; // explicitly opaque for performance [self.view addSubview:m_myImage]; }
You need to login to post a comment.
