gpt4 book ai didi

ios - 如何在iPhone中获取手指触摸的x和y位置后设置ImageView的位置

转载 作者:行者123 更新时间:2023-11-29 03:48:00 25 4
gpt4 key购买 nike

我正在制作一个ipad应用程序,其中有自定义的tableView,它工作正常,但我希望当用户单击自定义单元格中的添加按钮时,它应该获取该按钮的位置x和y,然后将imageView设置为该按钮的中心。

我已经搜索了它获得积分的代码,但是如何添加为 cell.addButton 制作此代码,因为单元格是自定义的。

        [cell.imageButton addTarget:self action:@selector(imageButtonActionOne:) forControlEvents:UIControlEventTouchUpInside];
[cell.addButton addTarget:self action:@selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];

这是触摸代码

    UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
[cell.addButton addGestureRecognizer:rec];
[rec release];

但是这样它就不会调用cell.addButton imageButtonAction上的方法。

最佳答案

据我了解,您需要在点击时在 UIButton 中设置图像。

如果您添加了 UITapGestureRecognizer,则您删除了默认的点击识别,现在只有这个可以工作:

 UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
[cell.addButton addGestureRecognizer:rec];
[rec release];

您应该删除上面的代码并仅设置以下内容:

[cell.addButton addTarget:self action:@selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];

其中 imageButtonAction: 是:

- (void )imageButtonAction:(UIButton *) b
{
[b setImage:[UIImage imageNamed:@"img.png"] forState:UIControlStateNormal];
}

如果您想在按钮旁边添加图像(假设在按钮左侧),则该功能应如下所示:

- (void )imageButtonAction:(UIButton *) b
{
UITableViewCell *cell = (UITableViewCell*)[b superview];
NSInteger imgWidth = 50;
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(b.frame.origin.x - imgWidth,b.frame.origin.y,imgWidth,b.frame.size.height)];
img.backgroundColor = [UIColor redColor];
[cell addSubview:img];
[img release];

}


UIButton *btn = (UIButton *)sender;
UIImageView *backimage = [[UIImageView alloc] initWithFrame:CGRectMake(10,0,312,105)];

//If you want to do this set interaction enabled.
backimage.userInteractionEnabled = YES;
backimage.image=[UIImage imageNamed:@"popupj.png"];



tab1 = [UIButton buttonWithType:UIButtonTypeCustom];
[tab1 setFrame:CGRectMake(-1,2,293,58)];
[tab1 setTitle:@"Record Video" forState:UIControlStateNormal];
[tab1 addTarget:self action:@selector(tab1Action) forControlEvents:UIControlEventTouchUpInside];
[tab1 setImage:[UIImage imageNamed:@"popuptab1j.png"] forState:UIControlStateNormal];

[backimage addSubview:tab1];

现在我正在向 imageView 添加按钮,但它没有被单击

关于ios - 如何在iPhone中获取手指触摸的x和y位置后设置ImageView的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17466761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com