可见时隐藏ImageView

时间:2013-07-15 08:10:59

标签: iphone uiview uiscrollview uiimageview hide

我有按钮来显示scrollView。我在awesomeView下有三个图像。然后我添加了awesomeView到scrollView滚动。当我点击任何图像时,awesomeView将添加到myImageView8。因为myImageView8将awesomeview图像显示在屏幕的中心。我需要隐藏myImageView8或awesomeView,当它在屏幕上可见时

代码:

scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 420, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
    //  CGFloat xOrigin = i * self.view.frame.size.width;
    // awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];

    awesomeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];

    awesomeView.backgroundColor=[UIColor clearColor];
    [scroll addSubview:awesomeView];

    img = [UIButton buttonWithType:UIButtonTypeCustom];

    [img setImage:[UIImage imageNamed:@"img.png"] forState:UIControlStateNormal];
    // [overlayButton setFrame:CGRectMake(80, 420, 60, 30)];

    [img setFrame:CGRectMake(0, 0, 100,130)];

    [img addTarget:self action:@selector(img:) forControlEvents:UIControlEventTouchUpInside];
    [img setTag:0];

    [awesomeView addSubview:img];

    img1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [img1 setImage:[UIImage imageNamed:@"sofa_s2.png"] forState:UIControlStateNormal];
    // [overlayButton setFrame:CGRectMake(80, 420, 60, 30)];
    [img1 setFrame:CGRectMake(110, 0, 100,130)];
    [img1 setTag:1];

    [img1 addTarget:self action:@selector(img1:) forControlEvents:UIControlEventTouchUpInside];
    [awesomeView addSubview:img1];

    img2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [img2 setImage:[UIImage imageNamed:@"sofa_s3.png"] forState:UIControlStateNormal];
    // [overlayButton setFrame:CGRectMake(80, 420, 60, 30)];

    [img2 setFrame:CGRectMake(210, 0, 100,130)];
    [img2 setTag:2];
    [img2 addTarget:self action:@selector(img2:) forControlEvents:UIControlEventTouchUpInside];
    [awesomeView addSubview:img2];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];

myImageView8用于显示屏幕中心的显示效果:

myImageView8=[[UIImageView alloc]initWithFrame:CGRectMake(0, 50, 320, self.view.frame.size.height)];
[self.view addSubview:myImageView8];
myImageView8.userInteractionEnabled=YES;

-(void)img:(id)sender {
    if(myImageView8){
        [myImageView8 removeFromsuperView];
    } 
    else {
        [myImageView8 addSubView:awesomeView];
    }
}

-(void)img1:(id)sender {
    if(myImageView8) {
        [myImageView8 removeFromsuperView];
    }
    else {
        [myImageView8 addSubView:awesomeView];
    }
}

-(void)img2:(id)sender {
    if(myImageView8) {
        [myImageView8 removeFromsuperView];
    }
    else {
    [myImageView8 addSubView:awesomeView];
    }
}

如果单击按钮,则显示滚动条。但是当我点击图片时,如果myImageView8可见,它会重新出现。

enter image description here

0 个答案:

没有答案