如何将另一个图像添加到滑块的拇指或如何添加两个拇指图像?

Posted

技术标签:

【中文标题】如何将另一个图像添加到滑块的拇指或如何添加两个拇指图像?【英文标题】:How to add a another image to Thumb of a slider or How to add two thumb images? 【发布时间】:2015-05-11 09:44:25 【问题描述】:

我正在使用故事板构建 ios 应用程序。我现在面临一个关键问题。我以编程方式创建了滑块。我添加了拇指图像作为我自己的自定义图像。我想增加和减少按钮点击的宽度。这些都工作正常。我唯一想做的是,我想在这个拇指图像下添加另一个图像。新添加的图像应与拇指图像分别移动。

【问题讨论】:

【参考方案1】:

这种方法会产生很多复杂性。最好创建自定义滑块。一切都必须以编程方式创建。

如下所示声明视图和框架。

UIView *slider,*timeLine,*mainView;
UIView *thumb;
CGRect sliderFrame,thumbFrame;
-(void)createTimeLine

    timeLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, mainView.frame.size.width, 10)];
    timeLine.backgroundColor=[UIColor colorWithRed:224.0/255.0f green:224.0/255.0f blue:224.0/255.0f  alpha:1.0];
    [mainView addSubview:timeLine];

    [self createSlider];
    [self createThumb];


-(void)createSlider

    sliderFrame=CGRectMake(50, 0, delta, 10);
    slider=[[UIView alloc]initWithFrame:sliderFrame];
    slider.backgroundColor=[UIColor colorWithRed:0.0/255.0f green:102.0/255.0f blue:0.0/255.0f  alpha:1.0];
    [mainView addSubview:slider];
    [slider bringSubviewToFront:mainView];

-(void)createThumb
    UIImage * thumbImage = [UIImage imageNamed:@"games@2x.png"];
    float actualHeight = thumbImage.size.height;
    float actualWidth = thumbImage.size.width;
    thumbFrame = CGRectMake((fabs(actualWidth - sliderFrame.size.width))/2,timeLine.frame.size.height,actualWidth,actualHeight);
    thumb=[[UIView alloc]initWithFrame:thumbFrame];
    thumb.center = CGPointMake(sliderFrame.origin.x + sliderFrame.size.width/2, sliderFrame.size.height+thumbFrame.size.height/2);
    thumb.backgroundColor= [UIColor colorWithPatternImage:thumbImage];
    thumb.userInteractionEnabled = YES;

    [mainView insertSubview:thumb atIndex:mainView.subviews.count];

    UIPanGestureRecognizer *move=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(drag:)];
    move.maximumNumberOfTouches = 1;
    move.minimumNumberOfTouches = 1;
    [thumb addGestureRecognizer:move];

-(void)drag:(UIPanGestureRecognizer *)panGestureRecognizer 
    CGPoint touchLocation = [panGestureRecognizer locationInView:mainView];
    if (panGestureRecognizer.state == UIGestureRecognizerStateBegan) 
    
    else if (panGestureRecognizer.state == UIGestureRecognizerStateChanged) 
        if ((touchLocation.x>(timeLine.frame.origin.x-(thumbFrame.size.width/2))) && (touchLocation.x <= (timeLine.frame.size.width-(thumbFrame.size.width/2))))
        
            thumbFrame.origin.x =touchLocation.x;
            thumb.frame = thumbFrame;
            sliderFrame.origin.x = touchLocation.x + (thumbFrame.size.width - sliderFrame.size.width)/2;
            slider.frame = sliderFrame;

        

     else if (panGestureRecognizer.state == UIGestureRecognizerStateEnded) 
    

Pangesture 用于移动 thumbImage。

【讨论】:

以上是关于如何将另一个图像添加到滑块的拇指或如何添加两个拇指图像?的主要内容,如果未能解决你的问题,请参考以下文章

拇指图像不会移动到 UISlider 的边缘

如何获取 UISlider 拇指位置

如何使用步进滑块在滑块拇指上添加 UILabel?

将滑块的拇指模板编辑为图像 Windows Phone 7

如何在 iOS 中添加双拇指滑块?以及为啥这个双滑块库不再适用于 xcode 7

如何找到滑块的拇指来设置其宽度