0

我正在尝试制作一个应用程序,您可以在其中录制“开火”,并且在您点击它的地方会使图像出现又名弹孔。我意识到我将不得不UIImageView出现,但我不知道如何。

任何帮助表示赞赏

4

4 回答 4

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
于 2013-08-30T05:23:11.747 回答
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
}

您可以根据需要设置不同的contentModelike UIViewContentModeScaleToFill、、、UIViewContentModeScaleAspectFitUIViewContentModeScaleAspectFill

于 2013-08-30T05:20:36.123 回答
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);
}

在上面的代码中,您将获得当前的触摸位置。因此,将您的图像放在该位置。希望它会帮助你。

于 2013-08-30T04:46:33.883 回答
0

将 aUITapGestureRecognizer应用于视图控制器的视图,并将其设置为执行显示图像的操作/方法。

于 2013-08-30T04:47:53.870 回答