ios - 以编程方式创建 subview ,然后添加 TapGesture For Action

标签 ios objective-c cocoa-touch

我是 PHP 背景的初学者。 我想使用 FOR 循环创建 subview ,然后检测哪个 subview 被点击,然后执行一些操作。 到目前为止,我已经成功地使用这段代码实现了创建 subview :

内部 -(IBAction)ADD:(id)sender{

UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:scrollview];

float x=0;
float y=0;
float w = 160.0; //width of a box
float h = 100.0; //height of a box
int num = 0;
int totalBoxes = 20;
BOOL yChanged = false; //this is to know if a new line to be added by incrementing y
NSTimeInterval delayTime = 0.1; //for delaying the animation a bit
for(int i=0; i<=totalBoxes; i++){
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x,y, w, h)];
    view.backgroundColor = [UIColor blueColor];
    view.alpha = 0.0; //initially setting this view to alpha=0 for fadeIn effect
    view.userInteractionEnabled = YES;
    [scrollview addSubview:view];
    delayTime += 0.1; //for delaying the animation a bit
    [UIView animateWithDuration:0.1
                    delay: delayTime
                    options: UIViewAnimationOptionCurveEaseIn
                    animations:^{view.alpha=1.0;}
                    completion:nil]; //performing block animation
    //this is all for detecting where to add the next box...
    if(yChanged){
        num = 1;
        yChanged = false;
    } else {
        num ++;
    }

    x += w+1;
    if(num >= 2){
        y += h+1;
        yChanged = true;
        x = 0;
    }
}
scrollview.contentSize = CGSizeMake(self.view.frame.size.width, (totalBoxes/2)*h);

现在,我不知道在哪里添加点击手势。 我想要的是,当一个框被点击一次时,我知道哪个框被点击了,这样我就可以从中提取一些信息并将其显示在新的 subview 上(即将所有框滑出屏幕然后显示一个我将在其中显示从 Tapped Box 中获取的内容的新 View 。这更像是一个 UITableView,但由于我的设计需要,我不想使用它。)

有什么想法吗?非常感谢,谢谢!

****更新**** 根据 Owen Hartnett 答案:

我在 [scrollview addSubview:view]; 之前添加了以下代码:

UIButton *playButton = [[UIButton init] initWithFrame:CGRectMake(5, 5, 50, 50)];
[playButton setTitle:@"Play" forState:UIControlStateNormal];
[playButton setTitleColor:[UIColor darkTextColor]forState:UIControlStateNormal];
[playButton setEnabled:YES];
[playButton setUserInteractionEnabled:YES];
[playButton addTarget: view
                   action:@selector()
         forControlEvents: UIControlEventTouchDown];

[view addSubview:playButton];

现在,我对如何在 action:@selector() 中专门为此按钮添加操作感到困惑? 这样每个按钮都会执行独特的操作。 请指教,谢谢!

最佳答案

在将新 View 作为 subview 添加到 ScrollView 后立即将其添加到您的代码中

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = view.frame;
//tag should be > 0
button.tag = i+1;
[button addTarget:self action:@selector(scrollButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:button];

当然,将此功能添加到您的 Controller 中:

-(void)scrollButtonTapped:(UIButton *)sender
{
   NSInteger scrollViewIndex = sender.tag-1;
}

关于ios - 以编程方式创建 subview ,然后添加 TapGesture For Action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25748428/

相关文章:

ios - iOS 钥匙串(keychain)并不总是存储值

ios - 为什么我的 NSFetchedResultsController 加载所有行?

ios - 核心数据executeFetchRequest消耗大量内存

objective-c - 测试 UIViews 的相等性,需要澄清

objective-c - 动态更改 Windows 根目录,Swift

iphone - 从 NSMutableArray 中的元素中删除字符?

iphone - 在 subview 的drawRect之后刷新父UIScrollView contentSize

ios - IOS如何添加、旋转、拖线?

ios - 为什么导航 Controller 从 Storyboard访问 View ?

iphone - 如何像闹钟专业应用一样播放超过30秒的闹钟声音?