0

UITapGestureRecognizer并且UIButton没有一起工作。

UIButton没有 . 单独工作正常UITapGesturerecognizer。它显示在所有滚动图像视图中,但在添加UITapGestureReconizer功能后,点击时不显示UIButton

BOOL numberofTaps;
@interface ImageScrollViewController : UIViewController <UIGestureRecognizerDelegate>
@property (nonatomic, assign) UITapGestureRecognizer *recognizer;

- (void)handleTap:(UIGestureRecognizer*)sender;
//////////////////////////
- (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];
        myButton.hidden = YES;

        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);

        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
        recognizer.numberOfTapsRequired = 1;
        [imageView addGestureRecognizer:recognizer];
        imageView.userInteractionEnabled = YES;
        recognizer.delegate = self;
        numberofTaps = 1;
        [recognizer release];


        [imageScrollView addSubview:imageView];
        [imageScrollView addSubview:myButton];
     // [imageScrollView addGestureRecognizer:tap];
     // [imageView addSubview:tap];
        [imageView release];
    }
    imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
    [self.view addSubview:imageScrollView];
    [imageScrollView release];
 }

编辑:这怎么行,但是当我只取消注释 myButton.hidden= NO 时,它可以工作,但仍然没有在图像视图上显示我的按钮 DONE

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

   // if(numberofTaps == 1){
    CGPoint tapPoint = [sender locationInView:_imageScrollView];
    int tapX = (int) tapPoint.x;
    int tapY = (int) tapPoint.y;
    NSLog(@"TAPPED X:%d Y:%d", tapX, tapY);
//_myButton.hidden = NO;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"How are you?" delegate:nil cancelButtonTitle:@"I'm awesome." otherButtonTitles:nil];
[alert show];
[alert release];
}

否则它正在工作但没有显示完成 UIButton 的原因是什么?

4

1 回答 1

0

首先,您必须找到button要展示的内容。因此,为此,您必须setTag:先将UIButton它们添加到scrollView

像这样 -

myButton.tag = i;

然后在添加标签UIImageView之前添加标签到你的也scrollView像这样 -

 imageView.tag = i*100;

现在在handleTap:方法中,您可以比较标签并获得您必须显示的按钮。

-(void)handleTap:(UIGestureRecognizer *)sender 
{
    //getting all buttons of scrollView
    for(UIButton *button in scrollView.subviews)
    {
       //comparing tags
       if(button.tag == sender.view.tag/100)
       {
           button.hidden = NO;
       }
    }
}

这样做,删除你的 if 条件。

于 2012-10-03T17:51:39.143 回答