2

我如何为 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 Done。

编辑:如果我添加

 [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 {

   }

感谢帮助。

4

2 回答 2

5

您需要在图像视图上而不是在 ViewController 上添加手势识别器。

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

imageView.userInteractionEnabled = YES;
于 2012-10-03T14:54:42.750 回答
2

检查UIScrollViewDelegate方法,当你滚动显示你的按钮将被显示,然后如果你想隐藏那个按钮,隐藏它。

对于你设置的 handleTap[imageView addGestureRecognizer:tap];而不是[self addGestureRecognizer:tap];

在其中创建您的按钮ViewDidLoad并最初隐藏它以及用户何时点击刚刚设置的图像`

button.hidden = NO;
于 2012-10-03T14:55:26.203 回答