我有按钮显示scrollview。我在awesomeview下有图像。并且滚动具有滚动图像的精彩视图。然后使用myImageView8将精彩视图带到屏幕中心。当我单击scrollview图像时,myImageView8会在屏幕中央显示awesomeview。然后我重新按下按钮,再次出现scrollview。但myImageView8不可触摸。单击scrollview图像后,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];
}
答案 0 :(得分:2)
在向视图添加图片时,您需要向Image对象添加触摸事件:
OR
只需以编程方式启用用户交互
和
使用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
}
}