@interface TestViewController : UIViewController {
UIImageView *imageView;
}
@property (readwrite, retain) UIImageView *imageView;
@end
@implementation TestViewController
@synthesize imageView;
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *tmpImage = [UIImage imageNamed:@"block.png"];
UIImageView *tmpImageView = [[UIImageView alloc] initWithImage:tmpImage];
CGRect initialFrame = CGRectMake(0, 0, tmpImage.size.width, tmpImage.size.height);
tmpImageView.frame = initialFrame;
tmpImageView.center = CGPointMake( self.view.frame.size.width / 2.0,
self.view.frame.size.height / 2.0);
self.imageView = tmpImageView;
[self.view addSubview:self.imageView];
[tmpImageView release];
tmpImageView = nil;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] == 1) {
// Single touch event, we will process here
UITouch *touch = [touches anyObject]; // Single object in the set so we can trust this
CGPoint location = [touch locationInView:self.view];
[UIView beginAnimations:@"MyAnimation" context:nil]; // only needed if you want to animate the move
[UIView setAnimationDuration:0.25f]; // only needed if you want to animate the move
self.imageView.center = location;
[UIView commitAnimations]; // only needed if you want to animate the move
} else {
// Not applicable to us pass to next responder
[[self nextResponder] touchesBegan:touches withEvent:event];
}
}
|