2011-04-19 71 views
3

我想显示一个图像(大于iphone的屏幕),用户可以滚动。 是不是difficoult,我已经使用此代码完成的:在我的.m文件 在我.h文件中UIImageView上的UIButton(通过UIScrollView)

@interface mappa1 : UIViewController <UIScrollViewDelegate> { 
IBOutlet UIImageView *img; 
IBOutlet UIScrollView *scroller; 
} 

UIImage *image = [UIImage imageNamed:@"prova.jpg"]; 
img = [[UIImageView alloc] initWithImage:image]; 
scroller.delegate = self; 
scroller.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
scroller.contentSize = img.frame.size; 
scroller.scrollEnabled = YES; 
scroller.directionalLockEnabled = NO; 
scroller.userInteractionEnabled = YES; 
CGSize ivsize = img.frame.size; 
CGSize ssize = scroller.frame.size; 

float scalex = ssize.width/ivsize.width; 
float scaley = ssize.height/ivsize.height; 
scroller.maximumZoomScale = 1.0; 
scroller.minimumZoomScale = fmin(1.0, fmax(scalex, scaley)); 
scroller.zoomScale = fmin(1.0, fmax(scalex, scaley)); 
[scroller addSubview:img]; 

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 

return img; 

}

和所有的工作正常!

现在我想在UIImage上添加一个UIButton,而不是在UIView上,因为我想如果用户放大或移动图像,uibutton会跟随uiimage的移动/缩放。

所以我添加以下代码:

UIButton *btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; 
btn.frame = CGRectMake(0, 0, 100, 25); 
[btn setTitle:@"Play" forState:UIControlStateNormal]; 
[btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside]; 
[img addSubview:btn]; 

但UIButton的是 “不点击”! 当然,UIView和UIImageView的UserInteractionEnabled设置为YES!

谢谢!

编辑:如果我写

[scroller addSubView:btn]; 

它的工作原理,但是,当然,如果用户放大/缩小,将UIButton的永远是大100×。

+0

有scrollViews“撤销的内容润色”的属性。你可以在IB的属性检查器中找到它。取消选中该选项,然后尝试。 – 2011-04-19 16:58:05

+0

谢谢,但不,它不起作用!此外,如果我取消选中“cancellabla content touches”,变焦不起作用! – JAA 2011-04-19 17:10:50

+0

您可能需要根据UIImage上的缩放级别手动实现按钮的大小调整。所以,只要有人放大图像,就可以调整按钮大小。 – Cyprian 2011-04-19 20:28:18

回答

2

解决了! 我不知道为什么,但如果我在Interface Builder中检查了UserInteractionEnabled(在UIImageView上),它不起作用。 如果我写

img.UserInteractionEnabled = YES; 

它的工作原理!

0

我想这和它的作品对我来说.......

UIImageView * imageView = [[UIImageView alloc]init]; 
[imageView setImage:[UIImage imageNamed:@"image.jpeg"]]; 
[imageView setFrame:CGRectMake(10,10,300,300)]; 
[imageView setContentMode:UIViewContentModeScaleToFill]; 
[imageView setUserInteractionEnabled:TRUE]; 

UIButton * ButtonOnImageView = [[UIButton alloc]init]; 
[ButtonOnImageView setFrame:CGRectMake(135,120,60,30)]; 
[ButtonOnImageView setImage:[UIImage imageNamed:@"image.jpeg"] forState:UIControlStateHighlighted]; 
[ButtonOnImageView setImage:[UIImage imageNamed:@"image2.jpeg"] forState:UIControlStateNormal]; 
[ ButtonOnImageView addTarget:self action:@selector(OnClickOfTheButton) forControlEvents:UIControlEventTouchUpInside]; 

[imageView addSubview:ButtonOnImageView]; 
[self.view addSubview:imageView];