仅当用户点击图像视图时,才在 UIScrollView 上显示 UIButton Done for Images
Posted
技术标签:
【中文标题】仅当用户点击图像视图时,才在 UIScrollView 上显示 UIButton Done for Images【英文标题】:Show UIButton Done on UIScrollView for Images only when user taps on image view 【发布时间】:2012-10-03 14:19:16 【问题描述】:我如何才能为 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];
现在它显示在每个图像视图上,但我希望它只在点击屏幕时才显示 UIButton。
编辑:如果我添加
[imageView addGestureRecognizer: tap];
并启动手势识别器
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
[tap release];
那么当用户点击图像视图时,我如何在句柄点击方法中编码以显示完成按钮
- (void)handleTap:(UIGestureRecognizer*)tap
感谢您的帮助。
【问题讨论】:
【参考方案1】:您需要在图像视图上而不是在 ViewController 上添加手势识别器。
另外,您需要像这样为图像视图启用用户交互:
imageView.userInteractionEnabled = YES;
【讨论】:
【参考方案2】:检查UIScrollViewDelegate
方法当你滚动显示你的按钮将被显示,然后如果你想隐藏那个按钮隐藏它。
对于handleTap,你设置[imageView addGestureRecognizer:tap];
而不是[self addGestureRecognizer:tap];
在ViewDidLoad
中创建您的按钮,并在最初隐藏它以及何时用户点击刚刚设置的图像`
button.hidden = NO;
【讨论】:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; tap.numberOfTapsRequired = 1; [imageView addGestureRecognizer:tap];- (void)handleTap:(id)sender _myButton.hidden = NO; 点击时不工作 设置委托,如 tap.delegate = self;并转到 .h 文件并编写以上是关于仅当用户点击图像视图时,才在 UIScrollView 上显示 UIButton Done for Images的主要内容,如果未能解决你的问题,请参考以下文章
仅当条件为 True 时才在 python 中使用 Eel 调用 JavaScript 函数