【问题标题】:UIButton over UIImageView (over UIScrollView)UIImageView 上的 UIButton(在 UIScrollView 上)
【发布时间】:2011-04-19 16:01:43
【问题描述】:

我想显示一个用户可以滚动的图像(大于 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。

【问题讨论】:

  • scrollViews 有一个属性“Cancellable Content Touches”。您可以在 IB 的属性检查器中找到它。取消选中该选项,然后尝试。
  • 谢谢,但不,它不起作用!另外,如果我取消选中“cancellabla content touches”,缩放不起作用!
  • 您可能需要根据 UIImage 上的缩放级别手动调整按钮的大小。因此,每当有人放大图像时,您就可以调整按钮大小。
  • 是的,uibutton 位于 uiscrollview 上方。问题是:我怎么知道图像的缩放比例是多少?但是没有办法在uiimageview上添加uibutton(就像我的代码一样)并使uibutton工作?
  • 为此,您必须继承滚动视图以检测子视图,并基于此您可以将动作事件赋予相同的我给您类似的链接,但您必须解决每个根据您的要求进行操作

标签: iphone uiscrollview uiimageview uibutton


【解决方案1】:

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

img.UserInteractionEnabled = YES;

有效!

【讨论】:

    【解决方案2】:

    我试过了,它对我有用.......

    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];
    

    【讨论】:

      【解决方案3】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      • 2016-05-26
      • 1970-01-01
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多