我正在尝试制作一个应用程序,您可以在其中录制“开火”,并且在您点击它的地方会使图像出现又名弹孔。我意识到我将不得不UIImageView
出现,但我不知道如何。
任何帮助表示赞赏
我正在尝试制作一个应用程序,您可以在其中录制“开火”,并且在您点击它的地方会使图像出现又名弹孔。我意识到我将不得不UIImageView
出现,但我不知道如何。
任何帮助表示赞赏
//
// ViewController.m
//
#import "ViewController.h"
@implementation ViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView: self.view];
UIImage *bulletImg = [UIImage imageNamed: @"bullet.png"];
UIImageView *bulletView = [[UIImageView alloc] initWithImage: bulletImg];
int bulletWidth = bulletImg.size.width;
int bulletHeight = bulletImg.size.height;
bulletView.frame = CGRectMake(
location.x - (bulletWidth / 2), // centered x position
location.y - (bulletHeight / 2), // centered y position
bulletWidth, // width
bulletHeight // height
);
[self.view addSubview: bulletView];
}
@end
确保您的userInteractionEnabled
观点YES
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
[self addBulletHoleImageAtLocation:location];
}
-(void) addBulletHoleImageAtLocation:(CGPoint)location {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(location.x, location.y, yourImageWidth, yourImageHeight)];
imgView.image = [UIImage imageNamed:@"bulletHole.png"];
imgView.contentMode = UIViewContentModeCenter;
[self.view addSubview:imgView];
[imgView release]; //if NON ARC
}
您可以根据需要设置不同的contentMode
like UIViewContentModeScaleToFill
、、、UIViewContentModeScaleAspectFit
或UIViewContentModeScaleAspectFill
。
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
NSLog(@"X cord: %f",location.x);
NSLog(@"Y cord: %f",location.y);
}
在上面的代码中,您将获得当前的触摸位置。因此,将您的图像放在该位置。希望它会帮助你。
将 aUITapGestureRecognizer
应用于视图控制器的视图,并将其设置为执行显示图像的操作/方法。