【问题标题】: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 应用程序。我现在面临一个关键问题。我以编程方式创建了滑块。我添加了拇指图像作为我自己的自定义图像。我想增加和减少按钮点击的宽度。这些都工作正常。我唯一想做的是,我想在这个拇指图像下添加另一个图像。新添加的图像应与拇指图像分别移动。

【问题讨论】:

    标签: ios uiimageview uibutton uislider


    【解决方案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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      相关资源
      最近更新 更多