问题描述
我创建了一个 UIScrollView,其中包含多个 UIImageView 实例.SDWebImage 框架用于缓存图像并将它们呈现在 UIImageView 实例中.现在我希望能够点击 UIImageView 的这些实例.
I created a UIScrollView which contains several UIImageView instances. The SDWebImage framework is used in order to cache images and present them in the UIImageView instances. Now I want to be able to click on these instances of UIImageView.
之前我将 UIButtons 添加到 UIScrollView 并为该按钮添加了一个图像.但是知道我无法将 UIImageView 添加到按钮.我应该如何解决这个问题?是否可以选择在此视图上放置 UIButton?我该怎么做?
Previously I added UIButtons to the UIScrollView and added an image to this button. But know I am not able to add a UIImageView to a button. How should I solve this? is it an option to place an UIButton over this view? and how should I do this?
我以前用它来添加可点击的图像(我需要改变它,由于 SDWebImage 框架,所以我不能使用 UIImage imageNamed:forState
I previously used this to add clickable images (I need to change this, due to the SDWebImage framework, so I cannot use UIImage imageNamed:forState
CGFloat y = i * self.view.frame.size.height/6;
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTag:[[idsCol objectAtIndex: i] intValue]];
aButton.frame = CGRectMake(0, y, self.view.frame.size.width/3, self.view.frame.size.height/6);
aButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
[aButton setImage:[UIImage imageNamed:@"image.jpg" forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
aButton.adjustsImageWhenHighlighted = NO;
[scrollView addSubview:aButton];
我目前需要使以下图像视图可点击:
I currently need to make the following imageview clickable:
[testImageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
推荐答案
我对 SDWebImage
不熟悉,但是如果这个框架允许你直接修改按钮的 imageView 属性你会更好关闭:
I'm unfamiliar with SDWebImage
, but if this framework would allow you to modify the buttons imageView property directly you'd be better off:
[myButton.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
或者,如果您想坚持使用图像视图,您可以通过向其添加一个简单的点击手势使其可点击":
Alternatively, if you want to stick with using a image view, you can make it "clickable" by adding a simple tap gesture to it:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myMethod:)];
[tap setNumberOfTapsRequired:1];
[tap setNumberOfTouchesRequired:1];
[testImageView setUserInteractionEnabled:YES];
[testImageView addGestureRecognizer:tap];
- (void)myMethod:(UIGestureRecognizer *)sender
{
NSLog(@"%u",sender.view.tag);
}
这篇关于将 UIImageView 添加到 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!