我想知道如何让图像显示在用户触摸的位置。我有一个与IBAction相关联的UIButton,当用户点击按钮时,我希望图像出现在触摸的x,y处。
任何人都有任何指示?谢谢!
答案 0 :(得分:0)
为什么使用UI按钮在触摸位置添加图像?
以下代码是在视图的触摸位置添加图像。试试这个 (别忘了在.h文件中添加委托)
- (void)viewDidLoad
{
//code for adding touch recognizer for UIview.
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:self.view];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(location.x-imagewidth/2,location.y-imageheight/2,imagewidth,imageheight)];
imageview.image = [UIImage imageNamed:@"imagename.png"];
[self.view addSubview:imageview];
}