【问题标题】:Add a uiimageview inside a uiview when touching the uivew触摸 uivew 时在 uiview 中添加 uiimageview
【发布时间】:2016-08-25 10:01:23
【问题描述】:

我想要做的是当我触摸UIView时,我想在触摸的位置添加一个图像。

这是我目前拥有的代码。 (我曾尝试在CGRectMake 中用touchLocation 替换x 和y,但这不起作用)。

已编辑 清理并重新启动 xcode 后,它就可以工作了。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:touch.view];

    if ([touch view] == GrassView) {

        NSLog(@"Hit grass");

        UIImageView *imageViewFlower =[[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)];
        imageViewFlower.center = touchLocation;
        imageViewFlower.image= [UIImage imageNamed:@"whiteflower.png"];
        [GrassView addSubview:imageViewFlower];

    }
}

感谢您的帮助!

【问题讨论】:

  • 当你执行这段代码时,发生了什么?
  • @user3266053,如果解决了,请打开一个新问题,请不要添加其他问题。答案会以这种方式误导......

标签: ios objective-c uiview uiimageview uitouch


【解决方案1】:

尝试使用以下代码更改if 分支中的imageViewFlower 插入代码:

UIImageView *imageViewFlower =[[UIImageView alloc] initWithFrame:CGRectMake(touchLocation.x-25,touchLocation.y-25,50,50)];
imageViewFlower.image = [UIImage imageNamed:@"whiteflower.png"];
[GrassView addSubview:imageViewFlower];

【讨论】:

    【解决方案2】:

    清理并重新启动 xcode 后,代码可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-07
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      相关资源
      最近更新 更多