如何只在用户点击UIScrollView上的图像时才能显示UIButton的代码。
- (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];
并启动Gesturerecognizer
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
[tap release];
那么当用户点击图像视图时,如何在句柄点击方法中编码以显示完成按钮
- (void)handleTap:(UIGestureRecognizer*)tap {
}
感谢您的帮助。
答案 0 :(得分:5)
您需要在图像视图上添加手势识别器,而不是在ViewController上添加。
此外,您需要为图像视图启用用户交互,如下所示:
imageView.userInteractionEnabled = YES;
答案 1 :(得分:2)
检查UIScrollViewDelegate
方法何时滚动显示您的按钮将被显示,然后如果您想隐藏该按钮隐藏它。
对于handleTap,您设置[imageView addGestureRecognizer:tap];
而不是[self addGestureRecognizer:tap];
在ViewDidLoad
中创建您的按钮并在最初隐藏它,当用户点击图片时只需设置`
button.hidden = NO;