Iphone 仅当用户点击图像视图时,在UIScrollView上为图像显示UIButton Done
如何编写UIButton代码,使其仅在用户点击UIScrollView查看图像时显示Iphone 仅当用户点击图像视图时,在UIScrollView上为图像显示UIButton Done,iphone,ios6,uigesturerecognizer,Iphone,Ios6,Uigesturerecognizer,如何编写UIButton代码,使其仅在用户点击UIScrollView查看图像时显示 - (void)viewDidLoad { self.view.backgroundColor = [UIColor blackColor]; UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.fra
- (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];
}
并启动手势识别器
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
[tap release];
那个么,当用户点击图像视图时,我如何在handle tap方法中编写代码来显示done按钮呢
- (void)handleTap:(UIGestureRecognizer*)tap {
}
谢谢您的帮助。您需要在图像视图而不是ViewController上添加手势识别器 此外,您还需要启用图像视图的用户交互,如下所示:
imageView.userInteractionEnabled = YES;
当您滚动显示按钮时,请选中
UIScrollViewDelegate
方法,然后如果要隐藏该按钮,请将其隐藏
对于handleTap,您可以设置[imageView addgestureRecognitor:tap]
而不是[自添加手势识别器:点击]代码>
在ViewDidLoad
中创建按钮,并在用户点击刚刚设置的图像时将其隐藏`
button.hidden = NO;
UITapGestureRecognizer*点击=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];tap.numberOfTapsRequired=1;[imageView ADDGESTURE识别器:点击];-(void)handleTap:(id)当点击设置委托(如点击.delegate=self)时,发送方{{u myButton.hidden=NO;}不工作;然后转到.h文件并写下你能告诉我你面临什么问题吗?在handleTap中放置一个断点,然后检查它是否指向该方法。我在句柄点击处添加了断点,它将指向该方法。想知道为什么不显示done button Set button.hidden=否;并设置背景色以检查按钮框。
button.hidden = NO;