识别UIScrollView中的UIButton Tap

时间:2014-04-05 04:51:15

标签: objective-c uiscrollview uibutton

我有UIScrollView动态添加UIImageViews。 在故事板上

enter image description here

运行程序后

enter image description here

添加图片的代码在我的自定义类上。该课程未分配到任何视图:

- (id)initWithModel:(UIScrollView *)scroller page:(NSInteger)page image:(UIImage*)image{
    self = [super init];
    if (self) {
        _scrollView = scroller;
        _imageScrollIndex = page;
        _image = image;
        _frame = _scrollView.bounds;
        _frame.origin.x = _frame.size.width * _imageScrollIndex;
        _frame.origin.y = 0.0f;
        _frame = CGRectInset(_frame, 10.0f, 0.0f);
        _imageView = [[UIImageView alloc] initWithImage:_image];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        _imageView.frame = _frame;
        //adding delete image button
        CGRect frame = _imageView.frame;
        frame.origin.x = frame.size.width/2;
        frame.origin.y = -frame.size.height/2+45;
        //_imageView.userInteractionEnabled = YES;
        UIButton *deleteImgButton = [[UIButton alloc] initWithFrame:frame];
        deleteImgButton.userInteractionEnabled = YES;
        [deleteImgButton addTarget:self action:@selector(clickedDeleteButton:)forControlEvents:UIControlEventTouchUpInside];
        [deleteImgButton setImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];
        [_imageView addSubview:deleteImgButton];

        ////
        [_scrollView addSubview:_imageView];        
    }
    return self;
}

- (void)clickedDeleteButton:(UIButton*)button
{
    printf("hi:\n");
}

我想成为:

  1. 能够识别滚动事件(因此我认为我需要保持userinteractionenabled的{​​{1}}是
  2. 能够识别按下图像的删除按钮
  3. 任何想法如何?

1 个答案:

答案 0 :(得分:0)

 _scrl_ipad.contentSize = CGSizeMake(768,273 * temp);
    _scrl_ipad.showsVerticalScrollIndicator=NO;


        UIButton *btn_scrl=[UIButton buttonWithType:UIButtonTypeCustom];
        [btn_scrl setFrame:CGRectMake(0,0, 328, 243)];
        btn_scrl.tag=i-1;
        [btn_scrl addTarget:self action:@selector(btn_scrl_tag:) forControlEvents:UIControlEventTouchUpInside];
        [_scrl_ipad addSubview:btn_scrl];


-(IBAction)btn_scrl_tag:(id)sender
{
    UIButton *btn = (UIButton *)sender;

    NSLog(@"%d",btn.tag);
}