我正在尝试创建一个应用程序,您可以在其中点击“开火”,然后点击它会使图像显示为一个弹孔。我意识到我必须出现UIImageView
,但我不知道如何。
感谢任何帮助
答案 0 :(得分:1)
-(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);
}
在上面的代码中,您将获得当前的触摸位置。将图像放在该位置。 希望它会对你有所帮助。
答案 1 :(得分:1)
确保您的观看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
,UIViewContentModeScaleToFill
,UIViewContentModeScaleAspectFit
或UIViewContentModeScaleAspectFill
。
答案 2 :(得分:1)
//
// 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
答案 3 :(得分:0)
将UITapGestureRecognizer
应用于视图控制器的视图,并将其设置为执行显示图像的操作/方法。