0

我正在尝试做一个程序,每次我的触摸结束时,都会出现另一个 UIView 使用循环显示我想要的 UIView 数量。如何在触摸结束时设置 UIView 循环?或者我应该在 viewDidLoad 中创建它并在触摸结束时调用它?

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event//upon leaving
    {
        UITouch *touch = [touches anyObject];

        for (int i=0; i < 100; i++) {
            UIImageView *layerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
            [layerView setAlpha:.05];
            [self.view addSubview:layerView];
        }
    }

希望你们能帮帮我。。

4

1 回答 1

2

如果您想在每次点击后添加一个视图,则不需要 for 循环。做就是了

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event//upon leaving
{
    UITouch *touch = [touches anyObject];

    UIImageView *layerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
    [layerView setAlpha:.05];

    // If you just want to add a line, add another uiview would the easiest way. For example:
    UIView *line = [UIView alloc] initWithFrame:CGRectMake(0, layerView.center.y, layerView.frame.size.width, 1)];
    line.backgroundColor = [UIColor blackColor];
    [layerView addSubview:line];

    [self.view addSubview:layerView];
}

当然,你也可以继承一个 UIView 并在其中绘制任何你想要的东西

- (void)drawRect:(CGRect)rect
于 2012-07-20T02:40:02.880 回答