[英]UIButton over UIImageView (over UIScrollView)
我想要顯示一個圖像(大於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。
解決了! 我不知道為什么,但如果我在Interface Builder中檢查了UserInteractionEnabled
(在UIImageView上),它就不起作用了。 如果我寫
img.UserInteractionEnabled = YES;
有用!
我試過這個,它對我有用.......
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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.