1

我有按钮来显示滚动视图。我在 awesomeview 下有图像。并且滚动具有用于滚动图像的 awesomeview。然后让 myImageView8 将真棒视图带到屏幕中心。当我单击滚动视图图像时,myImageView8 在屏幕中央显示 awesomeview。然后我重新按下按钮,滚动视图重新出现。但是 myImageView8 是不可触摸的。单击滚动查看图像后,myImageView8 再次在屏幕中央显示真棒。

代码:

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{
 [myImageView8 addSubview:awesomeView];


}

-(void)img1:(id)sender{
 [myImageView8 addSubview:awesomeView];


}

-(void)img2:(id)sender{
 [myImageView8 addSubview:awesomeView];


}
4

1 回答 1

2

当您将图像添加到视图中时,您需要将触摸事件添加到图像对象:

在此处输入图像描述

或者

只需以编程方式启用用户交互

使用 UIResponder 方法 touchesBegan、touchesMoved、touchesEnded 等来检测图像视图上的触摸:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    if ([touch view] == yourImageView)
    {
            //add your code for image touch here 
    }

}
于 2013-07-12T07:00:41.297 回答