【发布时间】: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