我想要显示一个图像(大于iphone的屏幕),用户可以滚动。 不是很困难,我已经完成了这段代码: 在我的.h文件中
@interface mappa1 : UIViewController <UIScrollViewDelegate> {
IBOutlet UIImageView *img;
IBOutlet UIScrollView *scroller;
}
在我的.m文件中
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总是大100x25。
答案 0 :(得分:2)
解决了!
我不知道为什么,但如果我在Interface Builder中检查UserInteractionEnabled
(在UIImageView上),它就不起作用了。
如果我写
img.UserInteractionEnabled = YES;
它有效!
答案 1 :(得分: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];
答案 2 :(得分:-1)