gpt4 book ai didi

iphone - 仅当用户点击 ImageView 时,才在图像的 UIScrollView 上显示 UIButton Done

转载 作者:行者123 更新时间:2023-12-03 20:25:06 26 4
gpt4 key购买 nike

我如何编写 UIButton 代码,使其仅在用户点击 UIScrollView 图像时才显示。

 - (void)viewDidLoad

{
self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

[myButton addTarget:self action:@selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];

myButton.frame = CGRectMake(xOrigin, 10, 60, 35);

[myButton.layer setMasksToBounds:YES];

[myButton.layer setCornerRadius:10.0f];

myButton.layer.borderWidth = 2;

myButton.layer.borderColor = [[UIColor whiteColor] CGColor];

[myButton setTitle:@"Done" forState:UIControlStateNormal];

myButton.backgroundColor = [UIColor blackColor];

NSString *imageName = [NSString stringWithFormat:@"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}

现在它显示在每个 ImageView 上,但我希望它应该仅在点击屏幕时显示 UIButton Done。

编辑:如果我添加

 [imageView addGestureRecognizer: tap];

并启动手势识别器

  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
[tap release];

那么我如何在句柄点击方法中进行编码,以在用户点击 ImageView 时显示完成按钮

 - (void)handleTap:(UIGestureRecognizer*)tap {

}

感谢您的帮助。

最佳答案

您需要在 ImageView 上添加手势识别器,而不是在 ViewController 上。

此外,您需要为 ImageView 启用用户交互,如下所示:

imageView.userInteractionEnabled = YES;

关于iphone - 仅当用户点击 ImageView 时,才在图像的 UIScrollView 上显示 UIButton Done,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12710473/

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