0

我需要为 a 的最小值和最大值图像添加触摸响应UISlider。滑块是一个缩放条,因此图像是减号和加号,如果触摸,它们应该相应地放大或缩小。UISlider属性minimumValueImagemaximumValueImage是 type ,所以我UIImage不能UIImageView直接向它添加手势识别器。

有任何想法吗?是唯一的选择,可以在图像顶部制作清晰的按钮吗?如果是这种情况,我怎样才能获得最小/最大图像帧?

4

3 回答 3

0

您要查找的图像视图位于UISlider类的 @package 内,无法从外部访问

UIImageView *_minValueImageView;
UIImageView *_maxValueImageView;

您仍然可以使用目标 c 运行时获取它们。

#import <objc/runtime.h>

// Initialize slider and images
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
[slider setMinimumValueImage:[UIImage imageNamed:@"img1"]];
[slider setMaximumValueImage:[UIImage imageNamed:@"img2"]];

Ivar minimumValueImageViewIvar = class_getInstanceVariable([slider class], "_minValueImageView");
UIImageView *minimumValueImageView =  (UIImageView*)( object_getIvar(slider, minimumValueImageViewIvar) );
// [minimumValueImageView setHidden:YES];

Ivar maximumValueImageViewIvar = class_getInstanceVariable([slider class], "_maxValueImageView");
UIImageView *maximumValueImageView =  (UIImageView*)( object_getIvar(slider, maximumValueImageViewIvar) );
// [maximumValueImageView setHidden:YES];
于 2013-10-16T03:59:22.353 回答
0

您可以查看滑块的子视图。应该有 2 个 UIImageView。然后,您可以设置 userInteractionEnabled=YES 并向它们中的每一个添加一个 UITapGestureRecognizer。

NSMutableArray* images = [NSMutableArray array];
for (id sv in slider.subviews) {
   if ([sv isKindOfClass:[UIImageView class]]) {
       [image addObject:sv];
   }
}
if (images.count == 2) {
    UITapGestureRecognizer* gr = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                         action:@selector(minImageTapped:)];
    [imageViews[0] addGestureRecognizer:gr];
    [imageViews[0] setUserInteractionEnabled:YES];

    gr = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                 action:@selector(maxImageTapped:)];
    [imageViews[1] addGestureRecognizer:gr];
    [imageViews[1] setUserInteractionEnabled:YES];
}
于 2015-06-02T17:27:07.210 回答
0

GenesisST 答案的 Swift 版本:

let images:[UIView] = outsSlider.subviews.filter({$0 is UIImageView})

if (images.count == 2) {
    let tapMin = UITapGestureRecognizer(target: self, action: #selector(minImageTapped));
    let tapMax = UITapGestureRecognizer(target: self, action: #selector(maxImageTapped));
    images[0].addGestureRecognizer(tapMin)
    images[1].addGestureRecognizer(tapMax)
    images[0].isUserInteractionEnabled = true
    images[1].isUserInteractionEnabled = true
}
于 2018-07-21T19:31:39.270 回答